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

时间:2021-11-27 21:44:15

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?

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

2 个解决方案

#1


0  

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

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

ex.

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

Reference Site

#2


0  

Define a variable $prevSubTitle before the while ($row=mysqli_fetch_array($res)) { condition.

在while之前定义一个变量$ prevSubTitle($ row = mysqli_fetch_array($ res)){condition。

And when you read value for it from $row['subtitle'], check if it matches with previously read value. If 'false' display new value, else fill td with a space as <td> &nbsp; </td>

当你从$ row ['subtitle']中读取它的值时,检查它是否与先前读取的值匹配。如果'false'显示新值,则使用空格填充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  

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

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

ex.

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

Reference Site

#2


0  

Define a variable $prevSubTitle before the while ($row=mysqli_fetch_array($res)) { condition.

在while之前定义一个变量$ prevSubTitle($ row = mysqli_fetch_array($ res)){condition。

And when you read value for it from $row['subtitle'], check if it matches with previously read value. If 'false' display new value, else fill td with a space as <td> &nbsp; </td>

当你从$ row ['subtitle']中读取它的值时,检查它是否与先前读取的值匹配。如果'false'显示新值,则使用空格填充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>";