0

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";
}
?>
2
  • 1
    you're storing your mysqli results in a session? Commented Aug 4, 2015 at 17:12
  • @trex005 yes, it is stored as a session. Results are correct but the 'num_rows' results is being displayed the amount of times that there records for the query. I want it to show up just once. Want to use something besides 'foreach' but nothing else brings back the query results Commented Aug 4, 2015 at 17:23

2 Answers 2

0

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>";
Sign up to request clarification or add additional context in comments.

Comments

0

How about just

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

No need to loop on an array just to get how many items are in there - that's what count() is for.

2 Comments

so this would take the place of my 'for' statement? Would this pass the rest of the array results to the 2nd table?
why shouldn' it? you're not "passing" anything. you're executing two separate loops, all this would do is completely remote the need to have the first loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.