Need final help to finish off PHP shopping cart script
Posted: Mon Jan 04, 2010 4:34 pm
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:
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:
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:
Code: Select all
$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: £'.$postage.'</h3>';
}I would really appreciate any help on this so much!!
This is some of the rest of the script, for reference:
Code: Select all
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>£'.$Price.'</td>';
$output[] = '<td>£'.(($Price) * $qty).'</td>';
$total += (($Price) * $qty);
// $output[] = '<td align="center">'.$qty.'</td>';
// $output[] = '<td align="center">£'.(($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: £'.$postage.'</h3>';
}
$output[] = '<h2 align="right">Total: £'.$total.'</h2>';
$output[] = '</form>';
} else {
$output[] = '<p>Your shopping basket is empty.</p>';
}
return join('',$output);