Jump to content

Recommended Posts

Hey,

 

I have a DB of products, and there is a field called postid

 

basically in the cart if you order up to 20 items with postid =1 then postage charge is £10, if you order more then in multiples of 20, it increases by £10

 

I have some code but it doesnt work...

 

if($postid == 1) {
$res = count($postid);
echo $res; } else { }

 

if there are 2 items in my cart with postid = 1 then $res should be 2 but currently it reads 11....

 

how can i get this to display correctly?

 

also, if I have 4 x ITEM1 (postid = 1) and 3 x ITEM2 (postid = 1) i need to get it to say oh, thats 7 items, not 2 items....

can this be done also?

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/
Share on other sites

this is the whole code on the page

 

function showCart() {
global $db;
$cart = $_SESSION['cart'];
$bus_name = $_REQUEST['bus_name'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}

	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table width="750" border="1" cellpadding="5" cellspacing="0" bordercolor="#000000"><tr>';
	$output[] = '<td width="90" bordercolor="#000000" bgcolor="#000000" class="tblheader"> </td>';
	$output[] = '<td bordercolor="#000000" bgcolor="#000000" class="tblheader">CODE</td>';
	$output[] = '<td bordercolor="#000000" bgcolor="#000000" class="tblheader">DESCRIPTION</td>';
	$output[] = '<td width="80" bordercolor="#000000" bgcolor="#000000" class="tblheader">PK SIZE</td>';
	$output[] = '<td width="80" bordercolor="#000000" bgcolor="#000000" class="tblheader">PRICE</td>';
	$output[] = '<td width="40" align="center" bordercolor="#000000" bgcolor="#000000" class="tblheader">QTY</td>';
	$output[] = '<td bordercolor="#000000" bgcolor="#000000" class="tblheader"> </td>';
	$output[] = '<td bordercolor="#000000" bgcolor="#000000" class="tblheader">Postage </td>';
	$output[] = '</tr>';
	foreach ($contents as $id=>$qty) {
		$sql = "SELECT * FROM products WHERE code = '".$id."'";
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);

	$output[] = '<td><a href="cart.php?action=delete&bus_name='.$bus_name.'&id='.urlencode($id).'">Remove</a></td>';
	$output[] = '<td class="main">'.$id.' </td>';
	$output[] = '<td class="main">'.$name.' </td>';
	$output[] = '<td class="main">'.$size.' </td>';
	$output[] = '<td class="main">£'.$price.' </td>';
	$output[] = '<td class="main" align="center" class="main"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
	$output[] = '<td>£'.sprintf('%04.2f',($price * $qty)).' </td>';
	$output[] = '<td>'.$postid.' </td>';

		if($postid == 1) {
			$res = count($postid);
			 echo $res;
		} 


		$total += $price * $qty;
		$output[] = '</tr>';





	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>£'.sprintf('%04.2f',$total).'</strong></p>';
//UPDATE CART
	$output[] = '<input name="bus_name" type="hidden" value="'.$bus_name.'">';
	$output[] = '<div><input type="submit" name="updateorder" value="Update Order"/></div>';
	$output[] = '</form>';
//SAVE ORDER
	$output[] = '<hr color=FF6600>';
	$output[] = 'To place your order, you need to have a Purchase Order. If you dont already have your purchase order then click on Save Order. This will enable you to save your order and return to it at a later date by clicking the saved orders linked on the main page after login.';
	$output[] = '<form action="save_order.php" method="post" name="save">';
	$output[] = '<input type="submit" name="saveorder" value="Save Order"/>';
	$output[] = '</form>';
//MAKE ORDER WITH PO NUMBER
	$output[] = '<hr color=FF6600>';
	$output[] = 'If you already have your purchase order number please enter it below and click place order.';
	$output[] = '<form action="order.php" method="post" name="order" id="order">';
	$output[] = 'Purchase Order Number: <input name="ponum" id="ponum" type="text" style="width: 120px;" ><br>';
	$output[] = '<input type="submit" name="placeorder" value="Place Order" onclick="return Check()"/>';
	$output[] = '</form>';

} else {
	$output[] = '<p>Your shopping cart is empty.</p>';
}
return join('',$output);
}

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794286
Share on other sites

It may be better to

 

if ($postid == 1) { $postid_count++; }

 

Then echo it outside of the loop.  I'm not sure what $postid is referring to, really, though.  If it is a category key or product key, then it is not counting the items ordered, but the key itself.  But, the above example should work.

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794323
Share on other sites

hey,

 

in the DB i have a field called postid - this is either 1, 2, 3 or 4...

 

what i then need to do is simply add up all the postid = 1 in a  count so i get 5 x number 1 7 x number 2 etc etc etc...

 

ive got this so far,

 

//CALCULATE POSTAGE
	if ($postid == 1) { 
	$postid_count++; 
	$idc = $postid_count + $qty;
	echo "Amount for " . $idc ;
	}

 

but its not working, as i have two items in my cart, i have

 

item1 x 7 (postid = 1)

item2 x 3 (postid = 1)

 

but when i count where postid = 1 the result echo'd is

 

Amount for 8

Amount for 5

 

obviously it should be 10....

 

any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794570
Share on other sites

wouldn't it be easier to do the count via MySQL ?

i assume grouping wouldn't be a problem!

 

<?php
$res = 0;
foreach ($contents as $id=>$qty)
{
$sql = "SELECT * , count(`postid`) as Counter FROM `products` WHERE code = '$id' GROUP BY `postid`";
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
if($postid == 1)
{
	$res += ($Counter*$qty); //Total Number of all items
	echo $Counter;
}


$total += $price * $qty;

///..........................SNIP.............

} //end of loop

echo "TOTAL: $res";
?>

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794655
Share on other sites

hey, hmmm, that doesnt seem to work, as all it says is 2, even if i select qty 10...

 

the cart is stored in an session array if it makes any difference?

 

maybe im just going about this all wrong, but basically I just need to find out the postid for the items in the cart and add them up so that i get 10x id1, 5x id2 etc etc, depending on what is selected....

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794953
Share on other sites

hang on, thats what we are doing wrong....

 

we are checking the database for the items....

 

SELECT * , count(`postid`) as Counter FROM `products` WHERE code = 'OC60X40B&B' GROUP BY `postid`

SELECT * , count(`postid`) as Counter FROM `products` WHERE code = 'OCA0MORTEMPTY' GROUP BY `postid`

 

I check the database purely to find out what the details are based on the order code thats ordered, but the qty is in the session, -

 

how can i query the cart for the items, not the DB

 

Yesideez, Thanks for that, I will need that, but not jsut yet, im still trying to get the actual qty of postid 1, then i can use it to say, up to 10 lots of post id 1 is £10 etc etc etc....

 

just need to find a way to get the actual details...

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794956
Share on other sites

Ok,

 

I have a shopping cart with DB of products.

 

on the product page, you select add to cart, and the itemid is added to the cart session...

on the cart page, it reads the itemsid in the cart session. it then connects to the DB and gets the relevant product details from the DB based on the itemid....

 

part of that database is a field - postid, there is a choice of 4 postid (1,2,3,4)....

 

what i need to know is how many items in my cart are postid=1

 

so i simply can echo "POST 1 " .$postid

 

I can echo it on each item result, but simply cant add them together for some reason....

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794961
Share on other sites

there is no table called cart, all the items "added to cart" are placed in a session called cart...

 

I can see what you've done and normally i could get that to work, however if i click add to cart on the same item 4 times, then i get:

$_SESSION['cart]' = OC60X40B&B, OC60X40B&B ,OC60X40B&B ,OC60X40B&B

 

on the cart page it opens the session['cart'] and qry's the DB so i can get the item name etc.... it also tells me that it is postid 1 - but as there are 4 items, it should tell me i have 4 items for postid 1....

 

thats the bit i cant get...

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794970
Share on other sites

I think I understand now - gotta be quick, start work very soon.

 

Instead of storing the cart in a session variable why not use temporary tables in the database?

 

Use a cookie on the user's machine to determine who's table belongs to who. If the cookie can't be created they can't have a cart  - I'm presuming they can have a cart without having to register first? If not identify the cart by their user ID.

 

That way you can store all the necessary data in a table plus the contents of the table can be archived so customers can look back on past orders.

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794972
Share on other sites

There probably is a way but I would have thought a temporary table for the cart would be the best way. That way if a person closes the browser their cart contents are still there. If you store it in a session variable it's lost.

 

A person would only need one cart at a time so they'd never need more than one cart table active.

 

When I say temporary table I don't mean one of these:

http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm

 

I'd create a normal table and just control it with CREATE and DROP.

 

Just a thought :)

Link to comment
https://forums.phpfreaks.com/topic/151206-simple-count-error/#findComment-794976
Share on other sites

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.