【问题标题】:php / MySQL - Displaying only one header for itemsphp / MySQL - 仅显示项目的一个标题
【发布时间】:2015-01-10 17:54:23
【问题描述】:

我在一个食品网站上工作,并在显示用户购物车时注意到一个问题,目前表头是按从 mySQL 获取的项目调用的,如下所示:

这是我当前的代码:

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));
            while ($get_row = mysql_fetch_assoc($get)) {
                $sub = $get_row['price']*$value;?>
                <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>
                <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');
}
}

我的问题是我怎样才能让它只显示一次表格标题,为什么还有一个额外的项目单独打印到屏幕底部?

【问题讨论】:

  • 你的循环中有一个开头的&lt;table&gt;,但根本没有结束标签 - 我怀疑这是问题所在,但我认为它不会有帮助。
  • @andrewsi 这有助于移动卡在底部的项目,所以非常感谢:D 我仍然有反复出现的标题问题,但
  • 你的&lt;th&gt; 标签也应该在循环之外——它们也需要被包裹在&lt;tr&gt; 标签中。

标签: php html mysql tableheader


【解决方案1】:

只需将表格和标题标签移出循环即可。

执行以下操作:

<table>
<th></th> #define all table headers

for each item:
   <tr>
     <td>item info</td>...
   </tr>

</table>

【讨论】:

  • 这大部分是正确的,但是请确保您的表格开始标签和表格标题在您的 while 循环之外。 “while 循环”将重复执行相同的功能,直到完成。在 OP 的情况下,它将一遍又一遍地重新创建表格和标题。
  • 我尝试将您列出的标签移到 while 循环之外,但标题仍在重复:(
【解决方案2】:

使用这个

<?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');
}
}

【讨论】:

  • 感谢您的回复,但不幸的是标题仍然重复 :( 真令人沮丧!您知道为什么会发生这种情况吗?
  • 尝试在function cart() {之后移动表格起始标签和其他标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
相关资源
最近更新 更多