Jump to content

Add Increment based on number of items in an array Probably Simple


ashton321

Recommended Posts

I am trying to integrate my shopping cart with paypal, however i can only get one item to work.  I beleive it is do to the fact that only the last item in my loop is being past in the $_POST[item_name] variable.  In order to send to items all the different parts price name quantity etc need to end with _1 _2 _3 etc.  I need to do this for each item in the cart.  Here is my current code.  But i do not know how to get the numbers to add to the end for each item and still be linked to the correct price and quantity.

 

<?php 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;
                }
                echo '<form action="http://www.fblaflowers.com/grad/php_paypal/process.php" method="post" id="cart">';
                echo '<table width="885" align="center"><tr align="left"><th>Flower</th><th>Unit Price</th><th>Quantity</th><th>Total Price</th></tr>';
                foreach ($contents as $id=>$qty) {
                        $sql = 'SELECT * FROM flower_inventory WHERE id = '.$id;
                        $result = $db->query($sql);
                        $row = $result->fetch();
                        extract($row);
                        echo '<tr>';
                        echo '<td>'.$name.'</td>';
                        echo '<td>$'.$price.'</td>';
                        echo '<td>'.$qty.'</td>';
                        echo '<td>$'.number_format($price * $qty,2).'</td>';
                        echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
                        $total += $price * $qty;
                        echo '</tr>';
                        ?> <input type="hidden" name="item_name" id="item_name" value="<?=$name?>">
                <input type="hidden" name="amount" id="amount" value="<?=$price?>">
                <input type="hidden" name="quantity" id="quantity" value="<?=$qty?>"> <?php
                }
                echo '</table>';
                echo '<p>Grand total: <strong>$'. number_format($total,2).'</strong></p>';
                                echo '<div><input type="image" name="submit" src="http://www.fblaflowers.com/images/paypal.gif"></div>';
                echo '</form>';


        } else {
                echo '<p>You shopping cart is empty.</p>';
        }
}

try

...
                        echo '</tr>';
                        ?> <input type="hidden" name="item_name[]" id="item_name" value="<?=$name?>">
                <input type="hidden" name="amount[]" id="amount" value="<?=$price?>">
                <input type="hidden" name="quantity[]" id="quantity" value="<?=$qty?>"> <?php
                }
                echo '</table>';
...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.