消除重复的特定行?

时间:2017-02-04 16:56:15

标签: php html

请帮助!!我只是想显示Barangay,这是没有其他重复值的talojongon 让我们说我目前的输出是:

|    Barangay    |    Disease    |
__________________________________
|   Talojongon   |    Cancer     |
|   Talojongon   |    Cancer     |
|   Talojongon   |    Cancer     |

我希望将其转为:

|    Barangay    |    Disease    |
__________________________________
|   Talojongon   |    Cancer     |
|                |    Cancer     |
|                |    Cancer     |

数据库中的接收是疾病和数据库中的状态是barangay ...谢谢...任何建议表示赞赏.. :)

这是我目前的代码部分

        <br>
        <table id="keywords" class="table table-striped table-bordered">
            <thead>
                <tr>

                    <th><center>Barangay</th></center>
                    <th><center>Diseases</th></center>


                </tr>
            </thead>
            <tbody>
                <?php
                include 'database.php';
                $pdo = Database::connect();
                $sql = 'SELECT * FROM patients WHERE receive="Cancer"';
                foreach ($pdo->query($sql) as $row){
                echo '<tr>';
                echo '<td><center>'. $row['status'] . '</center></td>';
                echo '<td><center>'. $row['receive'] . '</center></td>';

                    }
                    Database::disconnect();
                ?>
                </tr>
             </tbody>

3 个答案:

答案 0 :(得分:0)

            <?php
            include 'database.php';
            $pdo = Database::connect();
            $sql = 'SELECT * FROM patients WHERE receive="Cancer"';
            $counter=0;
            foreach ($pdo->query($sql) as $row){
            $counter++;
            echo '<tr>';
            if($counter==1){
            echo '<td><center>Cancer</center></td>';
            }else{
            echo '<td><center></center></td>';
            }
            echo '<td><center>'. $row['receive'] . '</center></td>';

                }
                Database::disconnect();
            ?>

只需回显第一行的状态......

答案 1 :(得分:0)

$row['status']的最后一个值保存在变量中。如果相同则在输出中留空。

$last_status = null;
foreach ($pdo->query($sql) as $row) {
    echo '<tr>';
    $status = $row['status'] == $last_status ? '' : $row['status'];
    echo '<td style="text-align: center;">' . $status . '</td>';
    echo '<td style="text-align: center;">' . $row['receive'] . '</td>';
    echo '</tr>';
    $last_status = $row['status'];
}

此外,您不应使用过时的<center>标记,请使用CSS。 </tr>应该在循环内,而不是在最后。

您可能还应该在查询中使用ORDER BY status,以便状态相同的所有行都在一起。

答案 2 :(得分:0)

这个怎么样?

<?php
    include 'database.php';
    $pdo = Database::connect();
    $sql = 'SELECT * FROM patients WHERE receive="Cancer" order by status';
    $last = '';
    foreach ($pdo->query($sql) as $row){
        echo '<tr>';
        if($last == $row['status'])
        {
            echo '<td></td>';
        }
        else
        {
            $last = $row['status'];
            echo '<td><center>'. $row['status'] . '</center></td>';
        }
        echo '<td><center>'. $row['receive'] . '</center></td>';
   }
   Database::disconnect();
?>