Snipey Snipey - 6 months ago 21
PHP Question

How to display multidimensional array data in a table

array(3) {
[0]=>
array(3) {
["name"]=>
string(6) "Blobby"
["avatar"]=>
string(116) "url"
["balance"]=>
string(7) "8289061"
}
[1]=>
array(3) {
["name"]=>
string(6) "Snipey"
["avatar"]=>
string(116) "url"
["balance"]=>
string(6) "208841"
}
[2]=>
array(3) {
["name"]=>
string(4) "Xg45"
["avatar"]=>
string(116) "url"
["balance"]=>
string(5) "20509"
}
}


I am passing this data from a mysql query to a variable and i was wondering how would i display the data in a table.

foreach ($leader as $key => $val) {
if ($key != 'submit') {
echo "<tr>" . $key['avatar'] . "</tr>";
echo "<tr>" . $key['name'] . "</tr>";
echo "<tr>" . $key['balance'] . "</tr>";
}
}


This is what I was trying to use but it was only displaying the first letter in each key.

Answer

<tr> does not display anything but a table row <tr> with a table data <td> in will

Also the data is in the $val variable and not the $key variable

foreach ($leader as $key => $val) {
    echo '<tr>';
    echo '<td>' . $val['avatar'] . "</td>";
    echo "<td>" . $val['name'] . "</td>";
    echo "<td>" . $val['balance'] . "</td>";
    echo '</tr>';
}

Also the if ($key != 'submit') { line is redundant as you have a numerically indexed array so $key could never be == 'submit'

Comments