如何在单个表单元格中分组和显示多个结果集行值?

时间:2018-08-05 16:35:41

标签: php html mysqli

<?php
include("functions/functions.php");
$con=mysqli_connect("localhost", "root", "", "db_stvs");
$query="select * from tbl_preset_position where pp_status='Active'";
$query1="select distinct pp_name from tbl_preset_position where pp_status='Active'";
$res=mysqli_query($con, $query);
$res1=mysqli_query($con, $query1);
?>
<button class="btn btn-primary">Back</button>
<button class="btn btn-primary" data-toggle="modal" data-target="#addModal">Add Position Preset</button>

<table class="table-bordered">
    <th>Preset Name</th>
    <th>Positions Available</th>
    <?php
    while ($row1 = mysqli_fetch_assoc($res1)) {
    ?>
        <tr>
            <td>
                <?php echo $row1['pp_name'];?>
            </td>
            <td>
            <?php
            while ($row= mysqli_fetch_assoc($res)) {
                if($row['pp_name']==$row1['pp_name']) {
                    echo $row['pp_position']."<br>";
                }
                ?>
                <?php
            }
            echo "</td></tr>";
    }
    ?>
</table>

我可以显示第一行及其位置,但是当涉及其他行时,则没有数据显示。

我只是想在正确的预设名称上显示特定位置。 例如:-

" Sample preset has 3 Positions 
"Sample Preset| President, Vice PResident" 
"Sample Preset 2| Supremo, Ala Supremo"

1 个答案:

答案 0 :(得分:1)

最明智的做法是,在查询中使用GROUP BYGROUP_CONCAT()来完全准备结果集。

类似这样的东西:(未经测试)

SELECT pp_name, GROUP_CONCAT(pp_position SEPARATOR '<br>') AS pp_position
FROM tbl_preset_position
WHERE pp_status = 'Active'
GROUP BY pp_name
ORDER BY pp_name

然后,您可以正常地将数据回显到表中。


否则,您将需要使用复杂的php来确定哪些行应附加数据,哪些行可以独立...

现在,我尚未测试此代码,但我相信条件逻辑应该成立。您只需要ORDER BY pp_name,然后在迭代结果集时确定是否要显示该组中的第一个。

我将OO语法用于查询函数,因为它不太冗长。

<?php
include("functions/functions.php");

echo '<button class="btn btn-primary">Back</button>';
echo '<button class="btn btn-primary" data-toggle="modal" data-target="#addModal">Add Position Preset</button>';

if (!$con = new mysqli("localhost", "root", "", "db_stvs")) {
    echo 'Connect failed: ', $con->connect_error);  // never display errors to the public
} elseif (!$result = $con->query("SELECT pp_name, pp_position FROM tbl_preset_position WHERE pp_status = 'Active' ORDER BY pp_name")) {
    echo 'Syntax Error', $con->error;  // never show the exact error message to the public
} elseif (!$result->num_rows) {
    echo 'No Active Records Found';
} else {
    echo '<table class="table-bordered">';
        echo '<tr><th>Preset Name</th><th>Positions Available</th></tr>';
        $name = null;                                                            // establish a default value that won't be matched
        while ($row = $result->fetch_assoc()) {
            if ($row['pp_name'] !== $name) {
                if ($name !== null) {
                    echo '</td></tr>';
                }
                echo "<tr><td>{$row['pp_name']}</td><td>{$row['pp_position']}";  // write row for pp_name group
            } else {
                echo "<br>{$row['pp_position']}";                                // append all subsequent values in group
            }
            $name = $row['pp_name']; // update temporary variable
        }
        echo '</td></tr>';
    echo '</table>';
}