如果行数据在使用PHP和MySQL的列中相同,我怎么能停止重复
问题描述:
Here is my code. I am newer in programming. If I made any mistake please help me to solve it. Thanks.
<table class="table table-striped" style="width:1300px; font-size:30px;" border="1px solid black">
<tr>
<th>Reference No</th>
<th>Category</th>
<th>Total Price</th>
</tr>
<?php
include("../db/db.php");
$sql_cost_details="select b.* from lc_details a, cost_details b where b.reference_no=a.reference_no AND a.lc_details_no='$lc_no' order by reference_no ASC";
$run_sql=mysqli_query($con, $sql_cost_details);
$i=0;
$total=0;
while($row_cost_details=mysqli_fetch_array($run_sql)){
$reference_no=$row_cost_details['reference_no'];
$category=$row_cost_details['category'];
$total_price=$row_cost_details['value'];
$i++;
?>
<tr align="center">
<td><?php echo $reference_no; ?></td>
<td><?php echo $category; ?></td>
<td><?php echo $total_price; ?></td>
</tr>
<?php } ?>
</table>
答
Use the below which solve your problem:
<table class="table table-striped" style="width:1300px; font-size:30px;" border="1px solid black">
<tr>
<th>Reference No</th>
<th>Category</th>
<th>Total Price</th>
</tr>
<?php
include("../db/db.php");
$sql_cost_details="select b.* from lc_details a, cost_details b where b.reference_no=a.reference_no AND a.lc_details_no='$lc_no' order by reference_no ASC";
$run_sql=mysqli_query($con, $sql_cost_details);
$i=0;
$total=0;
$prev = '';
while($row_cost_details=mysqli_fetch_array($run_sql)){
$reference_no=$row_cost_details['reference_no'];
$category=$row_cost_details['category'];
$total_price=$row_cost_details['value'];
$i++;
?>
<tr align="center">
<td><?php if ($prev != $reference_no){ echo $reference_no; } ?></td>
<td><?php echo $category; ?></td>
<td><?php echo $total_price; ?></td>
</tr>
<?php
$prev = $row_cost_details['reference_no'];
} ?>
</table>