Professional Web Applications Themes

[PHP] Store array as Session Variable - PHP Development

$details is an array (just like $company_name was). Try to view print_r($details); and see what you get. ---John Holmes... ----- Original Message ----- From: "Pushpinder Singh Garcha" <pgarchaadelphia.net> To: <php-generallists.php.net> Sent: Monday, July 28, 2003 3:18 PM Subject: [PHP] Store array as Session Variable hello All, I am trying to store an array as a session variable. while ($row = mysql_fetch_array($result)) { if ( !is_array($company_name) ) $company_name = array(); array_push($company_name, $row['company']); // try to register session variable $_SESSION['link'] = $company_name; } Now when I try to access this session variable in another file...I am getting a junk value.I am not ...

  1. #1

    Default Re: [PHP] Store array as Session Variable

    $details is an array (just like $company_name was). Try to view
    print_r($details); and see what you get.

    ---John Holmes...

    ----- Original Message -----
    From: "Pushpinder Singh Garcha" <pgarchaadelphia.net>
    To: <php-generallists.php.net>
    Sent: Monday, July 28, 2003 3:18 PM
    Subject: [PHP] Store array as Session Variable


    hello All,

    I am trying to store an array as a session variable.

    while ($row = mysql_fetch_array($result))
    {
    if ( !is_array($company_name) ) $company_name = array();
    array_push($company_name, $row['company']);

    // try to register session variable
    $_SESSION['link'] = $company_name;

    }


    Now when I try to access this session variable in another file...I am
    getting a junk value.I am not sure if I am getting this right.

    $details = $_SESSION['link'];
    echo "LINK: ".$details."<br>";


    Can some one fill me in on this. Appreciate it ! Thanks in advance.

    --Pushpinder Singh

    Cpt John W. Holmes Guest

  2. #2

    Default Re: [PHP] Store array as Session Variable

    --- Pushpinder Singh Garcha <pgarchaadelphia.net> wrote:
    > I am trying to store the value in row['company'] in an array
    > called $company_name.
    I see now. I think you might find this a bit simpler (if I understand
    correctly):

    $company_name = array();
    while ($row = mysql_fetch_assoc($result))
    {
    $company_name[] = $row['company'];
    }

    Also, at this point, you should probably examine the contents of $company_name
    before bothering with the session stuff. You may be having trouble with your
    query or something that is totally unrelated to sessions. Try John's suggestion
    of print_r() by doing something like this:

    echo '<pre>';
    print_r($company_name);
    echo '</pre>';

    Hope that helps.

    Chris

    =====
    Become a better Web developer with the HTTP Developer's Handbook
    [url]http://httphandbook.org/[/url]
    Chris Shiflett Guest

  3. #3

    Default Re: [PHP] Store array as Session Variable

    Thanks Chris,

    I am able to see the contents of the $company_name array. The next
    part involves storing this as a session variable.

    I am trying to link this page to another search page using the <a
    href=\"full-profile.php?name=".$row['company']. "\">--Link--</a>
    > while ($row = mysql_fetch_assoc($result))
    > {
    > "<TR BGCOLOR=$bgcolor>
    > <TD align=\"left\"><font color=\"#666666\" size=\"1\"
    > face=\"Verdana, Arial, Helvetica,
    > sans-serif\">$row[company]</font></TD>
    > <TD align=\"left\"><font color=\"#666666\" size=\"1\"
    > face=\"Verdana, Arial, Helvetica, > sans-serif\">$row[name_1]</font></TD>
    > <TD align=\"left\"><font color=\"#666666\" size=\"1\"
    > face=\"Verdana, Arial, Helvetica,
    > sans-serif\">$row[phone_1]</font></TD>
    > <TD align=\"left\"><font color=\"#666666\" size=\"1\" face=\"Verdana,
    > Arial, Helvetica, sans-serif\">$row[city]</font></TD>
    > <TD align=\"left\"><font color=\"#666666\" size=\"1\" face=\"Verdana,
    > Arial, Helvetica, sans-serif\">$row[url]</font></TD>
    > <TD align=\"left\"><font color=\"#666666\" size=\"1\" face=\"Verdana,
    > Arial, Helvetica, sans-serif\">$row[email_1]</font></TD>
    > <TD align=\"center\"><font color=\"#333333\" size=\"1\"
    > face=\"Verdana, Arial, Helvetica, sans-serif\"><a
    > href=\"full-profile.php?name=".$row['company']."\">--Link--</a></
    > font></TD> </TR>";
    > $company_name[] = $row['company'];
    // try to register the variable
    $_SESSION['link'] = $company_name;
    > }

    What would be the best way to register the array $company_name as a
    session var and access it on the following page. $_SESSION['link'] =
    $company_name;
    does not seem to work since it gives NULL when i try to echo
    $_SESSION['link'];


    Thanks in advance
    --Pushpinder



    On Monday, July 28, 2003, at 03:43 PM, Chris Shiflett wrote:
    > --- Pushpinder Singh Garcha <pgarchaadelphia.net> wrote:
    >> I am trying to store the value in row['company'] in an array
    >> called $company_name.
    >
    > I see now. I think you might find this a bit simpler (if I understand
    > correctly):
    >
    > $company_name = array();
    > while ($row = mysql_fetch_assoc($result))
    > {
    > $company_name[] = $row['company'];
    > }
    >
    > Also, at this point, you should probably examine the contents of
    > $company_name
    > before bothering with the session stuff. You may be having trouble
    > with your
    > query or something that is totally unrelated to sessions. Try John's
    > suggestion
    > of print_r() by doing something like this:
    >
    > echo '<pre>';
    > print_r($company_name);
    > echo '</pre>';
    >
    > Hope that helps.
    >
    > Chris
    >
    > =====
    > Become a better Web developer with the HTTP Developer's Handbook
    > http://httphandbook.org/
    >
    Pushpinder Singh Garcha Guest

  4. #4

    Default Re: [PHP] Store array as Session Variable

    --- Pushpinder Singh Garcha <pgarchaadelphia.net> wrote:
    > I am able to see the contents of the $company_name array. The
    > next part involves storing this as a session variable.
    Assuming $company_name is set correctly, as you verified, this should suffice:

    $_SESSION['company_name'] = $company_name;

    I should mention that you will need a session_start() call on the page you wish
    to set session variables on as well as any page that you wish to read session
    variables from.
    > I am trying to link this page to another search page using the <a
    > href=\"full-profile.php?name=".$row['company']. "\">--Link--</a>
    >
    > > while ($row = mysql_fetch_assoc($result))
    > > {
    > > "<TR BGCOLOR=$bgcolor>
    > > <TD align=\"left\"><font color=\"#666666\" size=\"1\"
    > > face=\"Verdana, Arial, Helvetica,
    > > sans-serif\">$row[company]</font></TD>
    > > <TD align=\"left\"><font color=\"#666666\" size=\"1\"
    > > face=\"Verdana, Arial, Helvetica, > sans-serif\">$row[name_1]</font></TD>
    > > <TD align=\"left\"><font color=\"#666666\" size=\"1\"
    > > face=\"Verdana, Arial, Helvetica,
    > > sans-serif\">$row[phone_1]</font></TD>
    > > <TD align=\"left\"><font color=\"#666666\" size=\"1\" face=\"Verdana,
    > > Arial, Helvetica, sans-serif\">$row[city]</font></TD>
    > > <TD align=\"left\"><font color=\"#666666\" size=\"1\" face=\"Verdana,
    > > Arial, Helvetica, sans-serif\">$row[url]</font></TD>
    > > <TD align=\"left\"><font color=\"#666666\" size=\"1\" face=\"Verdana,
    > > Arial, Helvetica, sans-serif\">$row[email_1]</font></TD>
    > > <TD align=\"center\"><font color=\"#333333\" size=\"1\"
    > > face=\"Verdana, Arial, Helvetica, sans-serif\"><a
    > > href=\"full-profile.php?name=".$row['company']."\">--Link--</a></
    > > font></TD> </TR>";
    This is about the best example I've seen as to why stylesheets are handy. :-)

    Is there an echo in there that I missed? I don't see how this is being output.

    Chris

    =====
    Become a better Web developer with the HTTP Developer's Handbook
    http://httphandbook.org/
    Chris Shiflett Guest

  5. #5

    Default Re: [PHP] Store array as Session Variable

    Thanks John,

    I used the $_GET['name'] on the next page to access the
    selection.

    [url]http://psg.local/~psgarcha/CRM/full-profile.php?name=jack[/url]
    [url]http://psg.local/~psgarcha/CRM/full-profile.php?name=tim[/url]
    [url]http://psg.local/~psgarcha/CRM/full-profile.php?name=mary[/url]
    [url]http://psg.local/~psgarcha/CRM/full-profile.php?name=evian[/url]

    My full-profile.php page first checks for a session before displaying
    the results.

    However if I decided to use the sessions approach how would I go about
    it ? I am just curious to find out why $_SESSION['company_name'] =
    $company_name; duz not werk for me !

    Thanks in advance
    --Pushpinder






    On Monday, July 28, 2003, at 04:14 PM, CPT John W. Holmes wrote:
    >> I am able to see the contents of the $company_name array. The
    >> next
    >> part involves storing this as a session variable.
    >>
    >> I am trying to link this page to another search page using the <a
    >> href=\"full-profile.php?name=".$row['company']. "\">--Link--</a>
    >
    > If you're passing the company name in the URL, why are you adding the
    > entire
    > array to the session? You can just use $_GET['name'] to get the name
    > of the
    > "company" that was clicked on...
    >
    > ---John Holmes...
    >
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    Pushpinder Singh Garcha Guest

  6. #6

    Default Re: [PHP] Store array as Session Variable

    --- Pushpinder Singh Garcha <pgarchaadelphia.net> wrote:
    > However if I decided to use the sessions approach how would I go about
    > it? I am just curious to find out why $_SESSION['company_name'] =
    > $company_name; duz not werk for me!
    Did you use session_start() like I mentioned?

    If so, you are probably making an error somewhere due to the complexity of your
    script(s). If you want to learn how to use sessions, focus on that and nothing
    else (e.g., forget interacting with a database, passing URL variables, etc.).
    Start with a simple example, get that to work, then integrate it into what you
    are trying to do.

    Hope that helps.

    Chris

    =====
    Become a better Web developer with the HTTP Developer's Handbook
    [url]http://httphandbook.org/[/url]
    Chris Shiflett Guest

  7. #7

    Default Re: [PHP] Store array as Session Variable

    Hello Chris,

    I am using session_start(). All the pages after the login page use
    sessions and will redirect the user to the login page, if he/she is NOT
    logged in. So I am guessing the error lies in my logic somewhere.


    Thanks again.
    --Pushpinder


    On Monday, July 28, 2003, at 04:51 PM, Chris Shiflett wrote:
    > --- Pushpinder Singh Garcha <pgarchaadelphia.net> wrote:
    >> However if I decided to use the sessions approach how would I go about
    >> it? I am just curious to find out why $_SESSION['company_name'] =
    >> $company_name; duz not werk for me!
    >
    > Did you use session_start() like I mentioned?
    >
    > If so, you are probably making an error somewhere due to the
    > complexity of your
    > script(s). If you want to learn how to use sessions, focus on that and
    > nothing
    > else (e.g., forget interacting with a database, passing URL variables,
    > etc.).
    > Start with a simple example, get that to work, then integrate it into
    > what you
    > are trying to do.
    >
    > Hope that helps.
    >
    > Chris
    >
    > =====
    > Become a better Web developer with the HTTP Developer's Handbook
    > [url]http://httphandbook.org/[/url]
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    Pushpinder Singh Garcha Guest

  8. #8

    Default Re: [PHP] Store array as Session Variable

    Hello Chris,

    I am using Apache webserver on the MAc Jaguar OS Platform

    Regards
    --Pushpinder


    On Monday, July 28, 2003, at 05:02 PM, Chris Shiflett wrote:
    > --- Pushpinder Singh Garcha <pgarchaadelphia.net> wrote:
    >> I am using session_start(). All the pages after the login page use
    >> sessions and will redirect the user to the login page, if he/she is
    >> NOT logged in.
    >
    > You're not using IIS by chance are you? If so, I think you will find
    > this link
    > helpful:
    >
    > [url]http://marc.theaimsgroup.com/?l=php-general&m=102929828515647&w=2[/url]
    >
    > Chris
    >
    > =====
    > Become a better Web developer with the HTTP Developer's Handbook
    > [url]http://httphandbook.org/[/url]
    >
    Pushpinder Singh Garcha Guest

  9. #9

    Default Re: [PHP] Store array as Session Variable


    "Pushpinder Singh Garcha" <pgarchaadelphia.net> wrote in message
    news:E8A08D14-C131-11D7-8E7F-000393874598adelphia.net...
    > $details is empty !!
    >
    I'd check your sql query and see if any rows are being returned. Also, put
    the initializing of the array ($company_name = array()) outside of your
    loop, just in case.

    I used to think that you had to serialize arrays before saving to session,
    but apparently that's not the case. Good to know.

    -- Rob


    > Thanks
    > --Pushpinder
    >
    >
    > On Monday, July 28, 2003, at 03:31 PM, CPT John W. Holmes wrote:
    >
    > > $details is an array (just like $company_name was). Try to view
    > > print_r($details); and see what you get.
    > >
    > > ---John Holmes...
    > >
    > > ----- Original Message -----
    > > From: "Pushpinder Singh Garcha" <pgarchaadelphia.net>
    > > To: <php-generallists.php.net>
    > > Sent: Monday, July 28, 2003 3:18 PM
    > > Subject: [PHP] Store array as Session Variable
    > >
    > >
    > > hello All,
    > >
    > > I am trying to store an array as a session variable.
    > >
    > > while ($row = mysql_fetch_array($result))
    > > {
    > > if ( !is_array($company_name) ) $company_name = array();
    > > array_push($company_name, $row['company']);
    > >
    > > // try to register session variable
    > > $_SESSION['link'] = $company_name;
    > >
    > > }
    > >
    > >
    > > Now when I try to access this session variable in another file...I am
    > > getting a junk value.I am not sure if I am getting this right.
    > >
    > > $details = $_SESSION['link'];
    > > echo "LINK: ".$details."<br>";
    > >
    > >
    > > Can some one fill me in on this. Appreciate it ! Thanks in advance.
    > >
    > > --Pushpinder Singh
    > >
    >

    Rob Adams Guest

  10. #10

    Default Re: [PHP] Store array as Session Variable

    Hi

    Try this

    $_SESSION[my_session] = array("item_1" => "Some Value" , "item_2" => "Some
    Value2" , "item_3" => "Some Value3");

    This allow s you to store the array in the session

    Pradeep DSouza
    Naharonline.com


    ----- Original Message -----
    From: "Pushpinder Singh Garcha" <pgarchaadelphia.net>
    To: "CPT John W. Holmes" <holmes072000charter.net>
    Cc: <php-generallists.php.net>
    Sent: Tuesday, July 29, 2003 1:00 AM
    Subject: Re: [PHP] Store array as Session Variable

    > $details is empty !!
    >
    > Thanks
    > --Pushpinder
    >
    >
    > On Monday, July 28, 2003, at 03:31 PM, CPT John W. Holmes wrote:
    >
    > > $details is an array (just like $company_name was). Try to view
    > > print_r($details); and see what you get.
    > >
    > > ---John Holmes...
    > >
    > > ----- Original Message -----
    > > From: "Pushpinder Singh Garcha" <pgarchaadelphia.net>
    > > To: <php-generallists.php.net>
    > > Sent: Monday, July 28, 2003 3:18 PM
    > > Subject: [PHP] Store array as Session Variable
    > >
    > >
    > > hello All,
    > >
    > > I am trying to store an array as a session variable.
    > >
    > > while ($row = mysql_fetch_array($result))
    > > {
    > > if ( !is_array($company_name) ) $company_name = array();
    > > array_push($company_name, $row['company']);
    > >
    > > // try to register session variable
    > > $_SESSION['link'] = $company_name;
    > >
    > > }
    > >
    > >
    > > Now when I try to access this session variable in another file...I am
    > > getting a junk value.I am not sure if I am getting this right.
    > >
    > > $details = $_SESSION['link'];
    > > echo "LINK: ".$details."<br>";
    > >
    > >
    > > Can some one fill me in on this. Appreciate it ! Thanks in advance.
    > >
    > > --Pushpinder Singh
    > >
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >
    >
    Pradeep D'Souza Guest

Similar Threads

  1. #39251 [NEW]: variable variable class array property is read only
    By taskfreak at gmail dot com in forum PHP Bugs
    Replies: 1
    Last Post: October 25th, 07:08 AM
  2. Writing an array into a session variable
    By Udo Schmitt in forum ASP Components
    Replies: 0
    Last Post: February 16th, 04:52 PM
  3. session variable with array
    By alternative in forum Macromedia ColdFusion
    Replies: 2
    Last Post: March 4th, 01:24 PM
  4. how to create a session variable as an array
    By alvis in forum ASP.NET Web Services
    Replies: 1
    Last Post: April 9th, 09:00 PM
  5. [SESSION] Session variable deleted prior to command?
    By Ivo Fokkema in forum PHP Development
    Replies: 0
    Last Post: July 16th, 12:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139