1

私は食品の Web サイトに取り組んでおり、ユーザーのカートを表示するときに問題に気付きました。現在、テーブル ヘッダーは 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');
}
}

私の質問は、テーブル ヘッダーを 1 回だけ表示する方法と、画面の下部だけに追加の項目が表示される理由です。

4

2 に答える 2

2

table と header タグをループの外に移動するだけです。

次のようにします。

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

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

</table>
于 2015-01-10T18:02:19.137 に答える
0

これを使って

<?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');
}
}
于 2015-01-10T19:15:42.073 に答える