For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > January 2008 > Re: [PHP-DB] Forms submitting to a session array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Forms submitting to a session array
Chris

2008-01-13, 7:00 pm

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/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com