需要将 MySQLi num_rows 的结果与 Array Results 分开显示 Table

Need to Display the Results of MySQLi num_rows in Separate Table from Array Results

我有一个显示 MySQLi 查询结果数组的页面。在此页面上,数据正确进入页面,但对于我的 $result['rows'],这是数组结果的 num_rows 的结果,每个记录都显示出来,然后显示数组结果。例如,如果查询有 100 条记录,则“100”会在数组结果之前显示 100 次。我知道这是因为我正在使用 foreach 但我无法让它与其他任何东西一起使用。我尝试了 whileforcontinue,但除了 foreach 之外没有任何效果可以正确检索数据。我错过了什么?

代码如下:

<?php
            if($results) {
    echo "<table id='test' class='tablesorter' border='2'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th class='header'># of Records</th>";
    echo "</tr>";
    echo "</thead>";
    echo "<tbody>";

        foreach($_SESSION['results'] as $result) {

                echo "<tr>";
                echo "<td>{$result['rows']}</td>";
                echo "</tr>";

            }
            echo "</tbody>";
            echo "</table>";
            }
            ?>

            <?php
            if($results) {
    echo "<table id='test' class='tablesorter' border='2'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th class='header'>Date Set</th>";
    echo "<th class='header'>Branch</th>";
    echo "<th class='header'>Appointment Date</th>";
    echo "<th class='header'>Employee</th>";
    echo "<th class='header'>First Name</th>";
    echo "<th class='header'>Last Name</th>";
    echo "<th class='header'>Last Four</th>";
    echo "<th class='header'>Phone</th>";
    echo "<th class='header'>City</th>";
    echo "<th class='header'>State</th>";
    echo "<th class='header'>Zip</th>";
    echo "</tr>";
    echo "</thead>";
    echo "<tbody>";


            foreach($_SESSION['results'] as $result) {

                echo "<tr>";
                echo "<td>{$result['set_date']}</td>";
                echo "<td>{$result['branch']}</td>";
                echo "<td>{$result['appt_date']}</td>";
                echo "<td>{$result['employee']}</td>";
                echo "<td>{$result['fname']}</td>";
                echo "<td>{$result['lname']}</td>";
                echo "<td>{$result['last_four']}</td>";
                echo "<td>{$result['phone']}</td>";
                echo "<td>{$result['city']}</td>";
                echo "<td>{$result['state']}</td>";
                echo "<td>{$result['zip']}</td>";
                echo "</tr>";

            }
            echo "</tbody>";
            echo "</table>";            

}else{

    echo "No Records Found";
}
?>

怎么样

$cnt = count($_SESSION['results'];
$s = ($cnt == 1) ? '' : 's';
echo "<th class='header'><?php echo $cnt ?> Record<?php echo $s ?></th>";

无需循环数组来获取其中有多少项 - 这就是 count() 的用途。

改变

foreach($_SESSION['results'] as $result) {

    echo "<tr>";
    echo "<td>{$result['rows']}</td>";
    echo "</tr>";

}

至:

echo "<tr>";
echo "<td>{$_SESSION['results'][0]['rows']}</td>";
echo "</tr>";