Custom form with addtocart function problem (no product id to send), please help, tq

Hi everyone,

I am building a shopping cart for a school exam. It is a custom mug order website. Basically, they want to let people custom design of the mug. I have set the order form like below, I am having some difficulty with the shopping cart. I took the basics of the shopping cart tutorial I found online. The only problem is that most shopping carts send an action and a product id through the URL and into the session for the shopping cart. Well, this custom order form have no id variable to send to cart, only the information will send. How would I code the addtocart.php to remenber all info using session, after yhat display the order detail in cart.php. I have tried manyday, but cannot get the entire thing to work. All help is appreciated. Below is my order form code:

http://www.mediafire.com/convkey/40aa/50qobdf5277cct8fg.jpg

<div class="custom_order">
<fieldset>
    <legend>Order Custom Mug here</legend>
        <br />
	<form id="custom_orderForm" name="custom_orderForm" method="post" action="custom_cart.php">
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Mug Moder</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Size</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Color</strong></td>
        <td align="center" width="25%" bgcolor="#C5DFFA"><strong>Message/Text will print at the mug</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Price</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Total</strong></td>
      </tr>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer" name="custom_mugModer">
              <option value="mug_moder_x">Mug Moder X</option>
              <option value="mug_moder_y">Mug Moder Y</option>
              <option value="mug_moder_z">Mug Moder Z</option>
            </select></td>
        <td align="center" width="15%"><label>
        	<select id="custom_mugSize" name="custom_mugSize">
              <option value="0.3l">0.3L</option>
              <option value="0.5l">0.5L</option>
              <option value="0.7l">0.7L</option>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor" name="custom_mugColor">
              <option value="red">Red</option>
              <option value="blue">Blue</option>
              <option value="while">While</option>
              <option value="black">Black</option>
              </select></td>
        <td align="center" width="25%"><label>
        	<input id="custom_mugMessage" name="custom_mugMessage" type="text" size="40" /></label></td>
        <td align="center" width="10%"><label>
        	<input id="custom_mugQty" name="custom_mugQty" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugPrice" name="custom_mugPrice" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugTotal" name="custom_mugTotal" type="text" size="6" /></label></td>
      </tr>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer" name="custom_mugModer">
              <option value="mug_moder_x">Mug Moder X</option>
              <option value="mug_moder_y">Mug Moder Y</option>
              <option value="mug_moder_z">Mug Moder Z</option>
            </select></td>
        <td align="center" width="15%"><label>
        	<select id="custom_mugSize" name="custom_mugSize">
              <option value="0.3l">0.3L</option>
              <option value="0.5l">0.5L</option>
              <option value="0.7l">0.7L</option>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor" name="custom_mugColor">
              <option value="red">Red</option>
              <option value="blue">Blue</option>
              <option value="while">While</option>
              <option value="black">Black</option>
              </select></td>
        <td align="center" width="25%"><label>
        	<input id="custom_mugMessage" name="custom_mugMessage" type="text" size="40" /></label></td>
        <td align="center" width="10%"><label>
        	<input id="custom_mugQty" name="custom_mugQty" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugPrice" name="custom_mugPrice" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugTotal" name="custom_mugTotal" type="text" size="6" /></label></td>
      </tr>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer" name="custom_mugModer">
              <option value="mug_moder_x">Mug Moder X</option>
              <option value="mug_moder_y">Mug Moder Y</option>
              <option value="mug_moder_z">Mug Moder Z</option>
            </select></td>
        <td align="center" width="15%"><label>
        	<select id="custom_mugSize" name="custom_mugSize">
              <option value="0.3l">0.3L</option>
              <option value="0.5l">0.5L</option>
              <option value="0.7l">0.7L</option>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor" name="custom_mugColor">
              <option value="red">Red</option>
              <option value="blue">Blue</option>
              <option value="while">While</option>
              <option value="black">Black</option>
              </select></td>
        <td align="center" width="25%"><label>
        	<input id="custom_mugMessage" name="custom_mugMessage" type="text" size="40" /></label></td>
        <td align="center" width="10%"><label>
        	<input id="custom_mugQty" name="custom_mugQty" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugPrice" name="custom_mugPrice" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugTotal" name="custom_mugTotal" type="text" size="6" /></label></td>
      </tr>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer" name="custom_mugModer">
              <option value="mug_moder_x">Mug Moder X</option>
              <option value="mug_moder_y">Mug Moder Y</option>
              <option value="mug_moder_z">Mug Moder Z</option>
            </select></td>
        <td align="center" width="15%"><label>
        	<select id="custom_mugSize" name="custom_mugSize">
              <option value="0.3l">0.3L</option>
              <option value="0.5l">0.5L</option>
              <option value="0.7l">0.7L</option>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor" name="custom_mugColor">
              <option value="red">Red</option>
              <option value="blue">Blue</option>
              <option value="while">While</option>
              <option value="black">Black</option>
              </select></td>
        <td align="center" width="25%"><label>
        	<input id="custom_mugMessage" name="custom_mugMessage" type="text" size="40" /></label></td>
        <td align="center" width="10%"><label>
        	<input id="custom_mugQty" name="custom_mugQty" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugPrice" name="custom_mugPrice" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugTotal" name="custom_mugTotal" type="text" size="6" /></label></td>
      </tr>
      <tr>
      	<td>Cart Grand Total:</td>
      </tr>
    </table>
    	<div style="text-align:center; margin-top:0.3em;">
    		<input id="custom_mugButton" name="custom_mugButton" type="submit" value="Add to Shopping Cart" />
        </div>
    </form>
</fieldset>
</div>

There are a few things to change before dealing with cart. When you are going to basically loop through form fields like this you will need to change the name of each input to an array by having after the name. For example

 name="custom_mugModer[]" 

Also anchor id’s or id’s in general need to be unique on the page and you are repeating the same id’s for each group. An easy fix is to add a number after the id name like so.

 id="custom_mugModer1"

Keep this number the same for all id’s in this group.

Also add a blank option for each of the selection fields so only those that have been selected can be processed.

<option value="">Select</option>

Once you have this done I would suggest changing the action on your form tag to go to the same page (at least for now while testing) and add the following php at the top.

<?php
session_start();

//As a test to see what is posted
echo "<pre>";
print_r($_POST); 
echo "</pre>";
?>

Submitting the form will now show an array of all posted fields which will look like this.

Array
(
    [custom_mugModer] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugSize] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugColor] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugMessage] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugQty] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugPrice] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugTotal] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [custom_mugButton] => Add to Shopping Cart
)

Each KEY in the array [0], [1], [2] and [3] represents a row in your html table or a group of form fields for each product with zero being the first item etc.

//// I need to go somewhere but will continue…

Also note there is no reason to manually write a group of fields over and over again. Just loop through or repeat this group of fields. If you define a variable stating the number of items to show at the top like this.

<?php
session_start();

//As a test to see what is posted
echo "<pre>";
print_r($_POST);
echo "</pre>";

///
$ItemsToShow = 4;
?>

You can then use a for() loop to get both the id numbers and display each group.

	  <?php
	  for($i=1;$i<=$ItemsToShow;$i++):
	  ?>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer<?php echo $i;?>" name="custom_mugModer[]">
              <option value="">Select</option>
              <option value="mug_moder_x">Mug Moder X</option>
              <option value="mug_moder_y">Mug Moder Y</option>
              <option value="mug_moder_z">Mug Moder Z</option>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugSize<?php echo $i;?>" name="custom_mugSize[]">
              <option value="">Select</option>
              <option value="0.3l">0.3L</option>
              <option value="0.5l">0.5L</option>
              <option value="0.7l">0.7L</option>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor<?php echo $i;?>" name="custom_mugColor[]">
              <option value="">Select</option>
              <option value="red">Red</option>
              <option value="blue">Blue</option>
              <option value="while">While</option>
              <option value="black">Black</option>
              </select></td>
        <td align="center" width="25%"><label>
        	<input id="custom_mugMessage<?php echo $i;?>" name="custom_mugMessage[]" type="text" size="40" /></label></td>
        <td align="center" width="10%"><label>
        	<input id="custom_mugQty<?php echo $i;?>" name="custom_mugQty[]" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugPrice<?php echo $i;?>" name="custom_mugPrice[]" type="text" size="6" /></label></td>
        <td  align="center" width="10%"><label>
        	$<input id="custom_mugTotal<?php echo $i;?>" name="custom_mugTotal[]" type="text" size="6" /></label></td>
      </tr>
	  <?php
	  endfor;
	  ?>

///To be continued…

Hi and thanks,

I have corrected the code and I learn alot.

Thanks again for your help, wait for your continued teaching.

Best regards
haozzi

Well I didn’t mean to go into full teacher mode, but glad to help. If you are now getting POST results like the array above we can now change POST to session. We start by wrapping our processing script in an IF condition using isset(). I’m using the button name as the key. We end the condition with endif;

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):

	endif;

Next within the condition we made we are setting our session cart key as an array.

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array 
		$_SESSION['cart'] = array();

	endif;;

It is much easier count the items in the cart and match items to the form rows if we use the same key we did in our for() loop in the form, e.g. 1,2,3,4 so we set a new variable in processing to be our next cart KEY.

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array 
		$_SESSION['cart'] = array();
		
		//Set an item number to variable
		$id = 1;

	endif;
?> 

We can now use a foreach() loop to get the item posted values and then close the loop with endforeach; As you recall from viewing the print array, names for an item have the same key and we will be using this key to identify each posted names VALUE. As ‘custom_mugModer’ is the first name we will us this to get the key we use for the other names.

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array 
		$_SESSION['cart'] = array();
		
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
		
		
		endforeach;

	endif;

We only want items that have been added to the cart so we will add an IF condition to check for !empty() then close this condition with endif;

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array 
		$_SESSION['cart'] = array();
		
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
			if(!empty($custom_mugModer)):
		
					
			endif;
		endforeach;

	endif;

Now that all the conditions are in place we can now add our items to session. We start with our KEY ‘cart’ then our $id KEY followed by the name = value; for example

$_SESSION['cart'][$id]['custom_mugModer']   = $custom_mugModer;

For the rest of the fields we use the POST name and KEY $k to get the value.

After we have added all fields to our array we increment the $id so the next item added will be 2, then 3 etc.

Adding another print($_SESSION[‘cart’]); we should now see our items that were saved to cart. The full processing script is here.

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array 
		$_SESSION['cart'] = array();
		
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
			if(!empty($custom_mugModer)):
				$_SESSION['cart'][$id]['custom_mugModer']   = $custom_mugModer;
				$_SESSION['cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
				$_SESSION['cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
				$_SESSION['cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
				$_SESSION['cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
				$_SESSION['cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
				$_SESSION['cart'][$id]['custom_mugTotal']   = $_POST['custom_mugTotal'][$k]; 
				
				//increment the $id
				$id++;
			endif;
		endforeach;
	 
		echo "<pre>";
		print_r($_SESSION['cart']); 
		echo "</pre>";
	endif;

And output would look like this.

Array
(
    [1] => Array
        (
            [custom_mugModer] => mug_moder_x
            [custom_mugSize] => 0.5l
            [custom_mugColor] => blue
            [custom_mugMessage] => I want blue
            [custom_mugQty] => 1
            [custom_mugPrice] => 
            [custom_mugTotal] => 
        )

)

I’m sure others will have different examples but this should get you started.

Thank alot, thank for your help.

if user want to order 6 type of Mug, submit 2 time of form, but our session can’t remember the previour order. how to fit this problem?
if user submit same moder, same size, same color, same message, then we only update the quantity.

Thank again

Best regards
haozzi

Yes, welcome to the world of programming.

As you learned earlier, you can loop through a group of form fields using for(), the same holds true for many aspects of your product display. Often a cart is built on records in a database. Based on the example here you might have the following tables:

  • products listing the product_id, product_name, description and price.
  • product_models listing the model_id, product_id, model_number and shown_model.
  • product_sizes listing the size_id, product_id, product_size and shown_size.
  • product_colors listing the color_id, product_id, product_color and shown_color.

It could be much more complicated. For example each size might have a different price but this is based on what we have.

Normally you would query the database and build an array listing all this information using the product_name as the primary KEY then model_number, model_size and model_color as secondary KEYs. A common way to manually build this array would be like this.

$items = array(
	'custom_mugModer' => array(
		'mug_Moder' => array(
			'mug_moder_x' => "Mug Moder X",
			'mug_moder_y' => "Mug Moder Y",
			'mug_moder_z' => "Mug Moder Z"		
		),
		'mug_Size' => array(
			'0.3l' => "0.3L",
			'0.5l' => "0.5L",
			'0.7l' => "0.7L"		
		),
		'mug_Color' => array(
			'red' => "Red",
			'blue' => "Blue",
			'white' => "White",
			'black' => "Black"		
		),
		'mug_Price' => "12.95"	
	)
);

However, when you make a database query, even though you have one product by the time you get all the models, colors and sizes, you might get 10 records back from your query. This is easily handled by putting these same product_name, models, colors and sizes as KEYs when builting the array in the query result loop. The format would be something like this, though this is a manual example.

$items = array(); 

$items['custom_mugModer']['mug_Moder']['mug_moder_x'] = "Mug Moder X";
$items['custom_mugModer']['mug_Moder']['mug_moder_y'] = "Mug Moder Y";
$items['custom_mugModer']['mug_Moder']['mug_moder_z'] = "Mug Moder Z";
$items['custom_mugModer']['mug_Size']['0.3l'] = "0.3L";
$items['custom_mugModer']['mug_Size']['0.5l'] = "0.5L";
$items['custom_mugModer']['mug_Size']['0.7l'] = "0.7L";
$items['custom_mugModer']['mug_Color']['red'] = "Red";
$items['custom_mugModer']['mug_Color']['blue'] = "Blue";
$items['custom_mugModer']['mug_Color']['white'] = "White";
$items['custom_mugModer']['mug_Color']['black'] = "Black";
$items['custom_mugModer']['mug_Price'] = "12.95";

Both of these examples are creating the same array so you can use either one but the last example is closer to how you would do it with a query result.

Now that we are working with an array we can make each of our selection boxes dynamically built. This is done with a foreach() statement. We’ll start with mug_Moder. This foreach statement is going to loop through the array that has the primary key of ‘custom_mugModer’ and secondary key of ‘mug_Moder’ and give you the “Moder key” and “Moder value”. We replace all our <option> lines within this loop like so.

			  <?php 
			  foreach($items['custom_mugModer']['mug_Moder'] as $mug_Moder_a => $mug_Moder_b):
				echo '<option value="' . $mug_Color_a . '">' . $mug_Color_b . '</option>'."\\r";
			  endforeach;
			  ?>

Now your question was regarding retaining information about what is in the cart. We do that by making the option SELECTED if that selection is found in the cart session. It’s a good practice to make sure each key is in the array before you check for a value. Will start by giving our “selected” a name

$selected_Moder = 

Then we check is we even have $_SESSION[‘cart’] using isset().
Remember how our increment $id was used to match the $i we used in our for() loop in the form? We now check if that array key exists and then we check if this session value matches the $mug_Color_a value. If we have a match we make the value of $selected_Moder = ’ selected=“selected”’ Otherwise the value is empty with ‘’. The line is long but looks like this

$selected_Moder = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugModer'] == $mug_Moder_a ? ' selected="selected"' : ''); 

We place this line within our foreach loop above the <option> and we echo the $selected_Moder within that tag. The complete select looks like this.

<select id="custom_mugModer<?php echo $i;?>" name="custom_mugModer[]">
              <option value="">Select</option>
			  <?php 
			  foreach($items['custom_mugModer']['mug_Moder'] as $mug_Moder_a => $mug_Moder_b):
			  	$selected_Moder = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugModer'] == $mug_Moder_a ? ' selected="selected"' : ''); 
				echo '<option value="' . $mug_Moder_a . '"' . $selected_Moder . '>' . $mug_Moder_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select>

As I’m not up to explaining in detail all the changes I’ve made on my copy I will just upload my sample copy in its entirety.

<?php
session_start();

//As a test to see what is posted
//echo "<pre>";
//print_r($_POST); 
//echo "</pre>";

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array 
		$_SESSION['cart'] = array();
		
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
			if(!empty($custom_mugModer)):
				$_SESSION['cart'][$id]['custom_mugModer']   = $custom_mugModer;
				$_SESSION['cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
				$_SESSION['cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
				$_SESSION['cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
				$_SESSION['cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
				$_SESSION['cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
				//Do the math Qty * Price
				$_SESSION['cart'][$id]['custom_mugTotal']   = $_POST['custom_mugQty'][$k]*$_POST['custom_mugPrice'][$k]; 
				//Grand total
				if(!array_key_exists('Grand_Total',$_SESSION['cart'])){ $_SESSION['cart']['Grand_Total'] = "";}																			  
				$_SESSION['cart']['Grand_Total']           += $_POST['custom_mugQty'][$k]*$_POST['custom_mugPrice'][$k]; 
				//increment the $id
				$id++;
			endif;
		endforeach;
	 
		//echo "<pre>";
		//print_r($_SESSION['cart']); 
		//echo "</pre>";
	endif;
///Set items to show to session
$ItemsToShow = 4;
/* 	   
$items = array(); 

$items['custom_mugModer']['mug_Moder']['mug_moder_x'] = "Mug Moder X";
$items['custom_mugModer']['mug_Moder']['mug_moder_y'] = "Mug Moder Y";
$items['custom_mugModer']['mug_Moder']['mug_moder_z'] = "Mug Moder Z";
$items['custom_mugModer']['mug_Size']['0.3l'] = "0.3L";
$items['custom_mugModer']['mug_Size']['0.5l'] = "0.5L";
$items['custom_mugModer']['mug_Size']['0.7l'] = "0.7L";
$items['custom_mugModer']['mug_Color']['red'] = "Red";
$items['custom_mugModer']['mug_Color']['blue'] = "Blue";
$items['custom_mugModer']['mug_Color']['white'] = "White";
$items['custom_mugModer']['mug_Color']['black'] = "Black";
$items['custom_mugModer']['mug_Price'] = "12.95";
*/

$items = array(
	'custom_mugModer' => array(
		'mug_Moder' => array(
			'mug_moder_x' => "Mug Moder X",
			'mug_moder_y' => "Mug Moder Y",
			'mug_moder_z' => "Mug Moder Z"		
		),
		'mug_Size' => array(
			'0.3l' => "0.3L",
			'0.5l' => "0.5L",
			'0.7l' => "0.7L"		
		),
		'mug_Color' => array(
			'red' => "Red",
			'blue' => "Blue",
			'white' => "White",
			'black' => "Black"		
		),
		'mug_Price' => "12.95"	
	)
);

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 7/2/2014 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>
</head>
<body>
<div class="custom_order">
<fieldset>
    <legend>Order Custom Mug here</legend>
        <br />
	<form id="custom_orderForm" name="custom_orderForm" method="post" action="">
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Mug Moder</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Size</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Color</strong></td>
        <td align="center" width="25%" bgcolor="#C5DFFA"><strong>Message/Text will print at the mug</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Price</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Total</strong></td>
      </tr>
	  <?php
	  for($i=1;$i<=$ItemsToShow;$i++):
	  ?>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer<?php echo $i;?>" name="custom_mugModer[]">
              <option value="">Select</option>
			  <?php 
			  foreach($items['custom_mugModer']['mug_Moder'] as $mug_Moder_a => $mug_Moder_b):
			  	$selected_Moder = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugModer'] == $mug_Moder_a ? ' selected="selected"' : ''); 
				echo '<option value="' . $mug_Moder_a . '"' . $selected_Moder . '>' . $mug_Moder_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugSize<?php echo $i;?>" name="custom_mugSize[]">
              <option value="">Select</option>
			  <?php 
			  foreach($items['custom_mugModer']['mug_Size'] as $mug_Size_a => $mug_Size_b): 
			  	$selected_Size = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugSize'] == $mug_Size_a ? ' selected="selected"' : ''); 
				echo '<option value="' . $mug_Size_a . '"' . $selected_Size . '>' . $mug_Size_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor<?php echo $i;?>" name="custom_mugColor[]">
              <option value="">Select</option>
			  <?php 
			  foreach($items['custom_mugModer']['mug_Color'] as $mug_Color_a => $mug_Color_b): 
			  	$selected_Color = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugColor'] == $mug_Color_a ? ' selected="selected"' : ''); 
				echo '<option value="' . $mug_Color_a . '"' . $selected_Color . '>' . $mug_Color_b . '</option>'."\\r";
			  endforeach;
			  ?>
              </select></td>
        <td align="center" width="25%"><label>
		
			<?php
			$mugMessage = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugMessage']}" : '');
			?>
			 
        	<input id="custom_mugMessage<?php echo $i;?>" name="custom_mugMessage[]" type="text" size="40" value="<?php echo $mugMessage;?>" /></label></td>
        <td align="center" width="10%"><label>
		
			<?php
			$mugQty = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugQty']}" : '');
			?>
			 
        	<input id="custom_mugQty<?php echo $i;?>" name="custom_mugQty[]" type="text" size="6" value="<?php echo $mugQty;?>" /></label></td>
        <td  align="center" width="10%"><label>
										  
			  <?php 
			  $mug_Price = $items['custom_mugModer']['mug_Price'];
			  ?>
			  
        	$<input id="custom_mugPrice<?php echo $i;?>" name="custom_mugPrice[]" type="hidden" size="6" value="<?php echo $mug_Price;?>" /></label><?php echo $mug_Price;?></td>
        <td  align="center" width="10%"><label>
			  					  
			  <?php 
			  $mug_Total = (isset($mugQty) && !empty($mugQty) ? number_format($mugQty*$mug_Price, 2, '.', '') : '0');
			  ?>
        	$<input id="custom_mugTotal<?php echo $i;?>" name="custom_mugTotal[]" type="hidden" size="6" value="<?php echo $mug_Total;?>" /></label><?php echo $mug_Total;?></td>
      </tr>
	  <?php
	  endfor;
	  ?>
	  			  
	  <?php 
	  $Grand_Total = (isset($_SESSION['cart']) && array_key_exists('Grand_Total',$_SESSION['cart']) ? number_format($_SESSION['cart']['Grand_Total'], 2, '.', '') : '0');
	  ?>
	  
      <tr>
      	<td colspan="7">Cart Grand Total: $<?php echo $Grand_Total;?></td>
      </tr>
    </table>
    	<div style="text-align:center; margin-top:0.3em;">
						  
			  <?php 
			  $button = (isset($_SESSION['cart']) ? "Update Shopping Cart" : "Add to Shopping Cart");
			  ?>
			  
    		<input id="custom_mugButton" name="custom_mugButton" type="submit" value="<?php echo $button;?>" />
        </div>
    </form>
</fieldset>
</div>

</body>
</html>

Thank a lot,

I get this code from you tube online tutorial and do some modification, I facing a problem, when user add the same moder of mug with same size, same color, same message, the code cannot auto add up the quantity.

this paragraph got problem? what should i change to made this paragraph to: IF same moder, same size, same color, same message, then add new quantity to old quantity,

		// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
		foreach ($_SESSION['Cart'] as $each_item) { 
		      $id++;
		      while (list($k, $value) = each($each_item)) {
				  if ($k == array_key_exists('custom_mugModer',$_SESSION['Cart']) && $value == $custom_mugModer) {
					  // That item is in cart already so let's adjust its quantity using array_splice()
					  array_splice($_SESSION['Cart'], $id-1, 1, array(array()));			  
						foreach($_POST['custom_mugModer'] as $k => $custom_mugModer){
							if(!empty($custom_mugModer)){
								$_SESSION['Cart'][$id]['custom_mugModer']   = $custom_mugModer;
								$_SESSION['Cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
								$_SESSION['Cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
								$_SESSION['Cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
								$_SESSION['Cart'][$id]['custom_mugQty']     = $each_item['custom_mugQty'] + $_POST['custom_mugQty'][$k];
								$_SESSION['Cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
								$_SESSION['Cart'][$id]['custom_mugTotal']   = $_POST['custom_mugTotal'][$k]; 
							}
						}
					  $wasFound = true;						  			  
				   } // close if condition
		      } // close while loop
	       } // close foreach loop

maybe this line

if ($k == array_key_exists('custom_mugModer',$_SESSION['Cart']) && $value == $custom_mugModer) {

below is full code

<?php //If our form button name is posted
if(isset($_POST['custom_mugButton'])){
$wasFound = false;
$id = 0;
	// If the cart session variable is not set or cart array is empty
	if (!isset($_SESSION['Cart']) || count($_SESSION['Cart']) < 1) { 
	// RUN IF THE CART IS EMPTY OR NOT SET
	$_SESSION['Cart'] = array(0 => array());
	//Set an item number to variable
	$id = 0;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer){
			if(!empty($custom_mugModer)){
				$_SESSION['Cart'][$id]['custom_mugModer']   = $custom_mugModer;
				$_SESSION['Cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
				$_SESSION['Cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
				$_SESSION['Cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
				$_SESSION['Cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
				$_SESSION['Cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
				$_SESSION['Cart'][$id]['custom_mugTotal']   = $_POST['custom_mugTotal'][$k]; 
					
				//increment the $id
				$id++;
			}
		}
	} else {
		// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
		foreach ($_SESSION['Cart'] as $each_item) { 
		      $id++;
		      while (list($k, $value) = each($each_item)) {
				  if ($k == array_key_exists('custom_mugModer',$_SESSION['Cart']) && $value == $custom_mugModer) {
					  // That item is in cart already so let's adjust its quantity using array_splice()
					  array_splice($_SESSION['Cart'], $id-1, 1, array(array()));			  
						foreach($_POST['custom_mugModer'] as $k => $custom_mugModer){
							if(!empty($custom_mugModer)){
								$_SESSION['Cart'][$id]['custom_mugModer']   = $custom_mugModer;
								$_SESSION['Cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
								$_SESSION['Cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
								$_SESSION['Cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
								$_SESSION['Cart'][$id]['custom_mugQty']     = $each_item['custom_mugQty'] + $_POST['custom_mugQty'][$k];
								$_SESSION['Cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
								$_SESSION['Cart'][$id]['custom_mugTotal']   = $_POST['custom_mugTotal'][$k]; 
							}
						}
					  $wasFound = true;						  			  
				   } // close if condition
		      } // close while loop
	       } // close foreach loop

		if ($wasFound == false) {
			array_push($_SESSION['Cart'], array());
			//Set an item number to variable
			foreach($_POST['custom_mugModer'] as $k => $custom_mugModer){
				if(!empty($custom_mugModer)){
					$_SESSION['Cart'][$id]['custom_mugModer']   = $custom_mugModer;
					$_SESSION['Cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
					$_SESSION['Cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
					$_SESSION['Cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
					$_SESSION['Cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
					$_SESSION['Cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
					$_SESSION['Cart'][$id]['custom_mugTotal']   = $_POST['custom_mugTotal'][$k]; 
							
					//increment the $id
					$id++;
				}
			}	   
		}
	}       
     
	echo "<pre>";
	print_r($_SESSION['Cart']); 
	echo "</pre>";

}  
?>

I played with posted code for a bit then decided to make my own. I start by setting cart sessions as before. Note, Grand_total removed from that part.

After that I run a double foreach and check for match on all required fields, model, size, color and message while also not including the original. If a match is found within this condition I save the original KEY in a “keep” array and all matches in a “delete” array.

Instead of attempting to update the quantity when a match is found we keep a running total just in case several items match. Once the secondary loop has run through all records I update the item session quantity and do the math for the total. The outside loop repeats for all items.

Once this is done we delete the duplicates. Then we recalculate the grand_total. Note: I renamed the key so it IS NOT part of cart as it was messing up the cart item count. I’ve updated the form to account for changes.

<?php
session_start();

//As a test to see what is posted
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array
		$_SESSION['cart'] = array();
				
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
			if(!empty($custom_mugModer)):
				$_SESSION['cart'][$id]['custom_mugModer']   = $custom_mugModer;
				$_SESSION['cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
				$_SESSION['cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
				$_SESSION['cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
				$_SESSION['cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
				$_SESSION['cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
				//Do the math Qty * Price
				$_SESSION['cart'][$id]['custom_mugTotal']   = $_POST['custom_mugQty'][$k]*$_POST['custom_mugPrice'][$k];
				//increment the $id
				$id++;
			endif;
		endforeach;
		
		//////////////////////////////////////////////////
		////////////// Look For Duplicates ///////////////
		//////////////////////////////////////////////////
		$keep = array();
		$delete = array();
		$cartCnt = count($_SESSION['cart']);	
		foreach ($_SESSION['cart'] as $key => $each_item):	
		
			//Get keeper Qty
			$Qty = $each_item['custom_mugQty'];					
			
			$inc =0;
			$addQty = "";	
			foreach ($_SESSION['cart'] as $key2 => $each_item2):
				$inc++;	
				if($each_item2['custom_mugModer'] == $each_item['custom_mugModer'] &&
				$each_item2['custom_mugSize'] == $each_item['custom_mugSize'] &&
				$each_item2['custom_mugColor'] == $each_item['custom_mugColor'] &&
				$each_item2['custom_mugMessage'] == $each_item['custom_mugMessage'] &&
				$key2 != $key):
				
					//Add keepers to $keep array	
					if(!in_array($key,$delete) && !in_array($key,$keep)):
						$keep[] = $key;
					endif;	
					
					//Add duplicates to $delete array
					if(!in_array($key2,$keep) && !in_array($key,$delete)):
						$delete[] = $key2;	
					endif;
					
					//Get duplicate Qty
					$Qty2 = $each_item2['custom_mugQty'];
					
					//Add duplicate Qty to running total
					$addQty += $Qty2;
					
				endif;	
				//If we've done a complete loop update the quantity and total
				if($inc == $cartCnt):	
					$NewQty = $Qty+$addQty;
					$_SESSION['cart'][$key]['custom_mugQty'] = $NewQty;
					$_SESSION['cart'][$key]['custom_mugTotal'] = $NewQty*$_SESSION['cart'][$key]['custom_mugPrice'];
				endif;				
			endforeach;			
		endforeach;
		
	 	//Delete duplicates
		foreach($delete as $d):
			unset($_SESSION['cart'][$d]['custom_mugModer']);
			unset($_SESSION['cart'][$d]['custom_mugSize']);
			unset($_SESSION['cart'][$d]['custom_mugColor']);
			unset($_SESSION['cart'][$d]['custom_mugMessage']);
			unset($_SESSION['cart'][$d]['custom_mugQty']);
			unset($_SESSION['cart'][$d]['custom_mugPrice']);
			unset($_SESSION['cart'][$d]['custom_mugTotal']);
			unset($_SESSION['cart'][$d]);
		endforeach;
		
	  	//Get new Grand_total
		$_SESSION['Grand_Total'] = "";					
		foreach ($_SESSION['cart'] as $key => $each_item):														
			$_SESSION['Grand_Total'] += $each_item['custom_mugTotal'];
		endforeach;			

		//echo "<pre>";
		//print_r($_SESSION['cart']);
		//echo "</pre>";
	endif;
	
/////////////////////////////////////////////////////////////	
//////////////////////////////////////////////////////////////
	
///Set items to show to session
$ItemsToShow = 4;
/* 	
$items = array();

$items['custom_mugModer']['mug_Moder']['mug_moder_x'] = "Mug Moder X";
$items['custom_mugModer']['mug_Moder']['mug_moder_y'] = "Mug Moder Y";
$items['custom_mugModer']['mug_Moder']['mug_moder_z'] = "Mug Moder Z";
$items['custom_mugModer']['mug_Size']['0.3l'] = "0.3L";
$items['custom_mugModer']['mug_Size']['0.5l'] = "0.5L";
$items['custom_mugModer']['mug_Size']['0.7l'] = "0.7L";
$items['custom_mugModer']['mug_Color']['red'] = "Red";
$items['custom_mugModer']['mug_Color']['blue'] = "Blue";
$items['custom_mugModer']['mug_Color']['white'] = "White";
$items['custom_mugModer']['mug_Color']['black'] = "Black";
$items['custom_mugModer']['mug_Price'] = "12.95";
*/

$items = array(
	'custom_mugModer' => array(
		'mug_Moder' => array(
			'mug_moder_x' => "Mug Moder X",
			'mug_moder_y' => "Mug Moder Y",
			'mug_moder_z' => "Mug Moder Z"		
		),
		'mug_Size' => array(
			'0.3l' => "0.3L",
			'0.5l' => "0.5L",
			'0.7l' => "0.7L"		
		),
		'mug_Color' => array(
			'red' => "Red",
			'blue' => "Blue",
			'white' => "White",
			'black' => "Black"		
		),
		'mug_Price' => "12.95"	
	)
);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 7/2/2014 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>
</head>
<body>
<div class="custom_order">
<fieldset>
    <legend>Order Custom Mug here</legend>
        <br />
	<form id="custom_orderForm" name="custom_orderForm" method="post" action="">
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Mug Moder</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Size</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Color</strong></td>
        <td align="center" width="25%" bgcolor="#C5DFFA"><strong>Message/Text will print at the mug</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Price</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Total</strong></td>
      </tr>
	  <?php
	  for($i=1;$i<=$ItemsToShow;$i++):
	  ?>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer<?php echo $i;?>" name="custom_mugModer[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Moder'] as $mug_Moder_a => $mug_Moder_b):
			  	$selected_Moder = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugModer'] == $mug_Moder_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Moder_a . '"' . $selected_Moder . '>' . $mug_Moder_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugSize<?php echo $i;?>" name="custom_mugSize[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Size'] as $mug_Size_a => $mug_Size_b):
			  	$selected_Size = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugSize'] == $mug_Size_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Size_a . '"' . $selected_Size . '>' . $mug_Size_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor<?php echo $i;?>" name="custom_mugColor[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Color'] as $mug_Color_a => $mug_Color_b):
			  	$selected_Color = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugColor'] == $mug_Color_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Color_a . '"' . $selected_Color . '>' . $mug_Color_b . '</option>'."\\r";
			  endforeach;
			  ?>
              </select></td>
        <td align="center" width="25%"><label>
		
			<?php
			$mugMessage = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugMessage']}" : '');
			?>
			
        	<input id="custom_mugMessage<?php echo $i;?>" name="custom_mugMessage[]" type="text" size="40" value="<?php echo $mugMessage;?>" /></label></td>
        <td align="center" width="10%"><label>
		
			<?php
			$mugQty = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugQty']}" : '');
			?>
			
        	<input id="custom_mugQty<?php echo $i;?>" name="custom_mugQty[]" type="text" size="6" value="<?php echo $mugQty;?>" /></label></td>
        <td  align="center" width="10%"><label>
										
			  <?php
			  $mug_Price = $items['custom_mugModer']['mug_Price'];
			  ?>
			
        	$<input id="custom_mugPrice<?php echo $i;?>" name="custom_mugPrice[]" type="hidden" size="6" value="<?php echo $mug_Price;?>" /></label><?php echo $mug_Price;?></td>
        <td  align="center" width="10%"><label>
			  					
			  <?php
			  $mug_Total = (isset($mugQty) && !empty($mugQty) ? number_format($mugQty*$mug_Price, 2, '.', '') : '0');
			  ?>
        	$<input id="custom_mugTotal<?php echo $i;?>" name="custom_mugTotal[]" type="hidden" size="6" value="<?php echo $mug_Total;?>" /></label><?php echo $mug_Total;?></td>
      </tr>
	  <?php
	  endfor;
	  ?>
	  			
	  <?php
	  $Grand_Total = (isset($_SESSION['Grand_Total']) && !empty($_SESSION['Grand_Total']) ? number_format($_SESSION['Grand_Total'], 2, '.', '') : '0');
	  ?>
	  <?php
	  $plural = (isset($cartCnt) && $cartCnt>1 ? "s" : '');
	  ?>
      <tr>
      	<td colspan="7">Cart Grand Total: $<?php echo $Grand_Total;?><span style="float:right;"><?php echo $cartCnt;?> Item<?php echo $plural;?></span></td>
      </tr>
    </table>
    	<div style="text-align:center; margin-top:0.3em;">
						
			  <?php
			  $button = (isset($_SESSION['cart']) ? "Update Shopping Cart" : "Add to Shopping Cart");
			  ?>
			
    		<input id="custom_mugButton" name="custom_mugButton" type="submit" value="<?php echo $button;?>" />
        </div>
    </form>
</fieldset>
</div>

</body>
</html>

Thank, thank, thank,

can we runing java script inside the form, exp: onkeyup=“restrict(???)” to check only number can be key in,

<input id=“custom_mugQty<?php echo $i;?>” name=“custom_mugQty” type=“text” onkeyup=“restrict(???)” size=“6” value=“<?php echo $mugQty;?>” />

<script>
    function restrict(elem){
        var tf = _(elem);
        var rx = new RegExp;
        if(elem == "???"){
            rx = /[^0-9]/gi;
        } 
        tf.value = tf.value.replace(rx, "");
    }
</script>

thank again

Best regards
haozzi

This should do it. Also updated $cartCnt section,line 254,255.

<?php
session_start();

//As a test to see what is posted
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array
		$_SESSION['cart'] = array();
				
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
			if(!empty($custom_mugModer)):
				$_SESSION['cart'][$id]['custom_mugModer']   = $custom_mugModer;
				$_SESSION['cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
				$_SESSION['cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
				$_SESSION['cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
				$_SESSION['cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
				$_SESSION['cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
				//Do the math Qty * Price
				$_SESSION['cart'][$id]['custom_mugTotal']   = $_POST['custom_mugQty'][$k]*$_POST['custom_mugPrice'][$k];
				//increment the $id
				$id++;
			endif;
		endforeach;
		
		//////////////////////////////////////////////////
		////////////// Look For Duplicates ///////////////
		//////////////////////////////////////////////////
		$keep = array();
		$delete = array();
		$cartCnt = count($_SESSION['cart']);	
		foreach ($_SESSION['cart'] as $key => $each_item):	
		
			//Get keeper Qty
			$Qty = $each_item['custom_mugQty'];					
			
			$inc =0;
			$addQty = "";	
			foreach ($_SESSION['cart'] as $key2 => $each_item2):
				$inc++;	
				if($each_item2['custom_mugModer'] == $each_item['custom_mugModer'] &&
				$each_item2['custom_mugSize'] == $each_item['custom_mugSize'] &&
				$each_item2['custom_mugColor'] == $each_item['custom_mugColor'] &&
				$each_item2['custom_mugMessage'] == $each_item['custom_mugMessage'] &&
				$key2 != $key):
				
					//Add keepers to $keep array	
					if(!in_array($key,$delete) && !in_array($key,$keep)):
						$keep[] = $key;
					endif;	
					
					//Add duplicates to $delete array
					if(!in_array($key2,$keep) && !in_array($key,$delete)):
						$delete[] = $key2;	
					endif;
					
					//Get duplicate Qty
					$Qty2 = $each_item2['custom_mugQty'];
					
					//Add duplicate Qty to running total
					$addQty += $Qty2;
					
				endif;	
				//If we've done a complete loop update the quantity and total
				if($inc == $cartCnt):	
					$NewQty = $Qty+$addQty;
					$_SESSION['cart'][$key]['custom_mugQty'] = $NewQty;
					$_SESSION['cart'][$key]['custom_mugTotal'] = $NewQty*$_SESSION['cart'][$key]['custom_mugPrice'];
				endif;				
			endforeach;			
		endforeach;
		
	 	//Delete duplicates
		foreach($delete as $d):
			unset($_SESSION['cart'][$d]['custom_mugModer']);
			unset($_SESSION['cart'][$d]['custom_mugSize']);
			unset($_SESSION['cart'][$d]['custom_mugColor']);
			unset($_SESSION['cart'][$d]['custom_mugMessage']);
			unset($_SESSION['cart'][$d]['custom_mugQty']);
			unset($_SESSION['cart'][$d]['custom_mugPrice']);
			unset($_SESSION['cart'][$d]['custom_mugTotal']);
			unset($_SESSION['cart'][$d]);
		endforeach;
		
	  	//Get new Grand_total
		$_SESSION['Grand_Total'] = "";					
		foreach ($_SESSION['cart'] as $key => $each_item):														
			$_SESSION['Grand_Total'] += $each_item['custom_mugTotal'];
		endforeach;			

		//echo "<pre>";
		//print_r($_SESSION['cart']);
		//echo "</pre>";
	endif;
	
/////////////////////////////////////////////////////////////	
//////////////////////////////////////////////////////////////
	
///Set items to show to session
$ItemsToShow = 4;
/* 	
$items = array();

$items['custom_mugModer']['mug_Moder']['mug_moder_x'] = "Mug Moder X";
$items['custom_mugModer']['mug_Moder']['mug_moder_y'] = "Mug Moder Y";
$items['custom_mugModer']['mug_Moder']['mug_moder_z'] = "Mug Moder Z";
$items['custom_mugModer']['mug_Size']['0.3l'] = "0.3L";
$items['custom_mugModer']['mug_Size']['0.5l'] = "0.5L";
$items['custom_mugModer']['mug_Size']['0.7l'] = "0.7L";
$items['custom_mugModer']['mug_Color']['red'] = "Red";
$items['custom_mugModer']['mug_Color']['blue'] = "Blue";
$items['custom_mugModer']['mug_Color']['white'] = "White";
$items['custom_mugModer']['mug_Color']['black'] = "Black";
$items['custom_mugModer']['mug_Price'] = "12.95";
*/

$items = array(
	'custom_mugModer' => array(
		'mug_Moder' => array(
			'mug_moder_x' => "Mug Moder X",
			'mug_moder_y' => "Mug Moder Y",
			'mug_moder_z' => "Mug Moder Z"		
		),
		'mug_Size' => array(
			'0.3l' => "0.3L",
			'0.5l' => "0.5L",
			'0.7l' => "0.7L"		
		),
		'mug_Color' => array(
			'red' => "Red",
			'blue' => "Blue",
			'white' => "White",
			'black' => "Black"		
		),
		'mug_Price' => "12.95"	
	)
);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 7/2/2014 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>

<script type="tex/javascript">
   <!--
   function isNumberKey(evt)
   {
      var charCode = (evt.which) ? evt.which : event.keyCode
      if (charCode > 31 && (charCode < 48 || charCode > 57))
         return false;

      return true;
   }
   //-->
</script>
</head>
<body>
<div class="custom_order">
<fieldset>
    <legend>Order Custom Mug here</legend>
        <br />
	<form id="custom_orderForm" name="custom_orderForm" method="post" action="">
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Mug Moder</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Size</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Color</strong></td>
        <td align="center" width="25%" bgcolor="#C5DFFA"><strong>Message/Text will print at the mug</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Price</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Total</strong></td>
      </tr>
	  <?php
	  for($i=1;$i<=$ItemsToShow;$i++):
	  ?>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer<?php echo $i;?>" name="custom_mugModer[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Moder'] as $mug_Moder_a => $mug_Moder_b):
			  	$selected_Moder = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugModer'] == $mug_Moder_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Moder_a . '"' . $selected_Moder . '>' . $mug_Moder_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugSize<?php echo $i;?>" name="custom_mugSize[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Size'] as $mug_Size_a => $mug_Size_b):
			  	$selected_Size = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugSize'] == $mug_Size_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Size_a . '"' . $selected_Size . '>' . $mug_Size_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor<?php echo $i;?>" name="custom_mugColor[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Color'] as $mug_Color_a => $mug_Color_b):
			  	$selected_Color = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugColor'] == $mug_Color_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Color_a . '"' . $selected_Color . '>' . $mug_Color_b . '</option>'."\\r";
			  endforeach;
			  ?>
              </select></td>
        <td align="center" width="25%"><label>
		
			<?php
			$mugMessage = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugMessage']}" : '');
			?>
			
        	<input id="custom_mugMessage<?php echo $i;?>" name="custom_mugMessage[]" type="text" size="40" value="<?php echo $mugMessage;?>" /></label></td>
        <td align="center" width="10%"><label>
		
			<?php
			$mugQty = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugQty']}" : '');
			?>
			
        	<input id="custom_mugQty<?php echo $i;?>" name="custom_mugQty[]" type="text" size="6" onkeypress="return isNumberKey(event)" value="<?php echo $mugQty;?>" /></label></td>
        <td  align="center" width="10%"><label>
										
			  <?php
			  $mug_Price = $items['custom_mugModer']['mug_Price'];
			  ?>
			
        	$<input id="custom_mugPrice<?php echo $i;?>" name="custom_mugPrice[]" type="hidden" size="6" value="<?php echo $mug_Price;?>" /></label><?php echo $mug_Price;?></td>
        <td  align="center" width="10%"><label>
			  					
			  <?php
			  $mug_Total = (isset($mugQty) && !empty($mugQty) ? number_format($mugQty*$mug_Price, 2, '.', '') : '0');
			  ?>
        	$<input id="custom_mugTotal<?php echo $i;?>" name="custom_mugTotal[]" type="hidden" size="6" value="<?php echo $mug_Total;?>" /></label><?php echo $mug_Total;?></td>
      </tr>
	  <?php
	  endfor;
	  ?>
	  			
	  <?php
	  $Grand_Total = (isset($_SESSION['Grand_Total']) && !empty($_SESSION['Grand_Total']) ? number_format($_SESSION['Grand_Total'], 2, '.', '') : '0');
	  ?>
	  <?php 	
	  $cartCnt = (isset($cartCnt) && $cartCnt>0 ? $cartCnt : 0);
	  $plural = (isset($cartCnt) && $cartCnt!=1 ? "s" : '');
	  ?>
      <tr>
      	<td colspan="7">Cart Grand Total: $<?php echo $Grand_Total;?><span style="float:right;"><?php echo $cartCnt;?> Item<?php echo $plural;?></span></td>
      </tr>
    </table>
    	<div style="text-align:center; margin-top:0.3em;">
						
			  <?php
			  $button = (isset($_SESSION['cart']) ? "Update Shopping Cart" : "Add to Shopping Cart");
			  ?>
			
    		<input id="custom_mugButton" name="custom_mugButton" type="submit" value="<?php echo $button;?>" />
        </div>
    </form>
</fieldset>
</div>

</body>
</html>

Thank, Thank, Thank

Is prosible, before user press the submit button, message pop up to say that this input only can content number.

Thank again,

Best regards
haozzi

Did you try posted code? It shouldn’t allow anything but a number so there’s no need for a pop-up.

Thank,

I try and I get this, as below

anythink wrong to my coding?

Thank again,

Best regards
haozzi

anythink wrong to my coding?

Don’t know. Don’t have your current code.
I just noticed a typo in my POSTed code.
I have

<script type="tex/javascript">

and it should be

<script type="text/javascript">

Yea! The form is working! Thank

Since our form already have this code " $ItemsToShow = 4; and <?php for($i=1;$i<=$ItemsToShow;$i++): ?> ", how to let user have addition option(drop down) button to select how many row they want? before start fill in the form.

Thank again,

Best regards
haozzi

Making a selection to POST number of items to show wouldn’t be hard. I made this version before reading your last post which has a “Add Another Item” link. Maybe this would do. Like I said, adding a small form to post this wouldn’t be hard to do if you want to go that route.

<?php
session_start();

//As a test to see what is posted
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";

	//If our form button name is posted
	if(isset($_POST['custom_mugButton'])):
	
		//Set our cart KEY 'cart' as an array
		$_SESSION['cart'] = array();
				
		//Set an item number to variable
		$id = 1;
		foreach($_POST['custom_mugModer'] as $k => $custom_mugModer):
			if(!empty($custom_mugModer)):
				$_SESSION['cart'][$id]['custom_mugModer']   = $custom_mugModer;
				$_SESSION['cart'][$id]['custom_mugSize']    = $_POST['custom_mugSize'][$k];
				$_SESSION['cart'][$id]['custom_mugColor']   = $_POST['custom_mugColor'][$k];
				$_SESSION['cart'][$id]['custom_mugMessage'] = $_POST['custom_mugMessage'][$k];
				$_SESSION['cart'][$id]['custom_mugQty']     = $_POST['custom_mugQty'][$k];
				$_SESSION['cart'][$id]['custom_mugPrice']   = $_POST['custom_mugPrice'][$k];
				//Do the math Qty * Price
				$_SESSION['cart'][$id]['custom_mugTotal']   = $_POST['custom_mugQty'][$k]*$_POST['custom_mugPrice'][$k];
				//increment the $id
				$id++;
			endif;
		endforeach;
		
		//////////////////////////////////////////////////
		////////////// Look For Duplicates ///////////////
		//////////////////////////////////////////////////
		$keep = array();
		$delete = array();
		$cartCnt = count($_SESSION['cart']);	
		foreach ($_SESSION['cart'] as $key => $each_item):	
		
			//Get keeper Qty
			$Qty = $each_item['custom_mugQty'];					
			
			$inc =0;
			$addQty = "";	
			foreach ($_SESSION['cart'] as $key2 => $each_item2):
				$inc++;	
				if($each_item2['custom_mugModer'] == $each_item['custom_mugModer'] &&
				$each_item2['custom_mugSize'] == $each_item['custom_mugSize'] &&
				$each_item2['custom_mugColor'] == $each_item['custom_mugColor'] &&
				$each_item2['custom_mugMessage'] == $each_item['custom_mugMessage'] &&
				$key2 != $key):
				
					//Add keepers to $keep array	
					if(!in_array($key,$delete) && !in_array($key,$keep)):
						$keep[] = $key;
					endif;	
					
					//Add duplicates to $delete array
					if(!in_array($key2,$keep) && !in_array($key,$delete)):
						$delete[] = $key2;	
					endif;
					
					//Get duplicate Qty
					$Qty2 = $each_item2['custom_mugQty'];
					
					//Add duplicate Qty to running total
					$addQty += $Qty2;
					
				endif;	
				//If we've done a complete loop update the quantity and total
				if($inc == $cartCnt):	
					$NewQty = $Qty+$addQty;
					$_SESSION['cart'][$key]['custom_mugQty'] = $NewQty;
					$_SESSION['cart'][$key]['custom_mugTotal'] = $NewQty*$_SESSION['cart'][$key]['custom_mugPrice'];
				endif;				
			endforeach;			
		endforeach;
		
	 	//Delete duplicates
		foreach($delete as $d):
			unset($_SESSION['cart'][$d]['custom_mugModer']);
			unset($_SESSION['cart'][$d]['custom_mugSize']);
			unset($_SESSION['cart'][$d]['custom_mugColor']);
			unset($_SESSION['cart'][$d]['custom_mugMessage']);
			unset($_SESSION['cart'][$d]['custom_mugQty']);
			unset($_SESSION['cart'][$d]['custom_mugPrice']);
			unset($_SESSION['cart'][$d]['custom_mugTotal']);
			unset($_SESSION['cart'][$d]);
		endforeach;
		
	  	//Get new Grand_total
		$_SESSION['Grand_Total'] = "";					
		foreach ($_SESSION['cart'] as $key => $each_item):														
			$_SESSION['Grand_Total'] += $each_item['custom_mugTotal'];
		endforeach;			

		//echo "<pre>";
		//print_r($_SESSION['cart']);
		//echo "</pre>";
	endif;
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//unset($_SESSION['ItemsToShow']);	
$default=1;
if(isset($_GET['mode']) && $_GET['mode'] == "AddItem"):
	if(isset($_SESSION['ItemsToShow'])){
		$_SESSION['ItemsToShow']++;
	}else{
		$_SESSION['ItemsToShow'] = $default+1;
	}
endif;	
///Set items to show to session
$ItemsToShow = (isset($_SESSION['ItemsToShow']) ? $_SESSION['ItemsToShow'] : $default);
$additemlink = '<a href="?mode=AddItem">Add Another Item</a>';
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
/* 	
$items = array();

$items['custom_mugModer']['mug_Moder']['mug_moder_x'] = "Mug Moder X";
$items['custom_mugModer']['mug_Moder']['mug_moder_y'] = "Mug Moder Y";
$items['custom_mugModer']['mug_Moder']['mug_moder_z'] = "Mug Moder Z";
$items['custom_mugModer']['mug_Size']['0.3l'] = "0.3L";
$items['custom_mugModer']['mug_Size']['0.5l'] = "0.5L";
$items['custom_mugModer']['mug_Size']['0.7l'] = "0.7L";
$items['custom_mugModer']['mug_Color']['red'] = "Red";
$items['custom_mugModer']['mug_Color']['blue'] = "Blue";
$items['custom_mugModer']['mug_Color']['white'] = "White";
$items['custom_mugModer']['mug_Color']['black'] = "Black";
$items['custom_mugModer']['mug_Price'] = "12.95";
*/

$items = array(
	'custom_mugModer' => array(
		'mug_Moder' => array(
			'mug_moder_x' => "Mug Moder X",
			'mug_moder_y' => "Mug Moder Y",
			'mug_moder_z' => "Mug Moder Z"		
		),
		'mug_Size' => array(
			'0.3l' => "0.3L",
			'0.5l' => "0.5L",
			'0.7l' => "0.7L"		
		),
		'mug_Color' => array(
			'red' => "Red",
			'blue' => "Blue",
			'white' => "White",
			'black' => "Black"		
		),
		'mug_Price' => "12.95"	
	)
);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 7/2/2014 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>

<script type="text/javascript">
   <!--
   function isNumberKey(evt)
   {
      var charCode = (evt.which) ? evt.which : event.keyCode
      if (charCode > 31 && (charCode < 48 || charCode > 57))
         return false;

      return true;
   }
   //-->
</script>
</head>
<body>
<div class="custom_order">
<fieldset>
    <legend>Order Custom Mug here</legend>
	<span style="float:right;"><?php echo $additemlink; ?></span>
        <br />
	<form id="custom_orderForm" name="custom_orderForm" method="post" action="">
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Mug Moder</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Size</strong></td>
        <td align="center" width="15%" bgcolor="#C5DFFA"><strong>Color</strong></td>
        <td align="center" width="25%" bgcolor="#C5DFFA"><strong>Message/Text will print at the mug</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Price</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Total</strong></td>
      </tr>
	  <?php
	  for($i=1;$i<=$ItemsToShow;$i++):
	  ?>
      <tr>
        <td align="center" width="15%">
        	<select id="custom_mugModer<?php echo $i;?>" name="custom_mugModer[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Moder'] as $mug_Moder_a => $mug_Moder_b):
			  	$selected_Moder = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugModer'] == $mug_Moder_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Moder_a . '"' . $selected_Moder . '>' . $mug_Moder_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugSize<?php echo $i;?>" name="custom_mugSize[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Size'] as $mug_Size_a => $mug_Size_b):
			  	$selected_Size = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugSize'] == $mug_Size_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Size_a . '"' . $selected_Size . '>' . $mug_Size_b . '</option>'."\\r";
			  endforeach;
			  ?>
            </select></td>
        <td align="center" width="15%">
        	<select id="custom_mugColor<?php echo $i;?>" name="custom_mugColor[]">
              <option value="">Select</option>
			  <?php
			  foreach($items['custom_mugModer']['mug_Color'] as $mug_Color_a => $mug_Color_b):
			  	$selected_Color = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) && $_SESSION['cart'][$i]['custom_mugColor'] == $mug_Color_a ? ' selected="selected"' : '');
				echo '<option value="' . $mug_Color_a . '"' . $selected_Color . '>' . $mug_Color_b . '</option>'."\\r";
			  endforeach;
			  ?>
              </select></td>
        <td align="center" width="25%"><label>
		
			<?php
			$mugMessage = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugMessage']}" : '');
			?>
			
        	<input id="custom_mugMessage<?php echo $i;?>" name="custom_mugMessage[]" type="text" size="40" value="<?php echo $mugMessage;?>" /></label></td>
        <td align="center" width="10%"><label>
		
			<?php
			$mugQty = (isset($_SESSION['cart']) && array_key_exists($i,$_SESSION['cart']) ? "{$_SESSION['cart'][$i]['custom_mugQty']}" : '');
			?>
			
        	<input id="custom_mugQty<?php echo $i;?>" name="custom_mugQty[]" type="text" size="6" onkeypress="return isNumberKey(event)" value="<?php echo $mugQty;?>" /></label></td>
        <td  align="center" width="10%"><label>
										
			  <?php
			  $mug_Price = $items['custom_mugModer']['mug_Price'];
			  ?>
			
        	$<input id="custom_mugPrice<?php echo $i;?>" name="custom_mugPrice[]" type="hidden" size="6" value="<?php echo $mug_Price;?>" /></label><?php echo $mug_Price;?></td>
        <td  align="center" width="10%"><label>
			  					
			  <?php
			  $mug_Total = (isset($mugQty) && !empty($mugQty) ? number_format($mugQty*$mug_Price, 2, '.', '') : '0');
			  ?>
        	$<input id="custom_mugTotal<?php echo $i;?>" name="custom_mugTotal[]" type="hidden" size="6" value="<?php echo $mug_Total;?>" /></label><?php echo $mug_Total;?></td>
      </tr>
	  <?php
	  endfor;
	  ?>
	  			
	  <?php
	  $Grand_Total = (isset($_SESSION['Grand_Total']) && !empty($_SESSION['Grand_Total']) ? number_format($_SESSION['Grand_Total'], 2, '.', '') : '0');
	  ?>
	  <?php 	
	  $cartCnt = (isset($cartCnt) && $cartCnt>0 ? $cartCnt : 0);
	  $plural = (isset($cartCnt) && $cartCnt!=1 ? "s" : '');
	  ?>
      <tr>
      	<td colspan="7">Cart Grand Total: $<?php echo $Grand_Total;?><span style="float:right;"><?php echo $cartCnt;?> Item<?php echo $plural;?></span></td>
      </tr>
    </table>
    	<div style="text-align:center; margin-top:0.3em;">
						
			  <?php
			  $button = (isset($_SESSION['cart']) ? "Update Shopping Cart" : "Add to Shopping Cart");
			  ?>
			
    		<input id="custom_mugButton" name="custom_mugButton" type="submit" value="<?php echo $button;?>" />
        </div>
    </form>
</fieldset>
</div>

</body>
</html>

Thank alot,

Every model of mug got difference price, how can we coding the form, when user select each mug model, the price will change according? after coding this our form look good, is time for us to checkout the order process, how to code that, please help.

Thank again,

Best regards
haozzi

So how goes your exam? How have you modified the array to list different pricing? What does your checkout look like?

You know if a person were to place both product model numbers and sizes as array keys for price, they would be able to list different pricing based on BOTH these selections. They could then use session cart values as keys to show pricing based on these selections.

$items = array(
	'custom_mugModer' => array(
		'mug_Moder' => array(
			'mug_moder_x' => "Mug Moder X",
			'mug_moder_y' => "Mug Moder Y",
			'mug_moder_z' => "Mug Moder Z"		
		),
		'mug_Size' => array(
			'0.3l' => "0.3L",
			'0.5l' => "0.5L",
			'0.7l' => "0.7L"		
		),
		'mug_Color' => array(
			'red' => "Red",
			'blue' => "Blue",
			'white' => "White",
			'black' => "Black"		
		),
		
		'mug_Price' => array(
		
			'mug_moder_x' => array(
				'0.3l' => "10.95",	
				'0.5l' => "11.95",	
				'0.7l' => "12.95"
				),	
			'mug_moder_y' => array(
				'0.3l' => "11.55",	
				'0.5l' => "12.55",	
				'0.7l' => "13.55"
				),	
			'mug_moder_z' =>  array(
				'0.3l' => "12.75",	
				'0.5l' => "13.75",	
				'0.7l' => "14.75"
				)
			)
		)
	);