Jump to content

Recommended Posts

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: £'.$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>£'.$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);

One part of your issue is:

 

$postage_query = 'SELECT postage FROM postage WHERE country="$country"';

 

Is not a valid SQL statement as it uses single quotes around it's values and not double.

 

$postage_query = "SELECT postage FROM postage WHERE country='$country'";

 

Should actually work.

 

As for the SUM portion, if you want it with the country together here is the proper way to do it:

$postage_query = "SELECT postage FROM postage WHERE country='$country' AND $sum BETWEEN start AND end";

 

EDIT:Reworking the above query / testing it as I am not sure that would work. Just tested it, and should work as long as it is really what you want.

 

Hope that helps.

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.