0

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?

2
  • you need to echo/print out the $html variable... Commented Mar 22, 2019 at 3:57
  • You only concatenated the values, use echo to display the values. You should use echo $html to print the table which you have build Commented Mar 22, 2019 at 3:59

4 Answers 4

1

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

1 Comment

Is $key and $row a custom variable or does it get made up as part of the foreach loop? Right now, that code displays "username: phillip" all in one <tr> it should be a separate column?
0

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>

Comments

0
<?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>

Comments

0

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

Comments

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.