PHP "for each" print in HTML table

Hello I have a PHP function that returns an array of values. Besides this I have a HTML page with PHP inside where I would like to call the function and print values inside a HTML table.
The code looks like this:

<table class="table table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Surname</th>
              <th>DOB</th>
              <th>Address</th>
              <th>E-mail</th>
              <th>Phone</th>
              <th>Gender</th>
              <th>Subscription</th>
              <th>Acc. Balance</th>
            </tr>
          </thead>
          <tbody>

            <?php 
            require_once ('database_initialisation.php');
            $table = "members"; 
            $array = $admin_query->viewTableData($table);
            foreach ($array as $row){
            print_r($array); 
            ?>
            
            <tr>
             <td> <?php $row[member_id]  ?> </td>
             <td> <?php $row[member_password]  ?> </td>
             <td> <?php $row[member_first_name]  ?> </td>
             <td> <?php $row[member_last_name] ?> </td>
             <td> <?php $row[member_DOB]  ?> </td>
             <td> <?php $row[member_address]  ?> </td>
             <td> <?php $row[member_email]  ?> </td>
             <td> <?php $row[member_phone]  ?> </td>
             <td> <?php $row[member_gender]  ?> </td>
             <td> <?php $row[member_subscription]  ?> </td>
             <td> <?php $row[member_account_balance]  ?> </td>

            </tr>

            <?php  } ?>

          </tbody>
        </table>

The print_r is just to see if I can get the actual array, which prints fine the array. Also within the table having the for each, it does generate the number of rows that are needed but it does not put any values inside the TD
Any suggestions would be much appreciated? Thanks

I managed to get it working doing it the other way arround. I am not sure if it the right way to do it, but it works.

<?php 
            require_once ('database_initialisation.php');
            $table = "members"; 
            $array = $admin_query->viewTableData($table);
            foreach ($array as $row)
            {

            echo 
                "<tr>
                  <td>" . $row['member_id'] . "</td>
                  <td>" . $row['member_password'] . "</td>
                  <td>" . $row['member_first_name'] .  "</td>
                  <td>" . $row['member_last_name'] .  "</td>
                  <td>" . $row['member_DOB'] .  "</td>
                  <td>" . $row['member_address'] .  "</td>
                  <td>" . $row['member_email'] .  "</td>
                  <td>" . $row['member_phone'] .  "</td>
                  <td>" . $row['member_gender'] .  "</td>
                  <td>" . $row['member_subscription'] .  "</td>
                  <td>" . $row['member_account_balance'] .  "</td>

                </tr>";

            } ?>

Or inside each <td> <?php $row[member_account_balance] ?> </td> you could use ‘echo’.

For example,

<td> <?php echo $row[member_account_balance];  ?> </td>

You were close with your first attempt, and if you look closely at your working version you can see a subtle difference on every line:

$row[member_account_balance]

VS

$row['member_account_balance']

… missing the single-quotes around the array key strings in the first example.

Do you have error reporting enabled? It is important to use the tools at your disposal, including error reporting.

A suggestion unrelated to your problem statement, move your database connection stuff to the top of the document. It makes code easier to maintain if you separate the data-gathering and preparation from the output/markup generation. At the top of your file, get your data 100% ready for output. Down in the HTML markup, try to limit yourself to simple function calls, if, and foreach as much as you can. In a more complex or mature project, you usually see these things happening in totally different files, functions, and/or classes. This is called “separation of concerns”, and even in a small one-file project, it good practice. If nothing else, it makes it easier on you in the future.

1 Like

Thanks for the suggestions :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.