Need help with a simple shopping cart-like php script

Discussion in 'PHP' started by pitto, May 6, 2010.

  1. #1
    Hi,

    I'm trying to put together a simple script that acts a little like a shopping cart. Essentially OrderCart.php will create a form with a table of different vegetables, their price and discription as well as a text input box so that a customer can input the number of say, beets, they want. All of the vegetables are drawn from a mysql database and the table-constructing code looks like this:

    
    if (!isset ($_POST['submitted'])) {
    	require_once ('../mysql_connect.php');//make a list of vegetables
    	$query = "SELECT vegetable_id AS vid, veg_name AS veg, price AS pr, description AS d FROM vegetables WHERE available='Y' ORDER BY veg ASC";
    	$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br 	/>MySQL Error: " . mysqli_error($dbc));
    	
    	// IF at least one vegetable is available in the database
    	if ($result) {
    		echo "\n\t\t<table>\n\t
    			<tr>\n\t\t
    				<th>Qty</th>\n\t\t
    				<th>Product</th>\n\t\t
    				<th>Price</th>\n\t\t
    				<th>Description</th>\n\t\t
    			</tr>\n\t
    			<form id=\"ordercart\" action=\"OrderCart.php\" method=\"post\">\n\t";
    		// List all available vegetables in columns.
    		while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
    			echo "\t\t<tr>\n\t
    					<input type=\"hidden\" name=\"vid[]\" value=\"{$row['vid']}\" />\n\t
    					<td><input type\"text\" name=\"qty[]\" value=\"0\" size=\"2\" maxlength=\"3\" /></td>\n\t
    					<td>{$row['veg']}</td>\n\t
    					<td>{$row['pr']}</td>\n\t
    					<td>{$row['d']}</td>\n\t
    				</tr>\n";
    		}
    		mysqli_free_result ($result);
    		mysqli_close ($dbc);
    		echo "\t<tr class=\"button\">\n\t
    				<td><input name=\"view_order\" id=\"view_order\" type=\"submit\" value=\"Preview Order\" /></td>\n
                            <td><a href=\"OrderSubmission.php\">Submit My Order</a></td>
    			</tr>\n
    			<input type=\"hidden\" name=\"submitted\" value=\"TRUE\" />\n
    			</form>\n
    			</table>";
    				
    	// ELSE no vegetables are available for sale.
    	} else {
    		echo '<p>No vegetables are available right now</a>.</p> ';
    	} // END IF vegetables are available.
    PHP:
    Now, what I want to do is post the vegetable id number (which is my primary key in the vegetables table of my database) and it's corresponding quantity to the same page, OrderCart.php so that it can store these corresponding values in $_SESSION['cart']. I used the hidden input named vid[] to send the vegetable id and the text input named qty[] to send it's corresponding quantity. My trouble is coming from dealing with the arrays vid[] and qty[]. Most of the examples of shopping carts online only have one item per page so you just post single values to $_SESSION['cart'] whereas I'm posting two arrays and its giving me a hell of a time. If $_POST['vid'] looks like:

    array ( array (0 => vid_0, 1 => vid_1, ...) ) where vid_i is the id number of the ith vegetable in the table​

    and $_POST['qty'] looks like:

    array (array (0 => qty_0, 1=> qty_1, ...) ) where qty_i is the quantity of the ith vegetable that the customer is ordering​

    How do I map these two subarrays together so that, within my session, the multidimensional array $_SESSION['cart'] looks like:

    array ( array ( vid_0 => qty_0, vid_1 => qty_1, ...) )​

    Is this possible? If so, is it advisable? Essentially, I need to create this array so that it saves the customer's cart within the session, re-displays it to them with a total cost so that they can readjust if they like, or submit it for processing. No payment will ever happen online it's just a way of me keeping track of their orders.

    Any help would be greatly appreciated.
     
    pitto, May 6, 2010 IP