duanhai4046 2014-04-17 06:19 采纳率: 100%
浏览 14
已采纳

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

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>

I'm trying to display the data in an HTML table (with primary key). I need the subtitle to be entered into the HTML table, only once, but with the if condition I'm not able to achieve it. How can I do that?

  • 写回答

2条回答 默认 最新

  • dp19001 2014-04-17 07:09
    关注

    You may use DISTINCT / GROUP BY in your query for prevention of the redundant value.

    ex.

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

    Reference Site

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP