php / MySQL - 如何加入' cart'一起吃饭

时间:2015-01-10 23:38:31

标签: php mysql html-table

如何将此表连接在一起,仅显示一次标题?我已经尝试过了一段时间,但我没有运气,也许我犯了错误?

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

<?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

2 个答案:

答案 0 :(得分:2)

更改脚本如下。希望它会奏效          $ 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));?>
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');
    }
    }

    ?>

答案 1 :(得分:1)

下面是更正的代码,表的创建移出循环。

此外,表头已添加到tr中以创建有效的html,并且还将数据收集到$ output变量中以防止您的位置重定向错误,因为数据已经写入浏览器(您可能仍然存在差异)这是一个问题,因为在调用此cart()函数之前可能还有其他输出。

<?php 
function cart() {

    $output = '';
    $output .= '<center>';
    $output .= '<table class='menufinal' border=0 width=75%>';
    $output .= '<tr>';
    $output .= '<th>Remove Item</th>';
    $output .= '<th>Item Name</th>';
    $output .= '<th>Item Price</th>';
    $output .= '<th>Quantity</th>';
    $output .= '<th>Line Total</th>';
    $output .= '</tr>';

    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;
                    $output .= '<tr>';
                    $output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
                    $output .= '<td>' . $get_row['name'] . '</td>';
                    $output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
                    $output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
                    $output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
                    $output .= '</tr>';
                }
            } 
            if (empty($total)) {

                if (empty($sub)) {
                    //do nothing
                } else {
                    $total = $sub;
                }
            } else {
                $total += $sub;
            }
        }
    }

    $output .= '</table>';

    if (empty($total)){
        header ('Location: index.php');
        exit;    
    }

    $output .=  '<br>Total: &pound' . number_format($total, 2) . '<br>';
    $output .=  '<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>';

    echo $output;
}

?>