I am building a shopping cart, everything is working fine accept for one thing. The diagram bellow represents the shopping cart page (cart.php page).
SHOPPING CART PAGE
====================================================================
| Id | name | price | quantity | remove |
|------------------------------------------------------------------|
| 111 cat $200 2 checkbox[] |
| 222 dog $200 1 checkbox[] |
| 333 bird $200 1 checkbox[] |
|------------------------------------------------------------------|
| back to shopping Update cart |
====================================================================
Lets say I have added 3 items to my cart; cat, dog and bird. And I didnt want the cat because it will eat the birds. So I decided to tick the check box under the heading "remove" and clicked update cart to remove the cat from the shopping cart.
The problem is that cat doesn't get removed... instead the last item in the cart get removed, which is BIRD. Similarly, if I decided to keep item 1(cat) and decided on removing the dog, the last item in the shopping cart is still going to remove instead of dog.
MY code is bellow and I have added a html checkbox input in the loop and added the checkbox name to an if isset post [checkboxname] statement inside the if isset post [submit] statement.
BTW MY code is not as long as it looks, i just added the style sheet in it to make it all look clearer.
**CODE
<?php
//////////////////////////////////////////////
$con = mysql_connect("localhost","root","");
mysql_select_db("itemsfuck",$con);
//////////////////////////////////////////////
session_start();
// create an array
$my_array=array();
// put the array in a session variable
if(!isset($_SESSION['animals']))
$_SESSION['animals']=$my_array;
// move submit code outside of foreach loop
if (isset($_POST["submit"]))
{
for ($i = 0; $i < count($_POST['aaa']); $i++) {
$aaa = $_POST['aaa'][$i];
$key_var = $_POST['ke'][$i];
// setting the session spesific session array value different for each key
$_SESSION['animals'][$key_var] = $aaa;
}
if (isset($_POST['brosrs'])) {
unset($_SESSION['animals'][$key_var]);
}
}
?>
<?php
// making the table and headers
?>
<table id="tablehead" width="700px">
<tr >
<td id="c">ID</td>
<td id="c">Name</td>
<td id="c">Price</td>
<td id="c">Product ID is</td>
<td id="c">Quantity</td>
</tr>
</table>
<!-- starting the form outside loop -->
<form method="post" action="">
<?php
//// declare the total price and start it as 0, Because its before the actual items added to cart
$total_price=0;
$subtotal = 0;
// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{
////////////////////////////DUMPING EVERYTHING FROM DATABASE////////////////////////////////////////////////
$key_array = array_keys($_SESSION['animals']);
$sql = "SELECT id, name, price FROM products WHERE id IN ({$key}) ORDER BY name";
// Get record where $key exists. code from your question
$sql = "SELECT id, name, price FROM products WHERE id IN ({$key}) ORDER BY name";
$myData = mysql_query($sql,$con);
// Loop through each record and see if $key_array is present in $row['id']
while($row = mysql_fetch_array($myData)){
if(in_array($row['id'], $key_array)){
// display records
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<td >". $row['id']. "<td/>";
echo "<td class='name'>".$row['name']. "<td/>";
echo "<td class='qtyid'>". $row['price']. "<td/>";
echo "<br/>";
$quantity_calc = $value;
$subtotal = $value * $row['price'];
$price = $row['price'];
echo $subtotal;
}
$total_price += $subtotal;
}
////////////////////////////DUMPING EVERYTHING FROM DATABASE////////////////////////////////////////////////
// and print out the values
echo " <td> Product ID is " .$key. " Quantity is </td>";
// getting the updated value from input box
?>
<td> <input id="numberinputsize" type="number" size="1" name="aaa[]" min="1" max="10" value="<?php echo $value ; ?>" > <td/>
<!-- take a hidden input with value of key -->
<td ><input type="hidden" name="ke[]" value="<?php echo $key; ?>"><br><td/>
<!-- setting up a checkbox next to each item for removing the item when the checkbox is checked and the submit button is pressed -->
<td> <input type ="checkbox" name="brosrs" value = "<?php echo $key; ?>"><?php echo $key; ?></input> </td>
<?php
echo "</tr>";
echo "</table>";
}
?>
<table class="ass">
<td>Total amount $<?php Echo $total_price; ?></td>
<td><a href="products-legit.php">back to shopping</a></td>
<td> <input class="inputbox" type="submit" value="Update value of key" name="submit"/><td/>
</table>
</form>
<style>
.table{
border: 3px solid whitesmoke;
background-color:whitesmoke;
width:700px;
height:100px;
margin:auto;
}
.tr{
border: 4px solid whitesmoke;
background-color:whitesmoke;
}
.ass{
margin:auto;
width:700px;
border: 2px solid whitesmoke;
background-color:whitesmoke;
}
.inputbox{
width:150px;
float: right;
}
#tablehead{
margin:auto;
}
#c{
border: 1px solid whitesmoke;
background-color:whitesmoke;
}
.name{
width:110px;
border: 2px solid whitesmoke;
background-color:whitesmoke;
}
.qtyid{
width:100px;
border: 2px solid whitesmoke;
background-color:whitesmoke;
}
#numberinputsize{
width:40px;
}
</style>
I was thinking of making a separate submit button next to the update care, called remove. which is specifically for removing checkbox items. But I cant have it inside the loop because there is already an input form using a submit button.
Any ideas on what could be the problem? thanks in advance
Aucun commentaire:
Enregistrer un commentaire