Show table in sql

0

How can I go through the users variable to show me the table:

<?php

require_once 'database.php';



$database_connection = database_connect();

$users = $database_connection->query('SELECT * FROM tabla')->fetchAll();




$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>
    <tr>
        <td></td>
    </tr>

</table>
';
include 'Template.php';
?>
    
asked by Perl 11.09.2016 в 12:05
source

2 answers

3

According to the php documentation :

  

Return values

     

PDOStatement :: fetchAll () returns an array that contains all the remaining rows of the result set. The array represents each row as an array with values of the columns, or as an object with properties corresponding to each column name. An empty array is returned if there are zero results to obtain, or FALSE in case of failure.

Therefore a simple foreach ( see documentation ) can be traced:

<?php

// ...

$users = $database_connection->query('SELECT * FROM tabla')->fetchAll();

foreach ($users as $user) { ?>
    <td><?php echo $user['name']; ?></td>
    <td><?php echo $user['phone']; ?></td>
<?php }
    
answered by 11.09.2016 в 13:39
2

The problem is not in the foreach, but rather in how the template is being made and how to put <?php .... ?> within the chain "breaks" the page:

<?php

...

$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>

    <tr>
        <?php foreach($users as $user): ?>
        <td><?php echo $user["id"]?></td>
        <?php endforeach;?>
    </tr>


</table>
';

(you can see how even the StackOverflow code formatter colors the foreach part differently after the first ?> )

To solve this we should know what Template.php does (do you use any framework?) and find out how to create a loop with data inside it ... or simply generate the string directly before passing it to the Template (which will be much simpler and should work without problems):

$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>

    <tr>';


foreach($users as $user) {
    $content = $content . '<td>' . $user["id"] . '</td>';
}


$content .= '</tr>


</table>
';

The final code would be like this (the foreach will not be needed at the end because it will have been fixed before).

<?php

require_once 'database.php';



$database_connection = database_connect();

$users = $database_connection->query('SELECT id FROM coffee')->fetchAll();


$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>

    <tr>';

foreach($users as $user) {
    $content = $content . '<td>' . $user["id"] . '</td>';
}

$content .= '</tr>


</table>
';

include 'Template.php';
      ?>
    
answered by 11.09.2016 в 15:22