如何避免在html表中打印重复值

时间:2014-04-17 06:19:58

标签: php mysql

Order.php:

<?php
    require_once('conn.php');
    session_start();
    $itemId=$_SESSION['itemId'];
    $tnumber=$_SESSION['tnumber'];
    $custno=$_SESSION['custno'];

    $sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
    $sql2="select subtitle from cart where itemId='$itemId'";
    $res2=mysqli_query($dbhandle,$sql2);
    $row1=  mysqli_num_rows($res2);
    $res=mysqli_query($dbhandle,$sql);
    $total=0;
?>
<html>
    <head>
        <title>Home</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type='text/css' href='cartbox.css'/>
    </head>
    <body>
        <h1></h1>
    <script src="home.js"></script>
        <div id="shopping-cart"> </div>
        <?php
            echo "<table id='t1' border='1'>
                <th>Subtitle</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Amount</th>
                </tr>";   
            if (row1 > 0) {
                while ($row=mysqli_fetch_array($res)) {
                    $amount=$row['price']*$row['quantity'];
                    echo "<form id='addform{$row['itemId']}' method='post'   action='cartdelete.php'> ";
                    echo "<tr>";
                    echo "<td>" .$row['subtitle'] ."</td>";
                    echo "<td>" .$row['quantity'] ."</td>";
                    echo "<td>" .$row['price'] ."</td>";
                    echo "<td>" . $amount . "</td>";
                    echo "<td><input type='submit' value='x' name='submit'></td>";
                    echo "<td><input type='text' name='itemId'  value= '{$row['itemId']}'></td>";   
                    echo"</form>";
                    echo "</tr>";
                    $total = $total+ $amount;
                 }
             }
             echo "</table>";     
         ?>
         <?php echo $total ?> 
         <input type="button" name="continue" value="Continue Order" style="position: absolute;top:200px;" onclick="location.href='customerdicecream.php'">
    </body>
</html>

我正在尝试在HTML表格中显示数据(使用主键)。我需要将字幕输入到HTML表中,只需输入一次,但是if条件我无法实现它。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

$prevSubTitle条件之前定义变量while ($row=mysqli_fetch_array($res)) {

当您从$row['subtitle']读取值时,请检查它是否与先前读取的值匹配。如果'false'显示新值,则填充td,空格为<td> &nbsp; </td>

@prevSubTitle = '';
while ($row=mysqli_fetch_array($res)) {
    $amount=$row['price']*$row['quantity'];
    echo "<form id='addform{$row['itemId']}' method='post'   action='cartdelete.php'> ";
    echo "<tr>";

    $subtitle = $row['subtitle'];
    if( ! ( $subtitle == $prevSubTitle ) ) {
       $prevSubTitle = $subtitle;
    }
    else {
       $subtitle = ' &nbsp; ';
    }
    echo "<td>" . $subtitle ."</td>"; // empty when matched with previous row title

    echo "<td>" .$row['quantity'] ."</td>";
    echo "<td>" .$row['price'] ."</td>";

答案 1 :(得分:0)

您可以在查询中使用DISTINCT / GROUP BY来防止冗余值。

离。

$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
    $sql2="select DISTINCT subtitle from cart where itemId='$itemId'";

Reference Site