Jump to content

Recommended Posts

I am trying to write a shopping cart script, that once they have confirmed their order it will insert into the database their order information.

 

Basically as a product can have a number of different quanitities that they wish to purchase, I want to create an associative array that has a product, linked to the quantity. I want to do this so that the order information stays in one row in the database, so in the 'product' row, it has values like (cow -> 2) cow being the product and 2 being the quantity.

 

Here is the code I am developing, and I have commented in the array which I am trying to create just the show my idea of thinking. any help, will be greatly appreciated!

function updateCart($product_id,$id){
if($_SESSION['cart']) {
	foreach($_SESSION['cart'] as $product_id => $quantity) {
		$total = 0;
		$sql = sprintf("SELECT title, price, shipping FROM product WHERE pkID = %d;",$product_id);
		$result = query($sql);
		if(numRows($result) > 0 ){
			$row = getResult($result);
			$price = $row['price'];
			$shipping = $row['ship'];
			$line_cost = ($price + $shipping) * $quantity;
			$total = $total + $line_cost;
			$product[] = $row['title'] -> $quantity; //unsure of how to code
			$sql2 = "INSERT INTO cart (userID, products, total, date) VALUES ('$id', '$product', '$total' '$date')";
			$query = query($sql2);
		}
	}
}else{
	$alert = 'No items in shopping cart.';
}
return $total;
}

 

I would put the items in the session as a multi-dem array, then you just add them to the cart from there.  No need to hit the items table.

 

 

//set the session.
$_SESSION['items'][$item][$price] = $quantity;

//now just send it to the database;
foreach($_SESSION['items'] as $product => $v) {
  foreach($v as $price => $quantity) {
  setlocale(LC_MONETARY,'en_US');
  $total = money_format("%i",$price * $quantity);
  $sql = "INSERT INTO cart (userID,products,total,date) VALUES ('$id','$product','$total','$date')";
}

 

Code is for demo purposes only, it is incomplete, and un-tested.

For such a complicated application that manages important information related to purchases, this is completely unsatisfactory.

 

You should have separate tables to manage this data and use join queries and construct single sql queries to insert based on loops to parse user input.

 

Your current method is hardly scalable and will be a pain to maintain as you develop and add to it.

 

From what I can tell, you want to store one column as $product[] = $row['titile'] -> $quantity. That is as simple as storing an array in the column with the relevant values but it isn't reliable. You're dealing with inventory and you have to account for that.

 

Say you need to check the order to ensure you have enough inventory, what do you do if all your quanties are stored within an array (but string) in a single column? You'd have to do a LIKE query or something. Bad idea.

 

You need to download an existing shopping cart script which has been professionally developed and look at how you can better that, rather than trying to reinvent the wheel.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.