Need final help to finish off PHP shopping cart script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

Need final help to finish off PHP shopping cart script

Post by slaterino »

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:

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: &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:

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>&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);
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need final help to finish off PHP shopping cart script

Post by manohoo »

tiny detail, reverse the single/double quotes to make it like this:

Code: Select all

$postage_query = "SELECT postage FROM postage WHERE country='$country'";
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

Re: Need final help to finish off PHP shopping cart script

Post by slaterino »

Thanks for the tip! Almost sorted now!
Post Reply