Saving an Array into Session

Discussion in 'PHP' started by Shaimaa, Feb 17, 2010.

  1. #1
    I have something like:

    <table>
    	 <tr>
         <td><input type="checkbox" name="day[]" value="Saturday" />Satur</td>
    	 
         <td><input type="checkbox" name="day[]" value="Sunday" />Sun</td>
         
         <td><input type="checkbox" name="day[]" value="Monday" />Mon</td>
         
         <td><input type="checkbox" name="day[]" value="Tuesday" />Tues</td>
        
         <td><input type="checkbox" name="day[]" value="Wednesday" />Wednes</td>
         
         <td><input type="checkbox" name="day[]" value="Thursday" />Thurs</td>
         
         <td><input type="checkbox" name="day[]" value="Friday" />Fri</td>
    	 </tr>
    	 </table>
    PHP:
    and I want to save day[] in a session and reuse it again how can I do that?

    Thank you in advance
     
    Shaimaa, Feb 17, 2010 IP
  2. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I did somthing like

    $day        =$_POST['day'];
    PHP:
    then I saved $day in a session

    $_SESSION['day'] = $day        ; // store session data
    PHP:
    then I want to display days in SESSION

    so I wrote:
    
    foreach($_SESSION['day'] as $day){
    echo "Session day: ".$day."<br>";
    }
    PHP:
    and it gives me the following

    Invalid argument supplied for foreach()
     
    Shaimaa, Feb 17, 2010 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    I'm not sure the reason you can't directly assign your $_POST array to a $_SESSION array and there might be a function to make it more effecient, but this worked for me:

    
    
    $_SESSION['day'];
    for ($i=0; $i<count($_POST['day']); ++$i) $_SESSION['day'][$i]=$_POST['day'][$i];
    
    PHP:
     
    plog, Feb 17, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    koko5, Feb 18, 2010 IP
  5. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes I am calling

    session_start(); 
    PHP:
    in the start of the file

    and day is an array as shown below

    <table> 
         <tr> 
         <td><input type="checkbox" name="day[]" value="Saturday" />Satur</td> 
          
         <td><input type="checkbox" name="day[]" value="Sunday" />Sun</td> 
          
         <td><input type="checkbox" name="day[]" value="Monday" />Mon</td> 
          
         <td><input type="checkbox" name="day[]" value="Tuesday" />Tues</td> 
         
         <td><input type="checkbox" name="day[]" value="Wednesday" />Wednes</td> 
          
         <td><input type="checkbox" name="day[]" value="Thursday" />Thurs</td> 
          
         <td><input type="checkbox" name="day[]" value="Friday" />Fri</td> 
         </tr> 
         </table>  
    PHP:
    and I get it from the POST method and save it

     $day        =$_POST['day'];  
    
    PHP:
    and the save it to a SESSION

     $_SESSION['day'] = $day        ; // store session data  
    PHP:
    then I want to display what's saved in SESSION and I don't know how?

    Can anyone tell me how?
     
    Shaimaa, Feb 18, 2010 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    This code will get into the session:

    
    $_SESSION['day'];
    for ($i=0; $i<count($_POST['day']); ++$i) $_SESSION['day'][$i]=$_POST['day'][$i];
    
    PHP:
    And this code will get it out:

    
    for ($i=0; $i<count($_SESSION['day']; ++$i) echo "Session day: ".$_SESSION['day'][$i]."<br>";
    
    PHP:
     
    plog, Feb 18, 2010 IP
  7. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have created 3 pages:

    first page

    called combobox.php

    <?php
    echo
    	'<form name="combo" method="post" action="post_days.php">
    	<table> 
        <tr> 
        <td><input type="checkbox" name="day[]" value="Saturday" />Satur</td> 
          
        <td><input type="checkbox" name="day[]" value="Sunday" />Sun</td> 
          
        <td><input type="checkbox" name="day[]" value="Monday" />Mon</td> 
          
        <td><input type="checkbox" name="day[]" value="Tuesday" />Tues</td> 
         
        <td><input type="checkbox" name="day[]" value="Wednesday" />Wednes</td> 
          
        <td><input type="checkbox" name="day[]" value="Thursday" />Thurs</td> 
          
        <td><input type="checkbox" name="day[]" value="Friday" />Fri</td> 
        </tr> 
    	<tr>
    	<input type="submit" />
    	</tr>
        </table>
    	</form>
    	';
    	 
    ?>
    PHP:
    second page:

    post_days.php
    
    <?php
    	session_start();
    	
    	//get into session
    	for ($i=0; $i<count($_POST['day']); $i++)
    	{
    		echo "POST Day... ".$_POST['day'][$i]."<br>";
    		$_SESSION['day'][$i]=$_POST['day'][$i];
    		echo "For 1... Session Day: ".$_SESSION['day'][$i]."<br>";
    	}
    	
    	//$_SESSION['day'] = $_POST['day'];
    	//get it out
    	for ($i=0; $i<count($_POST['day']); $i++){
    		echo "For 2... Session day: ".$_SESSION['day'][$i]."<br>";
    	}
    	
    	echo
    	'<form method="post" action="insert_days.php">
    	<input type="submit" />
    	</form>';
    ?>
    PHP:
    and the output is

    POST Day... Saturday
    For 1... Session Day: S
    POST Day... Wednesday
    For 1... Session Day: W
    For 2... Session day: S
    For 2... Session day: W

    I don't know why it's saved as single letter I want to know how to solve this problem
     
    Shaimaa, Feb 21, 2010 IP
  8. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Look at what you're inserting and echoing: $_POST['day'][$i]

    That will show the $i character in $_POST['day'] which is absolutely wrong.

    You can't run through that correctly in a for-loop. Use foreach instead.
    
    foreach ($_POST['day'] as &$value) {
        echo $value."<br />";
    }
    
    Code (markup):
     
    elias_sorensen, Feb 21, 2010 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    check what's going in the session:

    <?php echo '<pre>'; print_r($_SESSION); ?>

    Your first foreach error was because the data $_SESSION['day'] is not an array type.
    Use this code:

    
    <?php
    session_start();
    if(isset($_POST[submit])){ $_SESSION['d']= $_POST['d']; }
    
    //echo '<pre>'; print_r($_SESSION);
    
    //show session data
    foreach($_SESSION['d'] as $v){ echo $v.'<br>'; }
    
    print '
    <form action="" method="post">
    <input type="checkbox" name="d[]" value="sunday"> Sunday <br>
    <input type="checkbox" name="d[]" value="monday"> monday <br>
    <input type="checkbox" name="d[]" value="tuesday"> Tuesday<br>
    <input type="submit" name="submit">
    </form>
    ';
    ?>
    
    Code (markup):
     
    JEET, Feb 21, 2010 IP