whatitiswhatsup
whatitiswhatsup

Reputation: 1

PHP Display Array in HTML table

I have the database and web server on separate machines, an array from a mysql query is passed to the web server and looks like so

Array (
[user_id] => 1
[username] => phillip
[password] => 12345
[email] => [email protected]
[account_balance] => 100   )

If I print out this array in PHP using print_r($myArray); it shows in the web browser like so

 Array ( [user_id] => 1 [username] => phillip [password] => 12345 [email] => [email protected] [account_balance] => 100 ) 

I want to create PHP code that will iterate this array and make columns for user_id, username, password, email, and account_balance and then display the results in rows.

Here is my PHP code in a file display.php

<!DOCTYPE html>
   <html>
       <head>
           <meta charset="UTF-8">
           <title>Page</title>
       </head>
       <body>
          <table border="1">
<?php
 include(fileThatCallsDBServer.php);
 $myArray = getMYSQLArray(); //This calls a function located in the file in the include() and returns array
 print_r($myArray); //print the array for testing purposes

 //Create table to display array 
 $html = "<table>";
 foreach($myArray as $row) {
    $html .= "<tr>";
       foreach ($row as $cell) {
          $html .= "<td>" . $cell . "</td>";
       }
       $html .= "</tr>";
 }
 $html .= "</table>";
 ?>

       </body>

 </html>

However, nothing gets displayed when I look at the page on my browser. Ideas?

Upvotes: 0

Views: 8150

Answers (4)

Ashu
Ashu

Reputation: 1320

Hi your array is not nested array so you just have to echo it:

echo "<tr><td>".$myArray['user_id']."</td>
          <td>".$myArray['username']."</td>
          <td>".$myArray['password']."</td>
          <td>".$myArray['email']."</td>
          <td>".$myArray['account_balance']."<td></tr>";

Upvotes: 0

Hasan
Hasan

Reputation: 32

<?php
    $data = Array ( 'user_id' => 1,'username' => 'phillip', 'password' => 12345, 'email' => '[email protected]','account_balance' => 100 );
    $keys = array_keys($data);
?>
<table border=1>
    <tr>
        <?php
            foreach($keys as $key=>$value):
            ?>
                <td><?php echo $value;?></td>
            <?php
            endforeach;
        ?>
    </tr>
    <tr>
        <?php
            foreach($data as $key=>$value):
            ?>
                <td><?php echo $value;?></td>
            <?php
            endforeach;
        ?>
    </tr>
</table>

Upvotes: 0

Karim Aljandali
Karim Aljandali

Reputation: 136

Your code looks fine, you just seem to forget to echo the final result of your $html variable.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Page</title>
    </head>
    <body>
        <table border="1">
        <?php
        include(fileThatCallsDBServer.php);
        $myArray = getMYSQLArray(); //This calls a function located in the file in the include() and returns array
        print_r($myArray); //print the array for testing purposes

        //Create table to display array 
        $html = "<table>";
        foreach($myArray as $row) {
            $html .= "<tr>";
            foreach ($row as $cell) {
                $html .= "<td>" . $cell . "</td>";
            }
            $html .= "</tr>";
        }
        $html .= "</table>";
        ?>

        echo $html;

    </body>

</html>

Upvotes: 0

Pupil
Pupil

Reputation: 23948

You need to display keys and their respective values.

So, in array loop, get keys along with values.

You do not need two foreach loops.

...
foreach($myArray as $key => $row) {
 $html .= "<tr>";
 $html .= "<td>" . $key . ': ' . $row . "</td>";
 $html .= "</tr>";
}
...

Upvotes: 1

Related Questions