Php 如何避免在html表中打印重复值
Order.php:Php 如何避免在html表中打印重复值,php,mysql,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='$tnu
<?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>
在while($row=mysqli_fetch_数组($res)){
条件之前定义一个变量$prevstitle
当您从$row['subtitle']
读取它的值时,请检查它是否与以前读取的值匹配。如果'false'
显示新值,则在td
中填充一个空格作为
@prevSubTitle='';
while($row=mysqli\u fetch\u数组($res)){
$amount=$row['price']*$row['quantity'];
回声“;
回声“;
$subtitle=$row['subtitle'];
如果(!($subtitle==$prevSubTitle)){
$prevSubTitle=$subtitle;
}
否则{
$subtitle='';
}
echo“$subtitle.”;//与上一行标题匹配时为空
回显“$row['quantity']”;
回显“$row['price']”;
您可以在查询中使用DISTINCT
/GROUP BY
,以防止冗余值
前
使用按子标题分组
…在您的查询中…此外,mysql_*函数已被弃用,您不应该使用它们。请改用mysqli或PDO。您能否在if条件$row1>0
中为查询提供缺少$符号,而不是row1>0
对不起,这是一个印刷错误@pred对不起,回复太晚了,此代码没有s工作,即使现在我也能在html表格中插入两个相同的字幕
@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 = ' ';
}
echo "<td>" . $subtitle ."</td>"; // empty when matched with previous row title
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$sql2="select DISTINCT subtitle from cart where itemId='$itemId'";