Jump to content


Photo

Session Problems


  • Please log in to reply
4 replies to this topic

#1 mister_t101

mister_t101
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 March 2006 - 02:17 PM

I've looked everywhere in trying to help answer this question...

I am trying to create a script that combines the results of several form submissions, using a 2 dimensional
array. My problem is that the script doesn't seem to save the previous array, yet I know that the session function is working, because some variables are being updated. Can you create an array as a session variable?

What I want to end up happening is that on array submission, the entire array is printed, including the new information.....

on submission,
print

form
quantity
size

form
quantity
size
.
.
.


Here is the code
<html><body><br /><br />
<table width="760" border="1">
  <tr>
    <td width="257"><h4></h4>
      <form action="<?php $PHP_SELF;?>" method="post">
  Pose
  <select name="pose">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
  <br>
  <br>
  Quantity
  <input name="quantity" type="text" value="1" size="3" maxlength="3">
  <br>
  <br>
  Size
  <select name="size">
    <option value="wallets">Wallets</option>
    <option value="5x7">5 x 7</option>
    <option value="8x10" selected>8 x 10</option>
    <option value="10x13">10 x 13</option>
  </select>
  <input type="hidden" name="_submit_check" value="1"/>
  <input type="submit">
      </form></td>
    <td width="487">&nbsp;
    
    <?php
session_start();
$_SESSION['totalBasicPoints'];
$_SESSION['productNumber'];
$_SESSION['order'] = array();

    if (array_key_exists('_submit_check', $_POST)) {

$pose = $_POST['pose'];
$quantity = $_POST['quantity'];
$size = $_POST['size'];
$prodNum = $_POST['prodNum'];

$_SESSION['product'] = array(
                       "pose"=>$pose,
                       "quantity"=>$quantity,
                       "size"=>$size);           
                    
array_push($_SESSION['order'], $_SESSION[product]);
foreach ($_SESSION['order'] as $v){
    foreach ($v as $desc){
        print $desc. "<br />";
 } print "<br /><br />";
 }
  
while (list($key,$value) = each($_SESSION['order'])) {
echo "$key : $value<br>";
while (list($value,$desc) = each($_SESSION['order'])) {
echo "$key : $desc<br>";}
}
    }
?></td>
  </tr>
</table>
<h4><br />
  <br />
</h4>
</body></html>

EDITED BY OBER: PLEASE USE CODE TAGS WHEN POSTING CODE. THANKS.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 23 March 2006 - 02:42 PM

Yes, you can use arrays in session variables, however I'm not sure that you can use 2 dimensional arrays in sessions.

What you should really be doing for something this complicated is setting up temporary database records. It would not only be more reliable, but it would probably be easier to work with than a bunch of multi-dimensional arrays.

Info: PHP Manual


#3 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 23 March 2006 - 06:38 PM

[!--quoteo(post=357605:date=Mar 23 2006, 02:42 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 23 2006, 02:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yes, you can use arrays in session variables, however I'm not sure that you can use 2 dimensional arrays in sessions.

What you should really be doing for something this complicated is setting up temporary database records. It would not only be more reliable, but it would probably be easier to work with than a bunch of multi-dimensional arrays.
[/quote]

In order to use arrays in $_SESSION variables across multiple pages, you must first serialize() them for transport, and unserialize() them on the next page

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 March 2006 - 08:35 PM

Session variables, are autmatically serialized, you don't have to do anything except create them.

Ken

#5 mister_t101

mister_t101
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 March 2006 - 04:30 AM

after looking at [a href=\"http://www.codecomments.com/archive128-2005-1-380939.html\" target=\"_blank\"]http://www.codecomments.com/archive128-2005-1-380939.html[/a], I think i am a little bit closer to the answer; however, when I do a "print_r($_SESSION['order']);", it only prints the most recent form submission, and it doesn't seem like it's saving a new array for each form submission, or maybe I'm only asking it to tell me the most recent form submission. I don't know. The most recent code looks like this



    <?php
session_start();
session_register('totalBasicPoints');
session_register('order');
session_register('productNumber');
$_SESSION['totalBasicPoints'];
$_SESSION['productNumber'];
$_SESSION['order'] = array();

    if (array_key_exists('_submit_check', $_POST)) {

$pose = $_POST['pose'];
$quantity = $_POST['quantity'];
$size = $_POST['size'];
$prodNum = $_POST['prodNum'];

$_SESSION['product'] = array(
                       "pose"=>$pose,
                       "quantity"=>$quantity,
                       "size"=>$size);           
                    
array_push($_SESSION['order'], $_SESSION[product]);
foreach ($_SESSION['order'] as $v){
    foreach ($v as $desc){
        print $_SESSION['order'] . ": " .$desc. "<br />";
 } print "<br /><br />";
 }
  

    }





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users