I am creating a mini shopping cart, where I displayed my item and and quantity of my item, I want to add my product price into quantity, how I can do this?
My code is
enter code here
<?php
Include_once("connection.php");
?>
<form action="view.php" action ="GET">
<div align="center">
<table align="center" border="2" width= 70%>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Total</th>
</tr>
<?php
$view = mysqli_query($connection, "select * from `add_cart`");
$afshan=mysqli_fetch_assoc($view);
if($afshan)
{
?>
<td><?php echo $afshan['name']?></td>
<td><?php echo $afshan['price']?></td>
<td><img width="50px" height="50px" src="image/<?php echo $afshan['image'];? >"></td>
<td><input type="number" name="quantity" min="0" max="3" step="1" value="0"> <input type="submit" name="cart" value="Add to Cart"></td><td></td>
<?php
}
?>
</table>
</div>
</form>
echo $afshan['price']*$afshan['quantity']
Related
I am buildingd a library management system in PHP and HTMl.
I have some problem with the borrowing system where user can borrow books.
Here is my emitere-carti.php:
<form action="imprumutare.php" method="POST">
<table class="table table-dark">
<thead>
<tr>
<th>Imprumutare</th>
<th scope="col">Titlu </th>
<th scope="col">Autor</th>
<th scope="col">Categorie</th>
<th scope="col">Stoc</th>
</tr>
</thead>
<tbody>
<?php
$selectare="SELECT * FROM carti";
$rezultat=mysqli_query($conn,$selectare);
if(mysqli_num_rows($rezultat)>0){
while($row=mysqli_fetch_assoc($rezultat)){
$carte_nume=$row['titlu'];
$disabled=$row['stoc']>0 ? "" :"disabled";
?>
<tr>
<td><input type="submit" name="test" class="btn btn-secondary"
value="Imprumutare" <?php echo $disabled;?>></input>
<td><input type="text" name="nume" value="<?php echo $row['titlu'];?
>"></input></td>
<td><?php echo $row['autor'];?></td>
<td><?php echo $row['categorie'];?></td>
<td><?php echo $row['stoc'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Here is my imprumutare.php(that means borrowing):
include('conexiune.php');
//sfarsit if
//Imprumutare
if(isset($_POST['test'])){
$id=$_POST['identificator'];
$nume_carte=$_POST['nume'];
$sql_rezervare="UPDATE carti SET stoc=stoc-1 WHERE
titlu='$nume_carte' ";
if( mysqli_query($conn,$sql_rezervare)){
header('location:emitere_carti.php');
}
else{
die(mysqli_error($conn));
}
}
Here is a screenshot about what I am talking:
So my problem is both buttons are decrementeing the same value(the value of stoc).
Can someone help me?
Hey guys am trying to work on. when a user updates the quantity on a product its supposed to multiply the quantity by the unit price of the product. Apparently, it can only work with a single product, if it is more than that, everything turns to zero when i try to update the quantity. here is my code please help.
<tr align="left">
<th width="62"><input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?>"/></th>
<th width="155">
<img src="admin_area/product_images/<?php echo $product_image; ?>" width="60" height="60" /><br>
<?php echo $product_title; ?> <br><br>
</th>
<th width="61"><?php echo $product_size; ?></th>
<th width="62"><?php echo $product_paper; ?></th>
<th width="67"><?php echo $product_grammage; ?></th>
<th width="60"><?php echo $product_finish; ?></th>
<th width="96"><?php echo $product_sides; ?></th>
<th width="76"><?php echo $product_print; ?></th>
<th width="105"><input type="text" size="6" name="qty" value="<?php $_SESSION['qty']; ?>" /></th>
<?php
if(isset($_POST['update_cart'])) {
$qty = $_POST['qty'];
$update_qty = "update cart set qty='$qty'";
$run_qty = mysqli_query($con, $update_qty);
$_SESSION['qty']=$qty;
$total = $total*$qty;
}
?>
<th width="112"><?php echo "UGX" . $single_price; ?></th>
</tr>
<?php } } ?>
</table>
<table width="217" border="0" align="right" class="layoutCartTotal">
<tr>
<td colspan="2"><h2><b>Sub Total:</b></h2></td>
<td ><h2><?php echo "UGX" . $total; ?></h2></td>
</tr>
<tr>
<td><input type="submit" name="update_cart" class="myButton" value="Update Order" /></td>
<td><input type="submit" name="continue" class="myButton" value="Continue Shopping" /></td>
<td><input type="submit" name="checkout" class="myButton" value="Place Order" /></td>
</tr>
I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>
Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html
code for order.html.php:
<body>
<p> Place order</p>
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<form id="form1" action="" method="post" >
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<div><input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
<div>
</tbody>
</form>
</table>
</body>
and dom presented in developer tool is:
<body>
<p> Place order</p>
<div><input type="hidden" name="add" value="placeorder">
<input type="submit" value="Place Order">
<div>
</div></div>
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<form id="form1" action="" method="post" ></form>
<tbody>
<tr>
<td>1</td>
<td>hp keyboard old</td>
<td>400</td>
<td><input type="number" name="1"></td>
</tr>
<tr>
<td>2</td>
<td>lenovo keyboard old</td>
<td>450</td>
<td><input type="number" name="2"></td>
</tr>
<tr>
<td>3</td>
<td>kaspersky antivirus</td>
<td>430</td>
<td><input type="number" name="9"></td>
</tr>
</tbody>
</table>
</body>
see the position of submit and in order.html.php and the resulted dom.
because of this dom structure order.html.php is not able to submit value that resides in input filed.
here is the post with related issue but not exactly what is represent here,
Retrieve data from HTML table to PHP
Try proper html structure :
<body>
<p> Place order</p>
<form id="form1" action="" method="post" >
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<!--<td><?php echo $productData['mrp']; ?></td>-->
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="4">
<input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
</td></tr>
</tbody>
</table>
</form>
</body>
First off, just wrap the table with the <form> tag.
Second, it's not really good to use productId as your name="$productId", instead, use something like name="products[$productId]" so that after form submission, they will be grouped on the same variable as an array.
Sample Code: Sample Demo
$dataProduct = array(
array('productId' => 1, 'productName' =>'hp keyboard old', 'productPrice' => 400),
array('productId' => 2, 'productName' =>'lenovo keyboard old', 'productPrice' => 430),
array('productId' => 9, 'productName' => 'kaspersky antivirus', 'productPrice' => 430),
);
if(isset($_POST['submit'])) {
$values = $_POST['products']; // get all the grouped values
$total = array();
foreach($values as $productId => $quantity) { // loop values
foreach($dataProduct as $productData) {
if($productData['productId'] == $productId) {
// simple multiplication, quantitiy times price for each item
$total[$productId] = $productData['productPrice'] * $quantity;
}
}
}
echo '<pre>';
print_r($total);
echo '</pre>';
}
?>
<form method="POST">
<table cellpadding="10">
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ; foreach($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="products[<?php echo $productData['productId']; ?>]"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td><input type="submit" name="submit" /></td>
</tr>
</tbody>
</table>
</form>
So for example, in the form, I inputted in all of textboxes a quantity of 2. print_r() should yield like this:
Array
(
[1] => 800
[2] => 860
[9] => 860
)
Indices as the keys, the values are the totals for each quantity times price
I have a database
that have two tables.
The sql query fetching data correctly.
I want to sort fields data by typing table heading in text box and click on go that in table footer.
Many Thanks
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th class="capt" colspan="6">Available Projects</th>
</tr>
<tr>
<th>Select</th>
<th>Project</th>
<th>Crawler</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include(dirname(__file__)."/includes/dbConn.php");
$result = mysql_query("SELECT *, ( SELECT name FROM projects WHERE projects.id = crawlers.pid ) AS pname FROM `crawlers`", $appConn);
while($row = mysql_fetch_array($result))
{
?>
<tr id="crawler_<?php echo $row['id']; ?>">
<td>
<input value="" id="check" type="checkbox">
<input id="crawler_id_<?php echo $row['id']; ?>" type="hidden" value="<?php $row['id']; ?>" /> <!-- crawler id -->
</td>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>username#domain.com</td>
<td>Enabled</td>
<td><a class="edit" href="#">edit</a><a class="add" href="#">run</a><a class="delete" href="#">delete</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="6"> Sort Fields by <input value="type here" id="field" type="text"> Go </th>
</tr>
</tfoot>
</table>
I think, you are looking for ORDER BY
Have a look at the MySQL Documentation about the SELECT syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html