PHP / MySQL的 - 显示项目只有一个标题 [英] php / MySQL - Displaying only one header for items

查看:121
本文介绍了PHP / MySQL的 - 显示项目只有一个标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个食品网站上工作,并且在显示用户购物车时发现了一个问题,当前表头是从mySQL中取出的每个项目,这看起来像这样:






  function cart(){
foreach($ _ SESSION as $ name => $ $ b $ 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>删除项目< / th>
< th>项目名称< / th>
项目价格< / th>
< th>数量< / th>
行总数< / 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>' 。$值。 '< 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.pngwidth =240height = 152\" >< / A> < img src =spacer.pngwidth =200> < a href =checkout.php>< img src =checkout.pngwidth =240height =152>< / a>';
}
else {
header('Location:index.php');


$ / code>

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

解决方案

循环外的表格和标题标签。



做一些事情:

 < table> 
< th>< / th> #定义每个项目的所有表头


< tr>
< td>商品信息< / td> ...
< / tr>

< / table>


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?

解决方案

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>

这篇关于PHP / MySQL的 - 显示项目只有一个标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆