cheesycarrion Posted November 25, 2007 Share Posted November 25, 2007 this is a really simple thing that I should know how to do but don't. how do I make an html table showing: in the left column the id, and the right column showing the value, from an array? so from an array like this: $bob["name"] = "bob"; $bob["born"] = "2001"; $bob["died"] = "2003"; to an html table like this: <table border="0"> <tr> <td>name</td> <td>bob</td> </tr> <tr> <td>born</td> <td>2001</td> </tr> <tr> <td>died</td> <td>2003</td> </tr> </table> the example is kind of bad, but what I'm trying to do is find out how to use an array from phpbb containing user data. Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 25, 2007 Share Posted November 25, 2007 echo "<tr><td>name</td><td>" . $bob['name'] . "</td></tr>"; echo "<tr><td>born</td><td>" . $bob['born'] . "</td></tr>"; echo "<tr><td>died</td><td>" . $bob['died'] . "</td></tr>"; Quote Link to comment Share on other sites More sharing options...
Whitts Posted November 25, 2007 Share Posted November 25, 2007 While wsantos' code works, you will need to manually add lines for all of bob's attributes. Instead, we can use this: <?php //The assignment of the array happens here echo "<table border='0'>"; foreach ($bob as $name => $value) { echo "<tr><td>" . $name . "</td><td>" . $value . "</td></tr>"; } echo "</table>"; ?> We can use the foreach loop to loop through the array. For the loop, we have assigned $name to the value's name and $value to the actual value. However, depending on how much data you're expecting, you may decide to opt for wsantos' solution. Quote Link to comment Share on other sites More sharing options...
cheesycarrion Posted November 25, 2007 Author Share Posted November 25, 2007 thanks whitts. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.