Jump to content

[SOLVED] very very simple array question


cheesycarrion

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.