Complete novice looking for help...

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
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Complete novice looking for help...

Post by Christian1977 »

Hi...

I am currently helping a friend to try and build a site that has a shopping basket which will allow customers to then go and pay with paypal..

The problem I am having is sending the info through to paypal.
I have taken some really nice code offered by a chap on his site called the watchmaker project which was really easy to follow and amend as necessary. It uses a session to store the basket info.
The problem I am now having is using the html form offered by paypal to send the individual item details through to them.
I have created a new function to do this as my level of understanding requires and I have nearly got it working... the problem here in the code underneath is that there is a foreach statement which is causing the loop to loop through a number of times for each item in the basket. Therefore I now have two instances of each item if I have two items in the basket, three instances of each item if there are three items in the basket and so on and so forth.
I have tried to remove the foreach statement but this produces all sorts of errors..
Any help on removing this would be greatly appreciated.. I am a complete novice and apologise for the lack of understanding on my part. I have certainly leapt straight in and this has obviously caused me problems.

thanks in advance
Chris

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

echo "<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>";
echo "<input type='text' name='cmd' value='_cart'/>";
echo "<input type='text' name='upload' value='1'/>";
echo "<input type='text' name='business' value='katie.photoangel@googlemail.com'/>";
for($i=1; $i <= count($contents); $i++)
{
echo $i;
foreach ($contents as $id=>$qty)
{
$sql = 'SELECT * FROM products1 WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);

echo "<input type='text' name='item_name_$i' value='".$sweet."'/>";
echo "<input type='text' name='amount_$i' value='".$price."'/>";
}
}
}
echo "<input type='submit' value='Pay securely with Paypal'/>";
echo "</form>";
}
?>
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Re: Complete novice looking for help...

Post by Christian1977 »

Hi, Can anyone help or tell me where I am going wrong with my post?

thanks
Chris
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Complete novice looking for help...

Post by califdon »

What is the purpose of this line?

Code: Select all

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
 
It seems to be incrementing the quantity ordered for every item (even assuming that $item is numeric).
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Re: Complete novice looking for help...

Post by Christian1977 »

califdon wrote:What is the purpose of this line?

Code: Select all

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
 
It seems to be incrementing the quantity ordered for every item (even assuming that $item is numeric).
Hi,

thanks very much for responding. Muchly appreciated.
I think what it does is take every instance of an $item and increments it.. so if there are three instances of a product within the array from clicking on the add to cart button for the same product three separate times there will only be one recorded??
This seems to be what the author is suggesting in this article which is where I got the code..
http://www.thewatchmakerproject.com/jou ... pping-cart
thanks
Chris
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Complete novice looking for help...

Post by califdon »

Therefore I now have two instances of each item if I have two items in the basket, three instances of each item if there are three items in the basket and so on and so forth.
I have tried to remove the foreach statement but this produces all sorts of errors..
What I'm not clear on is what you mean by having 2 instances of each item. You're right, the foreach loop serves the purpose of accumulating possibly duplicate product requests into a single line item with a quantity. There are two arrays, $items (the product IDs or names) and $contents (the quantity of each item in the $items array). Frankly, I found the author's programming style a bit difficult to follow, and I don't have time to try to thoroughly analyze the entire script, so I'm not sure how much help I can give you. But from the many comments other users have added to his website, I'm assuming that it works, so can you be more specific about what isn't working for you?
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Re: Complete novice looking for help...

Post by Christian1977 »

Yep sure,

Basically in order to send over the items in the basket individually so that they are listed you need to send the information via the html form. As part of the form each item should be sent with the name of item_name_x (with x being a number that increments for every individual product that you want listing.) I managed to an extent to get this working with the for command and give the item_name_ a variable of $i that increments on each loop.
Unfortunately there seems to be another loop working (the foreach ($contents as $id=>$qty) I'm guessing that seems to be looping this whole process.) Therefore if my basket contains toffees and mints then I get two lots of toffees and two lots of mints. This seems to be because the unwanted loop is going through the basket checking how many items there are in there and looping the loop that I want. (hope that makes sense but it's probably as clear as mud!!) If there are three items then I get three lots of the individual items showing up.
When I try and remove the foreach loop the SQL query becomes invalid or so it would seem.
A few of the errors are:
Notice: Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 SQL: SELECT * FROM products1 WHERE id = in C:\www\vhosts\localhost\sweets\inc\mysql.class.php on line 109

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\www\vhosts\localhost\sweets\inc\mysql.class.php on line 151

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\www\vhosts\localhost\sweets\inc\mysql.class.php on line 167

Warning: extract() [function.extract]: First argument should be an array in C:\www\vhosts\localhost\sweets\inc\functions.inc.php on line 133
I will post the code below using the code feature as this seems to number the lines..
I've tried to contact the guy direct. He helped me once initially but I'm guessing he's getting a bit tired of me now ;o)
Cheers
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Re: Complete novice looking for help...

Post by Christian1977 »

Code: Select all

<?php
 
function writeShoppingCart() 
{
$cart = (isset($_SESSION['cart'])) ? $_SESSION['cart'] : '';
    if (!$cart)
    {
    return '<p class="body">You have: no items in your shopping cart</p>';
    }
    else
    {
    // Parse the cart session variable
    $items = explode(',',$cart);
    $s = (count($items) > 1) ? 's':'';
    return '<p class="body">You currently have a total of :&nbsp;<a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
    }
}
 
function showCart() 
    {
    global $db;
    $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="cart.php?action=update" method="post" id="cart">';   
    
    $total = 0;
    $total_w = 0;
    $postcost = 0;
    $output[] = '<table class=cart>';
    foreach ($contents as $id=>$qty) 
        {
        $sql = 'SELECT * FROM products1 WHERE id = '.$id;
        $result = $db->query($sql);
        $row = $result->fetch();
        
        extract($row);
        $output[] = '<tr>';
        $output[] = '<td class=contents><p class="tablebody"><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></p></td>';
        $output[] = '<td><p class="body">'.$sweet.'</p></td>';
        $output[] = '<td><p class="tablebody">&pound;'.$price.'&nbsp;'.$unit.'</p></td>';
        $output[] = '<td><p class="tablebody"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></p></td>';
        $output[] = '<td><p class="tablebody"><b>&pound;'.sprintf("%1.2f", $price * $qty).' </b></p></td>';
        
        $total += $price * $qty;
        $total_w += $weight * $qty;
        if ($total_w < 1000)
        {$totalweight = $total_w;}
         else 
        {$totalweight = $total_w / 1000;}
        if ($total_w < 1000)
        {$weightunit = 'grams';}
        else
        {$weightunit = 'kilograms';}
        if ($total_w <= 300)
        {$postcost = '1.00';}
        elseif 
        ($total_w >=301 && $total_w <= 500)
        {$postcost = '2.00';}
        else
        {$postcost = '3.00';}
        $total_all = $total + $postcost;
        
        $output[] = '<tr>';
        $output[] = '</tr>';
        $output[] = '<tr>';
        }
        $output[] = '</table>';
        $output[] = '<div align= right><button type="submit">Update cart</button></div>';
        $output[] = '<table>';
        $output[] = '<tr>';
        $output[] = '<tr>';
        $output[] = '<td><p class="tablebody">Your shopping cart total:</p></td>';
        $output[] = '<td>&nbsp;&nbsp;</td>';
        $output[] = '<td><p class="tablebody"><strong>&pound;'.sprintf("%1.2f", $total).'</strong></p></td>';
        $output[] = '<tr>';
        $output[] = '<td><p class="tablebody">Your shopping cart total weight:</p></td>';
        $output[] = '<td>&nbsp;&nbsp;</td>';
        $output[] = '<td><p class="tablebody"><strong>'.$totalweight.'</strong>&nbsp;'.$weightunit.' of sweets.</p></td>';
        $output[] = '<tr>';
        $output[] = '<td><p class="tablebody">Current shipping total</p></td>';
        $output[] = '<td>&nbsp;&nbsp;</td>';
        $output[] = '<td><p class="tablebody"><strong>&pound;'.sprintf("%1.2f", $postcost).'</strong></p></td>';
        $output[] = '<tr>';
        $output[] = '<tr>';$output[] = '<tr>';$output[] = '<tr>';$output[] = '<tr>';$output[] = '<tr>';
 
        $output[] = '<td><p class="tablebody">The total cost of your order is:</td>';
        $output[] = '<td>&nbsp;&nbsp;</td>';
        $output[] = '<td><p class="tablebody"><strong>&pound;'.sprintf("%1.2f", $total_all).'</strong></td>';
 
        $output[] = '</form>';
        $output[] = '</form>';
        }
    else
    {
    $output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
}
 
function sendtopaypal()
{
    global $db;
    $cart = $_SESSION['cart'];
    
    $i=1;
    if ($cart) 
    {
    $items = explode(',',$cart);
    $contents = array();
    foreach ($items as $item) 
        {
        $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        
    echo "<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>";
    echo "<input type='text' name='cmd' value='_cart'/>";
    echo "<input type='text' name='upload' value='1'/>";
    echo "<input type='text' name='business' value='katie.photoangel@googlemail.com'/>";
    for($i=1; $i <= count($contents); $i++)
        {
        echo $i;
    foreach ($contents as $id=>$qty) 
            {
    $sql = 'SELECT * FROM products1 WHERE id = '.$id;
    $result = $db->query($sql);
    $row = $result->fetch();
    extract($row);
        
    echo    "<input type='text' name='item_name_$i' value='".$sweet."'/>";
    echo    "<input type='text' name='amount_$i' value='".$price."'/>";
            }
        }
    }
    echo    "<input type='submit' value='Pay securely with Paypal'/>";
    echo    "</form>";
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Complete novice looking for help...

Post by califdon »

I'm afraid this is much more than I have time to tackle at this point. You have shown 3 functions, but not the code that uses them, so I am unable to make much out of it. Whenever an inexperienced programmer modifies a complex script, there is a real chance for things to get messed up. You definitely cannot just remove fundamental sections of code, like a foreach loop, and expect it to work. If you can't get the script's author to help you, perhaps someone else here may have the time to review the original script, but I'm afraid I don't. I wish I could help you. Sorry.
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Re: Complete novice looking for help...

Post by Christian1977 »

No problem...

Thanks anyway for taking the time to have a look. Greatly appreciated.

Chris
Christian1977
Forum Newbie
Posts: 7
Joined: Sat Mar 15, 2008 11:10 am

Re: Complete novice looking for help...

Post by Christian1977 »

I did get the solution in the end which was to remove the for loop that included the incrementing of the $i variable and place this code within the foreach loop.

Thanks to califdon for spending time looking into the problem.
Cheers
Chris
Post Reply