php - i am doing an add to cart program.i am listing several products on a single page.but i can only change the quantity of the first product -
i new php. doing add cart program.i listing several products on single page.but can change quantity of first product in list. while others taking default value 1.
this program used listing multiple products on single page retrieving database.there different categories , therefore taking cat_id url.
custproduct.php <?php session_start(); if (isset($_get['id'])) { // echo"id: ".$_get['id']; include ('custdb1.php'); echo "<h2>list of products:</h2>"; $sql = "select * `product` `cat_id`=".$_get['id']; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<br> product id: ". $row["pro_id"]. " - product name: ". $row["pro_name"]. "<br>"; echo "<img src='upload/".$row['pro_img']."'/>"; echo "<br> price: rs. ". $row["pro_price"]. " "; echo "<br>"; echo "quantity: <input type='text' name='qty' id='qty' value='1'>"; echo "<br>"; echo "<button onclick='addtocart(".$row['pro_id'].")'>add cart</button>" ; } } } else { echo "0 results"; } ?> <script> function addtocart(id) { var qty = document.getelementbyid('qty').value; document.write("qty"); window.location ="cart.php?id="+id+"&qty="+qty; } </script>
this cart.php page.this page used add products in cart.the customer see cart clicking link below "see list"
<?php session_start(); include('custdb1.php'); //$id = isset($_get['id']) ? $_get['id'] : ''; if (isset($_get['id'])) { $id=$_request["id"]; $qty=$_request["qty"]; //echo"qty=".$qty; $_session['cart'][$id] = array('id'=> $id,'qty'=> $qty); $sql = "select `pro_img` `product` `pro_id`=".$id; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "quantity:".$qty.'<br>'; echo "id:".$id.'<br>'; echo "<img src='upload/".$row['pro_img']."'/>"; } } } echo "<h4 align='center'> click here <a href='shoppingcart.php'>see list</a> </h4>"; echo "<h4 align='center'> click here <a href='custprofile.php'>continue shopping</a> </h4>"; ?>
this shopping cart.php.this page shows added products in cart...i.e customer can see cart.
<?php session_start(); include('custdb1.php'); echo "<pre>"; $value=$_session['cart']; //print_r($value); foreach ( $value $key=> $final_val ){ $cat_id = $key; $pro_id = $final_val['id']; $qty = $final_val['qty']; echo"product id:".$pro_id; echo"quantity:".$qty; $sql = "select * `product` `pro_id`=".$pro_id; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<br> product id: ". $row["pro_id"]. " - product name: ". $row["pro_name"]. "<br>"; echo "<img src='upload/".$row['pro_img']."'/>"; // echo "<button onclick='update(".$row['pro_id'].")'>update</button>" ; // echo "<h4 align='left'><a href='update.php'>update cart</a> </h4>"; } } else { echo "0 results"; } } echo "<h4 align='center'> click here <a href='custprofile.php'>continue shopping</a> </h4>"; ?>
set id of quantity html product id
while($row = $result->fetch_assoc()) { echo "<br> product id: ". $row["pro_id"]. " - product name: ". $row["pro_name"]. "<br>"; echo "<img src='upload/".$row['pro_img']."'/>"; echo "<br> price: rs. ". $row["pro_price"]. " "; echo "<br>"; echo "quantity: <input type='text' name='qty' id='". $row["pro_id"]. "' value='1'>"; echo "<br>"; echo "<button onclick='addtocart(".$row['pro_id'].")'>add cart</button>" ;
}
then can select input element corresponding product id set id
function addtocart(id) { var qty = document.getelementbyid(id).value; document.write("qty"); window.location ="cart.php?id="+id+"&qty="+qty; }
Comments
Post a Comment