gpt4 book ai didi

php/MySQL - 如何将 'cart' 表连接在一起

转载 作者:行者123 更新时间:2023-11-29 05:19:27 25 4
gpt4 key购买 nike

我怎样才能把这个表连接在一起,只显示一次表头?我曾尝试将 th 从 while 循环中取出,但我没有运气,也许我犯了一个错误?

到目前为止,这是我的代码:

<?php 
function cart() {
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
<?php while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '<a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a>' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {

if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="200"> <a href="checkout.php"><img src="checkout.png" width="240" height="152"></a>';
} else {
header ('Location: index.php');
}
}

?>

目前这段代码显示:

enter image description here

最佳答案

如下更改脚本。希望它能奏效 $值){

    if ($value>0){
if (substr($name, 0, 5)=='cart_') {

$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
if($i==0)
{
print("
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
</tr>
");
}
$i++;
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '<a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a>' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {

if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="200"> <a href="checkout.php"><img src="checkout.png" width="240" height="152"></a>';
} else {
header ('Location: index.php');
}
}

?>

关于php/MySQL - 如何将 'cart' 表连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com