需要在数组结果中显示独立表中的MySQLi num_rows的结果

时间:2015-08-04 17:09:57

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

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

2 个答案:

答案 0 :(得分:0)

如何

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

不需要在数组上循环只是为了得到那里有多少项 - 这就是count()的用途。

答案 1 :(得分:0)

更改

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