Jump to content
Killersites Community
makamo66

Update Shopping Cart

Recommended Posts

If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?

Share this post


Link to post
Share on other sites
On 11/8/2019 at 6:26 PM, makamo66 said:

If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?

<tr>
        <th class="tablerow">Product Code</th>
        <th class="tablerow">Product Name</th>
        <th class="tablerow">Image</th>
        <th class="tablerow">Quantity</th>
        <th class="tablerow">Price</th>
        <th class="tablerow">Total Price</th>
        <th class="tablerow">Action</th>                 
    </tr>

    <?php
    $grand_total = 0;                                   // For Calculating Grand Price
    foreach($_SESSION['cart'] as $id=>$quantity)
    {
        $sql="SELECT * FROM products WHERE id='$id'";
        $result=mysql_query($sql) or die("Error Message");
        while($row=mysql_fetch_array($result))
        {       
        $grand_total+= $row['product_price']*$quantity; // For Calculating Grand Price  
    ?>        

    <tr>                
        <td class="tablerow"><?php echo $row['id']; ?></td>
        <td class="tablerow"><?php echo $row['product_name']; ?></td>
        <td class="tablerow"><?php echo "<img height='50' width='50' src='admin/".$row['product_image']."'/>" ?></td>

        <form name="update_cart" action="update_cart.php" method="post">

        <td class="tablerow"><input type="text" name="quantity" value="<?php echo $quantity; ?>" /><br /><input type="image" src="admin/images/updatecart.png" name="Update" value="Update" /></td>

        </form>

        <td class="tablerow"><?php echo $row['product_price']; ?></td>
        <td class="tablerow"><?php echo $quantity*$row['product_price']; ?> </td>
        <td class="tablerow"><?php print "<a href='delete_cart.php?pid=".$row['id']."'><img src='admin/images/delete.png'></a>"; ?></td></form>
    </tr>

    <?php 
        }
    }
    ?>

    <tr>
        <td class="tablerow" colspan="7">Grand Total: Rs <?php echo $grand_total; ?></td>
    </tr>       

    <tr>
        <td class="tablerow" colspan="7"><?php print "<a href='clear_cart.php'><img src='admin/images/clearcart.png'></a><a href='http://localhost/Shopping-Cart/front-end/'><img src='admin/images/continueshopping.png'></a><a href='update_cart.php'><img src='admin/images/placeorder.png'></a>";?></td>
    </tr>              

    </table>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×