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

标签 php mysql

订单.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 条件下我无法实现它。我该怎么做?

最佳答案

您可以在查询中使用 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

关于php - 如何避免在 html 表中打印重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23125942/

相关文章:

php - 如何从 Controller 获取两个对象结果以在 Laravel5 中查看

php - 从 MySQL 获取信息

php - 我无法使用 PHP 插入 MySQL(没有显示错误)

PHP : LOAD DATA LOCAL INFILE forbidden

php - MySQL 查询 - 获取列值 "greater than..."

php - 如何在 Symfony 4 服务中使用 renderView 和 twig 模板

mysql - 连接到第二个表,其中某个字段的字符出现次数最多

php - Apache 抛出 "Parse error: syntax error, unexpected ' 类'”

mysql - 在特定日期执行触发器

php - Codeception,使用 pageObject 设计模式和小 cucumber 编写验收测试