Php Shopping Cart Update quantity using Sessions

Go To StackoverFlow.com

0

Another solution I came up with after posting this question instead of using a for loop to add one each time I could have just said:

$session->cart[$params->id] => $qty;

I found this to be a better way because you can update the cart this way instead of adding the desired number onto what is already in the cart.

For all reading this post I came up with a solution to update the cart using the handler. it is as follows . . . first in the form part of details.php

<form method="post"> //should be added to retrieve the qty data from the text field.

next in the handler . . .add the follwing loop and variable

$qty = $_POST['qty']; or $qty = $_REQUEST['qty'];

then

for($i =0; $i < $qty ; $i++){
  ++$session->cart[$params->id];
}

I am creating a shopping cart website using php to handle some tasks. I am having difficulty changing the quantity of an item in the cart. here is my code that i use to get the input handle the submission and display the quantity in a cart view

details.php:

    <form id="cart_form" action="handler-add-cart.php">
     <input type="hidden" name="id" value="<?php echo $product->id ?>" />
     <input type="submit" value="add to cart"/>
    **Quantity:<input type="text" name="qty" />**
    </form>

handler_add_cart.php:

<?php
require_once "include/Session.php";
$session = new Session();
**$params = (object) $_REQUEST;
++$session->cart[$params->id];**
header("location: cart.php");

cart.php:

 <?php
    require_once "include/Session.php";
    $session = new Session();
    require_once "include/db.php";

    // The $cart array simplifies the view generation below, keeping 
    // computations and database accesses in this controller section.
    $cart = array();
    if (isset($session->cart)) {
    $total = 0;
    foreach ($session->cart as $prod_id => $qty) {
    $product = R::load("products", $prod_id);
    $total += $qty * $product->price;

    $entry = new stdClass();  // entry will contain info for table
    $entry->id = $prod_id;
    $entry->price = $product->price;
    $entry->name = $product->name;
    **$entry->qty =  $qty ;**
    $cart[] = $entry;
    }
    }
    ?>

// here i have removed some html to focus in on my issue i have all the tags in the files so that is not the issue

    <h2>Cart</h2>

    <?php if (count($cart)): ?>

     <table id="display">
      <tr>
       <th>product</th><th>id</th><th>quantity</th><th class='price'>price</th>
      </tr>
      <?php foreach ($cart as $entry): ?>
       <tr>
        <td><a href="details.php?id=<?php echo $entry->id ?>"
            ><?php echo $entry->name ?></a></td>
        <td><?php echo $entry->id ?></td>
        **<td class='qty'><?php echo $entry->qty ?></td>**

             i cleared these fields below to not distract from the issue im having
        <td >
         </td>
       </tr>
      <?php endforeach ?>
      <tr>
       <th >

       </th>
      </tr>
     </table>


    </body>
    </html>
2012-04-05 20:13
by NoName
It might be good to tell us what difficulty you are having exactly rather than posting the entire code.. - Jules 2012-04-05 20:15
what's the printout of this. $session->cart, use pastie.or - wesside 2012-04-05 20:22
the problem i am having is when i fill the text field in that is labeled quantity in the details.php file and click the add to cart button, the cart only adds 1 to the information in the cart - NoName 2012-04-05 20:30
Cuz you do $entry->qty = $qty; instead of $entry->qty += $qty; - Jules 2012-04-05 20:33
if i do that it only increments the value. I would like it to except the text input i have and put that as the quantity in the cart vie - NoName 2012-04-05 20:37


0

To increment the quantity with the value entered in the form do:-

$entry->qty =  $qty + $_POST['qty'];

Although you probably want to verify that the user has entered a number and that the form has been posted, so you probably want something like:-

if (isset($_POST['qty']) && is_numeric($_POST['qty])) {
   $entry->qty =  $qty + $_POST['qty'];
}
else
{
   $entry->qty =  $qty;
}
2012-04-05 21:31
by rgvcorley
You should also consider using a PHP framework rather than jumbling HTML and PHP together like this - it will get very difficult to manage otherwis - rgvcorley 2012-04-05 21:36
this is not a solution. I have to somehow reference the variable, pass it to the handler-add-cart.php while keeping it separate or together, then pull it from the cart object in the cart.php file. any idea how to do tha - NoName 2012-04-06 03:42
I'm not going to write your entire script for you - make your question more specific and people will be more inclined to help yo - rgvcorley 2012-04-06 08:36
oh no i completely understand thats not what im asking i will simplify the above to show where my issue is. I just cant seem to pull the quantity field from the $_REQUEST object and display it in my cart - NoName 2012-04-06 18:42
Ads