【发布时间】:2015-05-12 13:35:24
【问题描述】:
我的网站上有一个购物篮,用户可以在其中将商品添加到购物篮中,然后将其移除并增加数量。该项目通过表单发布到 basket.php。该表单将项目的 ID、数量等发布到显示项目的 basket.php。使用输入类型 = '数字' 值 = '数量' 正确显示数量。更新 basket.php 中的数量也可以更新数据库中的数量。 我的问题是:这仅适用于一项。如果我在篮子中添加另一个项目,篮子中最后一个项目的数量将用于篮子中的所有其他项目,当我更新数量时会发生同样的事情。我怎样才能分别处理篮子中的每件物品,以免它们被视为一个。
我的代码:
function.php
<?php
/****Function to display list of foods in the order page,
info button and the add to basket button********/
function getFoo(){
$conn = mysql_connect('localhost','root','') or trigger_error("SQL",
E_USER_ERROR);
$db = mysql_select_db('1000_AD',$conn) or
trigger_error("SQL", E_USER_ERROR);
$sql = "SELECT * FROM food LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
$foo_id = $list['food_id'];
$foo_cat = $list['food_cat'];
$foo_type = $list['food_type'];
$foo_title = $list['food_title'];
$foo_price = $list['food_price'];
$foo_image = $list['food_image'];
// form below
echo "<div class = 'single_food'>
<form method='post' action='order.php'>
<h4>$foo_title</h4>
<div class = 'pic' ><img src='admin/food_images/$foo_image' width='180'
height= '160' /></div>
<p><b>£$foo_price</b></p>
<p><b>Qty</b> <input type='number' size='2' name='qty' style='width:30px'
min='1' max = '99' value='1' /></p>
<div class = 'btn'><a href='info.php?foo_id=$foo_id'
style='float:left'>INFO</a></div>
<div id ='btn2'><input type='submit' name='add_basket' id='submit'
value='Add to Basket' /></div>
<input type='hidden' name='foo_id' value='$foo_id' />
<input type='hidden' name='type' value='add' />
<input type='hidden' name='return_url' value='$current_url' />
</form>
</div>";
?>
Order.php
<?php
session_start();
include ("functions/functions.php");
?>
<?php
//if add basket button is clicked post the food_id and quantity selected
to basket in database.
if (isset($_POST['add_basket'])){
global $con;
$ip = getIp();
$id = $_POST['foo_id'];
$qty = $_POST['qty'];
$check_foo = "select * from basket where ip_add= '$ip' AND f_id
= '$id'";
$run_check = mysqli_query($con, $check_foo);
if(mysqli_num_rows($run_check)>0){
echo " ";
}
else {
$insert_foo = "insert into basket (f_id,ip_add,qty) values ('$id',
'$ip', '$qty')";
$run_foo = mysqli_query($con, $insert_foo);
$_SESSION['qty'] = $qty;
echo "<script>window.open('order.php','_self')</script>";
}
}
?>
Basket.php
<html>
<head>
<?php
session_start();
include ("functions/functions.php");
</head>
<body>
<?php
//displays items in the basket and calculates sub total
$total = 0;
global $con;
$ip = getIp();
$sel_price = "select * from basket where ip_add='$ip'";
$run_price = mysqli_query($con, $sel_price);
while ($p_price = mysqli_fetch_array($run_price)) {
$foo_id = $p_price['f_id'];
$foo_price = "select * from food where food_id ='$foo_id'";
$run_foo_price = mysqli_query($con, $foo_price);
while ($ff_price = mysqli_fetch_array($run_foo_price)) {
$food_price = array ($ff_price['food_price']);
$food_title = $ff_price['food_title'];
$food_image = $ff_price ['food_image'];
$single_price = $ff_price['food_price'];
$values = array_sum($food_price);
$total += $values;
?>
<?php
if (isset($_POST['update_quantity'])){
$qty = $_POST['qty'];
$update_qty = "update basket set qty='$qty' where f_id = '$foo_id'
and ip_add = ' $ip'";
$run_qty = mysqli_query($con, $update_qty);
$_SESSION['qty'] = $qty;
$total = $values*$qty;
}
?>
<tr align="center">
<td><input type="checkbox" name="remove[]" value="<?php echo $foo_id;?>"
/></td>
<td><?php echo $food_title; ?><br>
<img src="admin/food_images/<?php echo $food_image;?>"width="60"
height="60"/>
</td>
<td><input type="number" size="2" name="qty" style="width:30px"
min="1" max = "99" value ="<?php echo $_SESSION['qty'] ?>" /></td>
<td><?php echo "£".$single_price;?></td>
</tr>
<?php } } ?>
<tr align="right">
<td></td>
<td></td>
<td><b>Sub Total:</b></td>
<td><?php
$_SESSION['total'] = $total;
echo "£". number_format((float)$_SESSION['total'], 2, '.', ''); ?></td>
</tr>
<div id="up">
<td><input type="submit" name="update_basket" value="Remove"></td>
</div>
<div id="up">
<td>
<input name="adjustBtn' . $foo_id . '" type="submit"
value="Update Quantity"/>
<input name="update_quantity" type="hidden" value="<?php echo
$_SESSION['qty'] ?>" />
</td>
</div>
<div id="con">
<td><input type="submit" name="continue" value="Continue Shopping"></td>
</div>
<div id="chck">
<td><a href="checkout.php"><button type="hidden"
name="check">Checkout</button></a></td>
<td>
</div>
</table>
</form>
</body>
</html>
能否请您展示如何使用每个? jquery可以吗?
【问题讨论】:
-
The quantity is displayed in input type = 'number'-- 你的意思是<input value = 'number'>,对吧? -
哎呀,我就是这个意思
标签: php jquery html multidimensional-array foreach