0

Hello guys

I've coded cart class with cart items and showed cart items in a table

but I had a problem when I try to update the cart.. It updates all the cart items when I'm changing quantity in one of the items

HTML

<div class="cartContainer">
        <h1 class="shoppingBag">your shopping bag</h1>

        <h2 class="products-in-cart-info">YOU HAVE (<?= $countProducts ?>) ITEM(S) IN YOUR BAG</h2>
        <?php if (ShoppingCart::checkProductsInCart() >= 1) { ?>
            <div style="width: 990px; margin: 0px auto; float: left;">
                <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
                    <table id="cartTable">
                        <tr>
                            <thead>
                            <th>product</th>
                            <th>description</th>
                            <th>color</th>
                            <th>quantity</th>
                            <th>subtotal</th>
                            <th>remove</th>
                            </thead>
                        </tr>
                        <tbody>
                        <?php foreach ($cartItems as $cartItem) { ?>
                            <tr>
                                <td><img width="35px" height="70px" src="img/<?= $cartItem->image ?>"></td>
                                <td>
                                    <?= $cartItem->manufacture . ' ' . $cartItem->series . ' ' . $cartItem->model ?>
                                    <span style="height:10px;display:block;"></span>
                                    <?= $cartItem->price . ' $' ?>
                                </td>
                                <td><?= $cartItem->colorName ?></td>
                                <input type="hidden" name="colorId" value="<?= $cartItem->colorId ?>">
                                <td>
                                    <input type="button" id="minusInCart" class="minus" value="-">
                                    <input id="quantityInCart" class="count" type="text" name="quantity"
                                           value="<?= $cartItem->quantity ?>">
                                    <input type="button" id="plusInCart" class="plus" value="+">
                                </td>
                                <td><?= $cartItem->subtotal . ' $' ?></td>
                                <input type="hidden" name="productId" value="<?= $cartItem->productId ?>">
                                <td><input name="deleteItem" title="Remove this item" type="submit" class="deleteItem"
                                           value=""></td>
                            </tr>
                        <?php } ?>
                        </tbody>
                    </table>
                    <input type="submit" id="updateCart" name="updateCart" value="Update Shopping Bag">
                    <input type="submit" id="checkout" name="checkout" value="Proceed To Checkout">
                    <input type="submit" id="emptyCart" name="emptyCart" value="Empty Bag">
                </form>
            </div>
        <?php
        } else {
            echo "<div style='width:20%;margin:0 auto;padding:0;'>
                    <a href='index.php' id='backHome'>Back to home</a>
                  </div>";
        } ?>
    </div>

PHP

} elseif (isset($_POST['updateCart'])) {
    ShoppingCart::updateCart($_POST['quantity'], $cartId, $_POST['productId']);

// checkout process
}

PHP ShoppingCart Class

class ShoppingCart
{

    public
        $Id,
        $CustomerId,
        $CartDate,
        $IsComplete,
        $IsShipped;


    public static function updateCart($quantity, $cartId, $productId)
    {
        $dbh = mySqlDatabase::getConnectionObject();
        $sql = "UPDATE cartitems
                SET quantity =:quantity
                WHERE productId = :productId
                AND cartId = :cartId";
        $stmt = $dbh->prepare($sql);
        $data = array(
            ':quantity' => $quantity,
            ':productId' => $productId,
            ':cartId' => $cartId
        );
        $stmt->execute($data);
    }
}

and thanks in advance.

2
Contributors
2
Replies
30
Views
3 Years
Discussion Span
Last Post by safi.najjar1
0

A problem I see here is related to the attributes id and name which are the same for all the items, an id must be unique over the page, regarding the name you can have multiples in case of radio buttons because of the nature of this type of input field: if it is not checked is not sent to the $_POST array:

$items  = array('oranges', 'apples', 'coconut');
foreach($items as $item)
{
    echo "<input type='radio' name='fruit' value='".$item."' />";
}

When sending it outputs only the checked:

Array
(
    [fruit] => apples
    [submit] => submit
)

When using a text input field, instead, since $_POST is an array, the index name will overwrite all the previous and return always the last:

foreach($items as $item)
{
    echo "<input type='text' name='fruit' value='".$item."' />";
}

Output:

Array
(
    [fruit] => coconut
    [submit] => submit
)

Now, I suppose, you're using javascript to increment and decrement values, correct? By referring to the id, and being repeated over the page it's probably creating some unexpected behaviour. You could check the output of the form by applying this:

echo "<pre>" . print_r($_POST, true) . "</pre>";

So, you can see what is sent.

Edited by cereal

0

thx very much I forgot how to test the code !
but I think I've found the answer about how to update with loop like foreach($items as $item)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.