Jump to content

problem uploading items to my custom php cart properly


svgmx5

Recommended Posts

Like the title says it, i have a problem uploading items properly into a cart i've created using php.

 

Here's what i'm trying to accomplish...

 

I want to be able to add items to my cart, and when the cart is displayed, the quantity of items is arranged based on the sizes...so lets say someone purchases 4 of the same shirts, but in 2 different sizes..like 2 small and 2 large. I want it when the user adds it to the cart to add it that way instead of adding 2 of the same items only....and when displayed it displays as...

 

small red t-shirt x 2

large red t-shirt x 2

 

instead of red t-shirt x2

 

What i have so far..is the second example it displays them as one item x quantity...while it does display the size it only displays the first size chosen....

 

Anyway..i really hope i made sense..and here's the code that i'm using..I hope someone can point me to the right direction

 

add_to_cart.php


<?php

if(isset($_POST['add']) && $_POST['add']){

	$productID=intval($_GET['productID']);
	$productSize = $_POST['size'];

                //If the Item allready exists then just add 1 more 

	if(isset($_SESSION['cart'][$productID])){

		$_SESSION['cart'][$productID]['quantity']++;

	}else{
		//select and add to cart session
		$sql = "SELECT * FROM products WHERE productID={$productID}";
		$run = mysql_query($sql) or die (mysql_error());
		$num = mysql_num_rows($run);
		if($num!=0){
			while($fetch = mysql_fetch_array($run)){
				$productID = $fetch['productID'];
				$productPice = $fetch['productPrice'];

		}
                        //the contents of the cart in an array
		$_SESSION['cart'][$productID]=array(
				"quantity" => 1,
				"size" => $productSize,
				"price" => $productPice
			);
	}else{

		$message="This product id it's invalid!";

	}

}

}

?>

 

and here's the one that displays the cart contents

 


<?php
if(isset($_SESSION['cart']) && $_SESSION['cart']){

$sql = "SELECT * FROM products WHERE productID IN(" . implode(',', array_keys($_SESSION['cart'])) . ")";

$query = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($run);
$totalprice = 0;

while($row=mysql_fetch_array($query)){
$subtotal = $_SESSION['cart'][$row['productID']]['quantity']*$row['productPrice'];
$totalprice += $subtotal;
$i++;

//each item is displayed in a table column

?>

 

<td><?php echo $_SESSION['cart'][$row['productID']]['quantity']//displays the quantity?></td>

<td><?php echo $row['productName'] //displays the name of the item?></td>

<td><?php echo $_SESSION['cart'][$row['productID']]['size'] //displays the size selected?></td>

<td><?php echo $row['productPrice']//displays the price?></td>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.