如果行数据在使用PHP和MySQL的列中相同,我怎么能停止重复

时间:2015-12-11 13:06:59

标签: php mysql

这是我的代码。我是编程新手。如果我犯了任何错误,请帮我解决。 感谢。

<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>

它显示了这种类型的结果 Current result

预期结果如下: Want like this

1 个答案:

答案 0 :(得分:1)

使用以下解决问题的方法:

<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>