Jump to content

Multiple Session Vars


phpretard

Recommended Posts

I need to create a new session each time a form is submitted.

 

Kind of a shopping cart with no database...

 

///////////THE EXISTING CODE
<?php

if(isset($_POST['order_photo'])){

  foreach($_POST as $key => $val){
   $_SESSION[$key] = $val;
  }
}

///////////CURRENTLY OUTPUTS

$_SESSION:Array
(
    [picture] => ALBUMS/Holmes_Adams/P1040050.jpg
    [file] => P1040050.jpg
    [s46] => 4 x 6
    [QU46] => 10
    [s57] => 5 x 7
    [QU57] => 20
    [s810] => 8 x 10
    [QU810] => 30
    [order_photo] => Order This Photo
)

?>

/////////////I NEED NEW CODE TO OUTPUT THIS

$_SESSION:Array1
(
    [picture] => ALBUMS/Holmes_Adams/P1040050.jpg
    [file] => P1040050.jpg
    [s46] => 4 x 6
    [QU46] => 10
    [s57] => 5 x 7
    [QU57] => 20
    [s810] => 8 x 10
    [QU810] => 30
    [order_photo] => Order This Photo
)

$_SESSION:Array2
(
    [picture] => ALBUMS/Holmes_Adams/DIFFERENT NUMBER.jpg
    [file] => DIFFERENT NUMBER.jpg
    [s46] => 4 x 6
    [QU46] => DIFFERENT QUANTITY
    [s57] => 5 x 7
    [QU57] => DIFFERENT QUANTITY
    [s810] => 8 x 10
    [QU810] => DIFFERENT QUANTITY
    [order_photo] => Order This Photo
)

and so on....

 

All this comes from the same form with different values.

Link to comment
Share on other sites

Here is how I would  handle that:

<?php

if(isset($_POST['order_photo']))
{
  $temp_array = array();
  foreach($_POST as $key => $val){
   $temp_array[$key] = $val;
  }
  // This empty brackets ('[]') append the '$temp_array' to the $_SESSION variable
  $_SESSION[] = $temp_array;
}

// Now if you were to view the contents of the $_SESSION var, you would see something like this

$_SESSION => array(
1 => array (
    [picture] => ALBUMS/Holmes_Adams/P1040050.jpg
    [file] => P1040050.jpg
    [s46] => 4 x 6
    [QU46] => 10
    [s57] => 5 x 7
    [QU57] => 20
    [s810] => 8 x 10
    [QU810] => 30
    [order_photo] => Order This Photo
   ),
2 => array(
    [picture] => ALBUMS/Holmes_Adams/DIFFERENT NUMBER.jpg
    [file] => DIFFERENT NUMBER.jpg
    [s46] => 4 x 6
    [QU46] => DIFFERENT QUANTITY
    [s57] => 5 x 7
    [QU57] => DIFFERENT QUANTITY
    [s810] => 8 x 10
    [QU810] => DIFFERENT QUANTITY
    [order_photo] => Order This Photo
)
);
?>

Link to comment
Share on other sites

 

<?php
// This code should at least print out an empty array like this
$_SESSION => array{
  [0]=>
  array(0) {
  }
}

  $temp_array = array();
  foreach($_POST as $key => $val)
  {
   $temp_array[$key] = $val;
  }

  // This empty brackets ('[]') append the '$temp_array' to the $_SESSION variable
  $_SESSION[] = $temp_array;

var_dump($_SESSION);
?>

 

When I read you original post I thought that you were wanting to store multiple form results in the $_SESSION array. Is this correct?

 

 

 

Link to comment
Share on other sites

A user goes to the site >> they see a photo they want >> they click on it >> Now they are looking at a form with the picture they chose and size options.  They select the size and quantity and submit. >>> now they are redirected to a page that display there order details.

 

 

Not haveing left the site yet... they see another photo they like >>> they click on it >>> and it takes them to the same form with a different picture(picture# is the unique ID - I would like to name and store the session array by that number). When they submit the new picture it forwards them to the same details page and displays the content of the 2 session vars.

 

and again and again.

 

My problem.

 

On the details page it displays a picture for each ITEM in the array...not each array.

 

If I order a new photo...it replaces the old instead of showing both.

 

 

 

 

 

 

Link to comment
Share on other sites

Try this:

<?php
  $picture_id = "your unique picture id for this picture";
  foreach($_POST as $key => $val){
   $_SESSION[$picture_id][$key] = $val;
  }
?>
[code]

This will create an array of picture ids, and each picture id will contain an array of the form data.

[/code]

Link to comment
Share on other sites

That works.

 

   

[P1040054.jpg] => Array
        (
            [picture] => ALBUMS/Holmes_Adams/P1040054.jpg <-----//////////////////////THAT
            [file] => P1040054.jpg
            [s46] => 4 x 6
            [QU46] => 30
            [s57] => 5 x 7
            [QU57] => 20
            [s810] => 8 x 10
            [QU810] => 10
            [order_photo] => Order This Photo
        )

 

What if I wanted to echo <img src='THAT'>

Link to comment
Share on other sites

I should be more specific...

 

I need to pull the info from the fantastic array you just gave me.

 

[P1040054.jpg] => Array
        (
            [picture] => ALBUMS/Holmes_Adams/P1040054.jpg <-----//////////////////////THAT
            [file] => P1040054.jpg
            [s46] => 4 x 6
            [QU46] => 30
            [s57] => 5 x 7
            [QU57] => 20
            [s810] => 8 x 10
            [QU810] => 10
            [order_photo] => Order This Photo
        )


foreach ($_SESSION as $file => $val){

echo"<img src='THAT'>";

Link to comment
Share on other sites

what do you mean by 'pull information from the session'?

How do you normally access the session variables? Just do what you normally do, but insert this '[$picture_id]' right after the '$_SESSION' variable.

 

Do you understand multidimensional arrays?

 

Down near the bottom of this article it describes briefly describes multidimensional arrays: http://us2.php.net/types.array

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.