Re: Forms submitting to a session array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thomas wrote:
I'm trying to create a shopping cart where users can visit a product page (called from a MySQL database depending on the url ending in ?product_id=#). On the product page users can select options from dropdown lists (such as color, etc.) These dropdown lists are also dynamically generated from the database.

I need to store what the user has selected while the user browses the site. I want to also display what they've selected on a "cart" page.

I believe my best option is to store what they've selected in a session array. I'm not sure how to do this with the dropdown lists, etc.

In exactly the same way as any other data. If you want to save it per-product, you could do something like this.

<?php
session_start();

if (empty($_SESSION['products'])) {
	$_SESSION['products'] = array();
}

// appropriate checking for form submission here.
// and also making sure proper colors or whatever are posted.
// ....

$session_product = array();
$session_product['product_id'] = $_POST['product_id'];
$session_product['color'] = $_POST['color'];

// add the new array of info to the session.
array_push($_SESSION['products'], $session_product);


now if you print_r($_SESSION['products']) you'll see a big array of info with each product having a color.


--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux