I need to create an HTML table consisting of three columns: email, name, last name within my loop.
Could anyone give a hint how I can start creating?
$i=0;
foreach($result as $r) {
echo "<pre>";
print_r( $i ." ". $r['aluno_email'] ."". " | ". $r['aluno_nome']
. " " .
$r['aluno_sobrenome'] . " | ". strtolower(trim(($r['aluno_nome'])))."_".strtolower(trim($r['aluno_sobrenome'])));
echo "<pre>";
$i++;
}
Try this:
<?php
$i = 0;
echo '<table>';
foreach ($result as $r) {
echo "<tr><td>{$i}</td><td>{$r['aluno_email'}</td><td>{$r['aluno_nome']}</td></tr>";
$i++;
}
echo '</table>';
Loop through the results, each result outputs one row
echo '<table>';
foreach ($results as $r) {
echo sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $r['email'], $r['name'], $r['last_name']);
}
echo '</table>';
Create the table and table header outside the loop.
Populate the table body in the loop, and close the loop.
<table>
<tr>
<th>Email</th>
<th>Name</th>
<th>Last Name</th>
</tr>
<?php
$i = 0;
foreach ($result as $r) {
echo "<tr>";
echo "<td>" . $r['aluno_sobrenome'] . "</td><td>" . strtolower(trim(($r['aluno_nome']))) . "</td><td>" . strtolower(trim($r['aluno_sobrenome'])) . "</td>";
echo "</tr>";
$i++;
}
?>
</table>
this is how you do it
echo "<table>
<tr><th>Email</th><th>Name</th><th>LastName</th></tr>";
foreach($result as $val){
echo "<tr><td>".$val['email']."</td><td>".$val['name']."</td><td>".$val['lastName']."</td><td>"</tr>";
}
echo "</table>";
I usually start by checking for results, then build the table:
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Email</th>
<th>Name</th>
<th>LastName</th>
</tr>";
I run my loop and output as long as I have results:
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["Email"]. "</td>
<td>" . $row["Name"]. "</td>
<td>" . $row["LastName"]. "</td>
</tr>";
}
echo "</table>";