Search code examples
phparraysmysqliechomysql-num-rows

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


I have a page that displays a MySQLi query result array. On this page the data comes to the page correctly but for my $result['rows'] which is the result of num_rows of the array results are showing up for each record and then the array results are being displayed. For example if there are 100 records to the query there is '100' displayed 100 times before the array results. I know it's because I'm using foreach but I can't get it work with anything else. I've tried while, for, and continue but nothing works but foreach to retrieve the data correctly. What am I missing?

Here is the code:

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

Solution

  • Change

    foreach($_SESSION['results'] as $result) {
    
        echo "<tr>";
        echo "<td>{$result['rows']}</td>";
        echo "</tr>";
    
    }
    

    to:

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