Reputation:
I am trying to create a table using PHP.
For some reason I can't display the key. Any idea why?
Here's the table.
Here's my code that I tried:
<table>
<?php
$users = array(
array('first_name' => 'RS', 'last_name' => 'AD'),
array('first_name' => 'SQ', 'last_name' => 'FS'),
array('first_name' => 'SA', 'last_name' => 'Guillen'),
array('first_name' => 'AS', 'last_name' => 'Gs')
);
$array_number = count($users)-1;
foreach($users as $key => $user) {
for($i=0; $i<=$array_number; $i++){
echo $users[$i]['first_name'].' '. $users[$i]['last_name'].'<br>';
}
}
?>
</table>
Any idea?
Upvotes: 1
Views: 126
Reputation: 11
<table>
<tr>
<th>User #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Full Name in UpperCase</th>
</tr>
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
foreach ($users as $key => $user) {
echo "<tr>";
echo "<td>" , $key + 1 , "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
`
Upvotes: 0
Reputation: 21437
Try this
<table>
<tr>
<th>User #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Full Name in UpperCase</th>
</tr>
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
foreach ($users as $key => $user) {
echo "<tr>";
echo "<td>" , $key + 1 , "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
Brief Explanation:
As you have shown within your image you need to define predefined headings which can be done using <th></th>
and rest of those values you were getting from an $users
array which can be placed within loop.
Note: Here you need to use single loop not nested loops as shown in example
Upvotes: 4
Reputation: 845
<table> <?php
$users = array( array('first_name' => 'Michael', 'last_name' => 'Choi'), array('first_name' => 'John', 'last_name' => 'Supsupin'), array('first_name' => 'Mark', 'last_name' => 'Guillen'), array('first_name' => 'KB', 'last_name' => 'Tonel') );
$i = 1;
foreach($users as $key => $user) {
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "</tr>"; $i++;
}
?>
</table>
Hope that will help
Upvotes: 0
Reputation: 365
You should use <tr>
and <td>
.
<table border="1">
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
$array_number = count($users)-1;
for($i=0; $i<=$array_number; $i++){
?>
<tr> <td><?php echo $users[$i]['first_name']; ?></td><td><?php echo $users[$i]['last_name']; ?></td> </tr>
<?php }
?>
</table>
Upvotes: 0
Reputation: 46549
The problem is that you loop over the array twice, once with the foreach
and inside that with the for
. But you need only one loop. Choose one.
Also, make it a habit to always use htmlspecialchars
when echoing the contents of a variable. In this case it won't matter, but sometimes, you won't know where the variable has been!
Also, see Amol's answer.
Upvotes: 0