SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I assign an array to session and check each value??

    hello people,
    How can I assign an array to a session. I have values I am getting from the database then assign them to an array. Now I would like to assign an array to a session. Thereafter, I would like to use each value in a session array different. meaning at each page it should check if there is given number in session

    For example, I session may have numbers 1, 6,8, 9.

    Now I would like to test or check if in a session there is 9 or 6 then do something? I can I achieve this?

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Hi zamzam,
    comments are in the code:
    PHP Code:
    /** start the session
        ** */
    session_start();

    /** temporary array
        ** This would be the output from your database
        ** query
        ** */
    $tempArray = array(1,3,4,5,7,9,8);

    /** dump the contents of the tempArray
        ** into a SESSION variable
        ** */
    $_SESSION['set_tempArray'] = $tempArray;

    /** use array_key_exists([key], [array to search]) 
        ** to check for the value in the array
        ** */
    if(array_key_exists(1$_SESSION['set_tempArray'])) {
        echo 
    'its here';

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the other way of writing that array_key does not exit? opposite of if(array_key_exists(1, $_SESSION['set_tempArray'])) {
    echo 'its here';
    }


    what is the other way of assign a dynamic an array? I have an array that is pulling from database. But I am having problems assign the dynamic array to an array. my code is below

    PHP Code:
    $query="select id from  studentRole  where user='$user'  order by  Id ASC"
                
    $result=mysql_query($query);        
                   echo 
    mysql_error(); 
                  
                while((
    $row mysql_fetch_assoc($result)))
                   {
                        
    $info[] = $row;
                   }
                   
    //Assign the array to session
                             
    $tempArrayPermission = array($info);
                  
    //Session with roles
                  
    $_SESSION['set_tempArray'] = $tempArrayPermission

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    ok, to reverse a function use !
    eg:
    PHP Code:
    if(!array_key_exists($key$array)) { 
    PHP Code:
    /** start the session
        ** */
    session_start();

    /** database stuff
        ** */
    mysql_connect('localhost''''');
    mysql_select_db('sitepoint');


    /** query
        ** select all the id's from the table
        ** ugid
        ** */
    $sql mysql_query("
        select
            ugid
        from
            tabs    
    "
    );

    /** get the results from the database
        ** as an associated ARRAY
        ** */
    $rows mysql_fetch_assoc($sql);

    /** dump the contents of the tempArray
        ** into a SESSION variable
        ** */
    $_SESSION['set_tempArray'] = $rows;

    /** use array_key_exists([key], [array to search]) 
        ** to check for the value in the array
        ** */
    if(array_key_exists(1$_SESSION['set_tempArray'])) {
        echo 
    'its here in the array';
    }

    /** use !array_key_exists([key], [array to search]) 
        ** to check for the value in NOT the array
        ** */
    if(!array_key_exists(1$_SESSION['set_tempArray'])) {
        echo 
    'its missing from the array';


    That pulls from the database and puts the results into an array. You could also use mysql_fetch_array($sql);
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    How can I move array from in a session from one page to another and use it?
    $_SESSION['set_tempArray']

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    SESSION data is available on any page as long as you have included the lines:
    PHP Code:
    /** start the session
        ** */
    session_start(); 
    put that at the top of the page.

    http://www.php.net/sessions
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have done that but it is not working? where I am going wrong? basicaly I want the values in an array to move from one page to another in a session the used to check if the a given number exit. There two pages

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    ok, on page 2 put the following code:
    PHP Code:
    /** start the session
        ** */
    session_start(); 

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

    And see what is displayed. If nothing is displayed then the SESSION is set up wrong.#
    Post what the output is.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is displaying the values in an array, but it is not finding the values in the condition. What I mean is that when I put a check to say if there is no value 2 say this or do this. it does different even when value 2 is there? The code I am talking about is below. Help

    PHP Code:
    $Myselect $_SESSION['set_tempArray'];

    if(!
    array_key_exists(2,$Myselec)) 
    {
    //say this


  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    There is a typo in your code:
    $Myselect and $Myselec

    If that isnt it, post your full code.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    I have corrected but it is not working. The problem is that is not checking the values in an array but index. What Iam using is below please help

    PHP Code:
    if(!array_key_exists(5,$_SESSION['set_tempArray'])) 
    {
    //say this


  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    post the output of the SESSION array. If its checking keys and you want to check values then it is different.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The output of array is as below. How do I check the values in an array?
    Array
    (
    [set_tempArray] => Array
    (
    [0] => Array
    (
    [Id] => 2
    )

    [1] => Array
    (
    [Id] => 3
    )

    [2] => Array
    (
    [Id] => 4
    )

    [3] => Array
    (
    [Id] => 5
    )

    )

  14. #14
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try,

    array_key_exists(5,array_values($_SESSION['set_tempArray']))

  15. #15
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    The reason it fails is because you have a multi dimensional array (an array with arrays as values). in_array or array_key_exists only work on single dimensions.

    try:
    PHP Code:
    $set_tempArray = array(
        
    => array(3),
        
    => array(4),
        
    => array(5)
    );

    /** iterate through the 
        ** main array
        ** */

    foreach($set_tempArray as $data) {

    /** iterate through the sub array
        ** */

        
    foreach($data as $subData) {

    /** compare the $subData value to the 
        ** value you are looking for
        ** */
            
    if($subData == 3) {
                echo 
    'Here';
            }
        }


    Is there any reason you are using a multi array?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  16. #16
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. How can I make single dimensions array when values are coming from the database?

    2Second want I want to achieve is permission on pages. You know,I want someone to have more than one role. So these roles are presented by numbers e.g 5, 9, 6,5. Now for someone to access a page, in that array they should a given number. E.g To access page A there should value 9 in the array.If there is no 9 it should not allow the person to access the page.

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by zamzam View Post
    1. How can I make single dimensions array when values are coming from the database?
    See my code in post#4
    mysql_fetch_assoc() gives you the array - you dont then need to manipulate it any further.

    Quote Originally Posted by zamzam View Post
    2Second want I want to achieve is permission on pages. You know,I want someone to have more than one role. So these roles are presented by numbers e.g 5, 9, 6,5. Now for someone to access a page, in that array they should a given number. E.g To access page A there should value 9 in the array.If there is no 9 it should not allow the person to access the page.
    http://www.codewalkers.com/c/a/Misce...System-in-PHP/
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •