phpretard Posted April 18, 2008 Share Posted April 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 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 ) ); ?> Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 That doesn't show anything using this: echo '<pre>$_SESSION:' . print_r($_SESSION,true) . '</pre>'; Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 If you try to order a photo it would help explain the problem. http://www.rememberthetimephotos.com/index.php?page=forms/order&picture=ALBUMS/FLM-MINC/P1090702.jpg&BM=P1090702.jpg Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 Did you call 'session_start()' at the begining of your program? Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 Yes Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 <?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? Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 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] Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 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'> Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 Is this what you are wanting? <?php $picture_id = "P1040054.JPG"; echo "<img src=" . {$_SESSION[$picture_id]['picture']} . ">"; // -OR- echo "<img src=" . {$_SESSION['P1040054.JPG']['picture']} . ">"; ?> Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 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'>"; Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 Is this what you were thinking of? I don't understand exactly what you mean. <?php foreach ($_SESSION as $file => $val) { echo "Image for file '" . $file . "'<img src='" . $val['picture'] . "'>"; } ?> Quote Link to comment Share on other sites More sharing options...
phpretard Posted April 18, 2008 Author Share Posted April 18, 2008 I just want to pull the information from the session --- $_SESSION['picture'] --- and use it for the source of an image. Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.