I wanted to retrieve the proid thats in the session.
when i add to cart rdy to check out .
The proid seems cannot to be retrieve which show empty.
but it did count the item that i entered.
cause when i add 2 item in cart , it display 2 row.
please help me.
this is cart.php
if (isset($_SESSION['cart']))
{
$total = 0;
foreach($_SESSION['cart'] as $proid =>$x)
{
$results = mysqli_query($con,"Select * from product where Product_ID = $proid");
$myrow = mysqli_fetch_array($results);
$proid = $myrow['Product_ID'];
$productname = $myrow['Product_Name'];
$productprice= $myrow['Product_Price'];
$line_cost = $productprice * $x;
$total=$total+$line_cost;
?>
<li class = "cartlist">
<?php echo '<img src="data:image/jpeg;base64,' . base64_encode($myrow['Product_Pic']) . '" width="196" height="120">';?>
<p><span style = "font-weight: bold; font-size: 1.2em;">
<?php echo $productname ?>
</span></br />RM <?php echo $productprice;?>
</br />Quantity : <?php echo $x ?></br><a href='cart.php?proid=<?php echo $proid;?>&action=add'>Add </a> <a href ='cart.php?proid=<?php echo $proid;?>&action=remove'> Reduce </a></br>RM<?php echo $line_cost; ?> <br/>
</p> </li>
<?php
}
This is payment.php
i wanted to retrieve the proid and display the product name and total to the user.
print_r($_SESSION);
if (isset($_SESSION['cart']))
{
$total = 0;
foreach($_SESSION['cart'] as $proid =>$x)
{
$results = mysqli_query($con,"Select * from product where Product_ID = $proid");
$myrow = mysqli_fetch_array($results);
$productname = $myrow['Product_Name'];
$productprice= $myrow['Product_Price'];
$line_cost = $productprice * $x;
$total=$total+$line_cost;
?>
<tr>
<td><? echo $productname ?></td>
<td><? echo $x ?></td>
<td><? echo $line_cost ?></td>
</tr>
<?php
}
}
After adding a print_r($_SESSION['cart']); to the top of payment.php I get this output
Array ( [36] => 3 )
Related
I already search more about this topic solution "How to insert multiple item into database", but i can't understand. Hope somebody can help me !
shopping cart
{
while($row=mysqli_fetch_array($res)){
$total=($row['product_price']*$row['quantity']);
$tot = $tot + ($row['product_price']*$row['quantity']);
?>
<tr>
<td align="center"><img src="<?php echo $item['product_image'];?>" width="150px" height="150px" alt=""/></td>
<td><?php echo $row['product_name'];?></td>
<td><?php echo $row['quantity'];?></td>
<td>RM <?php echo $total;?></td>
<td style="text-align:center"><a href="delete_shopping_cart_item.php?id=<?php echo $row["id"];?>" onclick="return confirm('Are you sure you want to delete this item?');">
Remove
</a></td>
</tr>
<?php
}
if(isset($_POST['submit1']))
{
$customer = $_SESSION['login_user'];
$res = mysqli_query($mysqli,"SELECT * FROM shopping_cart where customer ='$customer'");
$row=mysqli_fetch_array($res);
$qty = $row['quantity'];
$product_name = $row['product_name'];
$rname = $_POST['rname'];
$raddress = $_POST['raddress'];
$rcontactno = $_POST['rcontactno'];
$total = $_SESSION['total'] ;
mysqli_query($mysqli,"insert into confirm_order (product_name,quantity,total_price,receiver_name,receiver_address,receiver_contact,customer)
values('$product_name','$qty','$total','$rname','$raddress','$rcontactno','$customer')");
$_SESSION['receiver'] = $rname;
}
}
Try this:
while ($row=mysqli_fetch_array($res)) {
$qty = $row['quantity'];
$product_name = $row['product_name'];
$rname = $_POST['rname'];
$raddress = $_POST['raddress'];
$rcontactno = $_POST['rcontactno'];
$total = $_SESSION['total'] ;
mysqli_query($mysqli,"insert into confirm_order (product_name,quantity,total_price,receiver_name,receiver_address,receiver_contact,customer)
values('$product_name','$qty','$total','$rname','$raddress','$rcontactno','$customer')");
}
I am trying to create a shopping cart and take the quantities in the cart to subtract from my inventory in my table in mysql. But everytime i try and update it keeps on subtracting the wrong amount. So far i have this.
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<center><B>Invoice</B></center>
<?php
require 'connect.php';
require 'item.php';
if(isset($_GET['id'])){
$result = mysqli_query($con, 'select * from products where
id='.$_GET['id']);
$products = mysqli_fetch_object($result);
$item = new Item();
$item->id = $products->id;
$item->name = $products->name;
$item->price = $products->price;
$item->quantity = 1;
// Check if the products exists in the cart
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++)
if($cart[$i]->id==$_GET['id'])
{
$index = $i;
break;
}
if($index==-1)
$_SESSION['cart'][] = $item;
else{
$cart[$index]->quantity++;
$_SESSION['cart'] = $cart;
}
}
// Delete products in cart
if(isset($_GET['index'])){
$cart = unserialize(serialize($_SESSION['cart']));
unset($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<table cellpadding="3" cellspacing="3" border="0">
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-Total</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
$index = 0;
for($i=0; $i<count($cart); $i++){
?>
<tr>
<td><?php echo $cart[$i]->id; ?></td>
<td><?php echo $cart[$i]->name; ?></td>
<td><?php echo $cart[$i]->price; ?></td>
<td><?php echo $cart[$i]->quantity; ?></td>
<td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?></td>
</tr>
<?php
$index++;
$quantitycart=$cart[$i]->quantity;
$idcart=$cart[$i]->id;
}
?>
<td><input type="submit" name="submit" value="submit"></td></tr>
<tr><td colspan="5"</tr>
<tr>
</tr>
</table><br>
<?php
if(array_key_exists('submit', $_POST))
{ $results = mysqli_query($con, 'select * from products');
for($i=0; $i<count($cart); $i++)
{
while($products= mysqli_fetch_object($results)){
$quantity= $products->quantity;
$newquantity= $quantity-$quantitycart;
$sql = "UPDATE products SET quantity='$newquantity' WHERE id='$idcart'";
}}
if ($con->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $con->error;
}
$con->close();
}
$con is my connnection. Any help would be appreciated. The main problem is located in the last part of the code.
I have add to cart already but I don't want to reload the page when I click Add to Cart Button so I can still choose other products even its not reloading the page.
Index.php
<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}else{
$sql_p="SELECT * FROM products WHERE product_id={$id}";
$query_p=mysqli_query($con, $sql_p);
if(mysqli_num_rows($query_p)!=0){
$row_p=mysqli_fetch_array($query_p);
$_SESSION['cart'][$row_p['product_id']]=array("quantity" => 1, "price" => $row_p['product_price']);
}else{
$message="Product ID is invalid";
}
}
}
?>
product.php
<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}else{
$sql_p="SELECT * FROM products WHERE product_id={$id}";
$query_p=mysqli_query($con, $sql_p);
if(mysqli_num_rows($query_p)!=0){
$row_p=mysqli_fetch_array($query_p);
$_SESSION['cart'][$row_p['product_id']]=array("quantity" => 1, "price" => $row_p['product_price']);
}else{
$message="Product ID is invalid";
}
}
}
?>
<h1>Products</h1>
<?php
if(isset($message)){
echo "<h2>$message</h2>";
}
?>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Items Price</th>
</tr>
<?php
$query = mysqli_query($con,"SELECT * FROM products WHERE product_category= 'Chicken' ORDER BY product_name ASC");
while($row=mysqli_fetch_assoc($query)){
?>
<tr>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_description']; ?></td>
<td><?php echo "$" . $row['product_price']; ?></td>
<td>Add to Cart</td>
</tr>
<?php
}
?>
</table>
Cart.php
<?php
if(isset($_POST['submit'])){
if(!empty($_SESSION['cart'])){
foreach($_POST['quantity'] as $key => $val){
if($val==0){
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
}
?>
<h1>View Cart || Products</h1>
<form method="post" action="index.php?page=cart">
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Subtotal</th>
</tr>
<?php
$sql = "SELECT * FROM products WHERE product_id IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .=$id. ",";
}
$sql=substr($sql,0,-1) . ") ORDER BY product_id ASC";
$query = mysqli_query($con, $sql);
$totalprice=0;
if(!empty($query)){
while($row = mysqli_fetch_array($query)){
$subtotal= $_SESSION['cart'][$row['product_id']]['quantity']*$row['product_price'];
$totalprice += $subtotal;
?>
<tr>
<td><?php echo $row['product_name']; ?></td>
<td><input type="text" name="quantity[<?php echo $row['product_id']; ?>]" size="6" value="<?php echo $_SESSION['cart'][$row['product_id']]['quantity']; ?>"> </td>
<td><?php echo "$" .$row['product_price']; ?></td>
<td><?php echo "$" .$_SESSION['cart'][$row['product_id']]['quantity']*$row['product_price']. ".00"; ?></td>
</tr>
<?php
}
}else{
?>
<tr><td colspan="4"><?php echo "<i>Add product to your cart."; ?></td></tr>
<?php
}
?>
<tr>
<td colspan="3">Total Price: <h1><?php echo "$" ."$totalprice". ".00"; ?></h1><td>
</tr>
</table>
<br/><button type="submit" name="submit">Update Cart</button>
</form>
<br/><p>To remove an item, set quantity to 0.</p>
//how to solve this please all kind guys can direct change code, my error is undifined offset value of product_id
<?php
include('dataconn.php');
$result=mysql_query("select * from product");
while($row=mysql_fetch_assoc($result))
{
echo "<p>$row[Product_Name]</p>";
echo "<p>$row[Product_Quantity]</p>";
echo "<p>Add To Cart</p>";
}
?>
logout
/*--add to cart page--*/
<?php
/* ----------- ADD TO CART PAGE ----------------*/
include("dataconn.php");
$product_id = isset($_GET['pid']) ? $_GET['pid'] : null;
$action = isset($_GET['action']) ? $_GET['action'] : null; //the action
$quantity=isset($_GET['qty']) ? $_GET['qty'] : null;
?>
<html>
<head>
<title>View Cart</title>
</head>
<body>
<?php
$customer_id=$_SESSION['customer_id'];
$result=mysql_query("select * from product");
$row=mysql_fetch_assoc($result);
switch($action)
{
//decide what to do
case "add":
$_SESSION['cart'][$product_id]++;
break;
case "remove":
$_SESSION['cart'][$product_id] = 0;
unset($_SESSION['cart'][$product_id]);
header("Location: payment.php");//if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
header("Location: payment.php");
break;
}
//------- for checkout ---------
if(isset($_SESSION['cart']) && $_SESSION['cart']) {
$total = 0;
echo "<form name='cart' method='POST' action=''>";
echo "<table border=\"1\" padding=\"3\" width=\"100%\" class=\"data-container\">";
echo "<td colspan=\"4\" align=\"right\"><img src=\"image/delete.png\"/></td>";
foreach($_SESSION['cart'] as $product_id => $quantity)
{
$sql = sprintf("SELECT * FROM product WHERE Product_ID = %d;", $product_id);
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$itemsInfo = mysql_fetch_assoc($result);
$cost = $itemsInfo["Product_Price"] * $quantity;
$total = $total + $cost; //add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">".$itemsInfo["Product_Name"]."</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$quantity </td>";
echo "<td align=\"center\">RM $cost</td>";
echo "<td align=\"center\"><img src=\"image/remove.png\"/></td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" ></td>";
echo "<td align=\"center\">Total</td>";
echo "<td colspan=\"2\" align=\"center\">RM $total</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
?>
<br>
<div>
<span style="margin-left: 88%"><input type="submit" name="shopping_more" class="custom-button" value="Back To Shopping"/></span>
</div>
</html>
i'm a beginner in php and i'm hoping that somebody here can help me in this.
i have a table that has a check box to select an item and a text box to indicate the quantity the person wants to borrow. i was wondering how i can retrieve both these data and then save them in my database.
here is a part of my code:
<td width="30">
<input id="optionsCheckbox" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['item_name']; ?></td>
<td align="center">
<img class="img-rounded" src="<?php echo $row['item_image'];?>" border="0" onMouseOver="showtrail('<?php echo $row['item_image'];?>','<?php echo $row['item_code'].": ".$row['item_name'];?> ',200,5)" onMouseOut="hidetrail()"></a></td>
<td><?php echo $row['item_quantity'] - $row['item_consumption']; ?> <?php echo $row['unit']; ?></td>
<td><input type="text" name="consume[]" pattern="[0-9]{1,4}"/></td>
here's what i have so far:
$id=$_POST['selector'];
$consume= $_POST['consume'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
echo $code; echo $name;
echo $id;
}
$x = count($consume);
for($y=0; $y < $x; $y++)
{echo $consume;
}
the echos are just for checking if the data goes through. i'm just making sure they do before i make a query for table insertion.
what i'm trying to do is to post them on another page, display them in a table (a la shopping cart) and then make the borrower fill out a form with his details.
ok, using gul's answer below, this is what i did:
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$consumes = substr($consumes,0,-1);
$query = $conn->query("select * from item where item_id IN($ids)")or die(mysql_error());
//table element
echo "<table border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Item Code</td>";
echo "<td width='150' align='center'>Item Name</td>";
echo "<td width='150' align='center'>Quantity</td>";
echo "</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table ="";
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="<td>".$consumes."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
however, when i select multiple items, only the last one shows up, but the consumes show up like this:
consume shows in one cell with single quotes and comma. help?
Try like this:
//first get the post
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
//$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$query = $conn->query("select * from item where item_id IN($ids))or die(mysql_error());
//table element
$table = "<table border='1'>";
$table.="<tr>";
$table.="<th>Code</th><th>Name</th>";
$table.="</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
ok, so i tried this and it worked. thanks to gul for helping me.
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$data = array();
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$row = array('selector'=>$id[$i],'consume'=>$consume[$i]);
//push in array
array_push($data,$row);
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
while ($row = $query->fetch()) {
$id = $row['id'];
}
?>
<tr>
<td><?php echo $code; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $consume[$i]; ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
I'm trying to display my products from the database. I'm trying to split it using the if 4%=0, but I can't get it to display 4 items in a row.
Here is my codes:
<table>
<tr>
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
?>
<td>
<table class="normal_text" align="center">
<tr>
<td><a href="product.php?product_id=<?php echo $id?>">
<img width="200px" height="133px" src="products/<?php echo $product_code?>.jpg" />
</a></td>
</tr>
<tr>
<td align="center" style="font-weight:bold"><?php echo $title;?></td>
</tr>
<tr>
<td align="center">$<?php echo $price;?></td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>
You've got the PHP logic before, rather than inside your HTML table output.
Try reorganizing like this:
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
?>
<table class="normal_text" align="center">
<tr>
<?php
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
// output product details -- note use of quotes to include PHP vars
$rowHTML = "<td><a href='product.php?product_id=$id'>";
$rowHTML .= "<img width='200px' height='133px' src='products/$product_code.jpg' />";
$rowHTML .= "</a><br/>";
$rowHTML .= "<strong>$title</strong>";
$rowHTML .= "$price</td>";
echo $rowHTML;
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
}
?>
</tr>
</table>
while($row=mysql_fetch_assoc($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
if ($split % 4 === 0) echo "\n<tr>";
?>
// ONLY <td> CODE </td> here. NO <tr> and NO </table>
<?php
if ($split % 4 === 3) echo "</tr>";
$split++;
}
?>
You are starting and ending at same time and then putting table for each.
You want to start <tr> when %4==0 and end </tr> when %4==3.
Rather than use modulus (%), just add a second variable called $cols, assign it to the amount of columns you want, and compare it. For example:
$rows = 1;
$cols = 4;
$display_all = mysql_query("SELECT * FROM products ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($rows==$cols){
echo '</tr><tr>';
$rows = 1;
}