【问题标题】:Need to Display the Results of MySQLi num_rows in Separate Table from Array Results需要在单独的表中显示 MySQLi num_rows 的结果与数组结果
【发布时间】:2015-08-04 17:09:56
【问题描述】:

我有一个显示 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";
}
?>

【问题讨论】:

  • 您将 mysqli 结果存储在会话中?
  • @trex005 是的,它存储为会话。结果是正确的,但“num_rows”结果显示的次数与查询记录的次数相同。我希望它只显示一次。想要使用除“foreach”之外的其他东西,但没有其他东西可以返回查询结果

标签: php arrays mysqli echo mysql-num-rows


【解决方案1】:

改变

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

【讨论】:

    【解决方案2】:

    怎么样

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

    无需为了获取其中有多少项目而在数组上循环 - 这就是 count() 的用途。

    【讨论】:

    • 所以这会代替我的“for”声明?这会将其余的数组结果传递给第二个表吗?
    • 为什么不应该呢?你没有“通过”任何东西。您正在执行两个单独的循环,所有这些都完全不需要第一个循环。
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多