我正在创建一个订单购物车。PHP - 如何做到这一点,如果?
在显示购物车的页面上,它检查存储在会话$order
中的值是否与mysql表中的某一行的id相对应。如果该匹配存在,则返回相应的行。
在此过程中,我试图检索会话$quantity
中存储的与表中行的id对应的数量值。
$order
和$quantity
中的每个值都分配了一个名称,它是从中添加的项目的ID。
这是增加了为了将购物车的代码:
if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$_POST['id']] = $_POST['id'];
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity'];
header('Location: .');
exit();
}
这是购物车页上的代码:
foreach ($order as $item)
foreach ($quantity as $amount)
{
mysql_data_seek($productsSql, 0); //<- this line, to reset the pointer for every EACH.
while($row = mysql_fetch_assoc($productsSql))
{
$itId = $row['id'];
$itDesc = $row['desc'];
$itPrice1 = $row['price1'];
if ($item == $itId)
{
$pageContent .= '
<tr>
<td>'.$itDesc.'</td>
<td>'.if ($item[''.$itId.''] == $amount[''.$itId.'']) {echo $amount}.'</td>
<td>R'.number_format($itPrice1*$amount, 2).'</td>
</tr>
';
}
}
}
此行产生语法错误:
<td>'.if ($item[''.$itId.''] == $amount[''.$itId.'']) {echo $amount}.'</td>
这里的初学者有什么问题?其次,我需要怎样做才能完成我面临的任务?
对此的任何输入将不胜感激!
虽然这工作被添加到购物车中的第一项。当第二件物品被添加到购物车中时,这些物品会被重复,例如,应该有两个物品,其中有四个物品,这些物品的价值混合了吗? –
添加var转储时,$ $ itId的值为:string(1)“3”string(1)“3”string(1)“6”string(1)“6” –
您可以尝试使用$ item [intval($ itId)]? – Lordalcol