php / MySQL - Displaying only one header for items

杀马特。学长 韩版系。学妹 提交于 2019-12-24 14:01:06

问题


I am working on a food website and have noticed an issue when I display the users cart, currently the table header is called per item which is taken from mySQL, which looks like this:

Here is my current code:

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

My question is how can I get it to display the table header only once and also why is there an additional item being printed to the bottom of the screen alone?


回答1:


Just move the table and header tags outside of the loop.

Do something like:

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

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

</table>



回答2:


use this

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


来源:https://stackoverflow.com/questions/27879328/php-mysql-displaying-only-one-header-for-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!