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>
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;
}