Need final help to finish off PHP shopping cart script

Hi,
I have been trying all day to finish off a script and feel I am almost there. I started off with someone else’s script, which I didn’t understand completely, but have almost got it to where it needs to be. Maybe I should stop agreeing to do favours for people!

Basically there are two things left I need to do. The first is to work out the SELECT statement. I was playing around with this but couldn’t work it out completely! Basically I have a table ‘postage’ with this structure:

postage
id
country
start
end
postage

And I need my SELECT statement to select a value based on two variables, $country and $sum. $country simply needs to equate to the ‘country’ value in the table so this is no problem, but the other criteria is that $sum should be between ‘start’ and ‘end’. I don’t know how to write this as ‘SELECT postage WHERE $sum BETWEEN start AND end’ doesn’t seem to work.

The second part of my problem is because I don’t complete understand how the PHP works in this script and so seem to be having some problems actually generating the ‘postage’ value from my database. This is the script I have been trying:

		$postage_query = 'SELECT postage FROM postage WHERE country="$country"';
		$result = mysql_query($postage_query) or die(mysql_error());
		while(list($postage)= mysql_fetch_row($result))
{
		$output[] = '<h3 align="right">Postage: &pound;'.$postage.'</h3>';
}

But nothing is being returned. There are no errors with this part of the script and I can output the $sum and $country values so these are definitely there. What is the best way for me to pull out this value?

I would really appreciate any help on this so much!!

This is some of the rest of the script, for reference:

	
global $db;
	$country = $_SESSION['country'];
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<form action="basket.php?action=update" method="post" id="cart">';
		$output[] = '<table width="600" align="center">';
		$output[] = '<tr><th>Item</th><th>Item Price</th><th>Quantity</th><th>Subtotal</th></tr>';
		foreach ($contents as $id=>$qty) {
			$sql = 'SELECT * FROM products WHERE Product_ID = '.$id;
			$result = $db->query($sql);
			$row = $result->fetch();
			extract($row);
			$output[] = '<tr>';
			$output[] = '<td align="center">'.$Common_Name.' ('.$Genus.')</td>';
	 		$output[] = '<td>&pound;'.$Price.'</td>';
			$output[] = '<td>&pound;'.(($Price) * $qty).'</td>';
			$total += (($Price) * $qty);
//			$output[] = '<td align="center">'.$qty.'</td>';
//			$output[] = '<td align="center">&pound;'.(($Price + $postage)).'</td>';

//			$total += ($Price + $postage);
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$sum = array_sum($contents);
		$postage_query = 'SELECT postage FROM postage WHERE country="$country"';
		$result = mysql_query($postage_query) or die(mysql_error());
		while(list($postage)= mysql_fetch_row($result))
{
		$output[] = '<h3 align="right">Postage: &pound;'.$postage.'</h3>';
}
		$output[] = '<h2 align="right">Total: &pound;'.$total.'</h2>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>Your shopping basket is empty.</p>';
	}
	return join('',$output);

Is what you are looking for, this?


SELECT postage FROM postage WHERE country="$country" AND (postage.start > $start_variable AND postage.end < $end_variable)

The brackets make the condition inside equate to true.

Hope that helps

Keith

Yes!
That is exactly what I need!!!

If you have any idea of how the rest works that would be mightily appreciated! I think I have to use ‘explode’ as part of my function but as I said I’m a little confused by the whole situation!

Thanks!

This link worths reading:
http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html