Amilo Amilo - 1 month ago 7
HTML Question

Inserting array values into a html table

I got an array that holds info that is being queried from two game servers. It looks like this:

Array (
[PERP] => Array (
[gq_address] => 192.168.1.1
[gq_dedicated] => d
[gq_gametype] =>
[gq_hostname] => Test Server 1
[gq_mapname] => gm_construct
[gq_maxplayers] => 60
[gq_mod] => garrysmod
[gq_numplayers] => 11
[gq_online] => 1
[gq_password] => 0
[gq_port] => 27016
[gq_protocol] => source
[gq_transport] => udp
[gq_type] => gmod
)

[TTT] => Array (
[gq_address] => 192.168.1.1
[gq_dedicated] => d
[gq_gametype] =>
[gq_hostname] => Test Server 2
[gq_mapname] => gm_construct
[gq_maxplayers] => 30
[gq_mod] => garrysmod
[gq_numplayers] => 0
[gq_online] => 1
[gq_password] => 0
[gq_port] => 27029
[gq_protocol] => source
[gq_transport] => udp
[gq_type] => gmod
)
)


And then I have a table that shows the information in the array. However, currently it's showing both servers on the same row in the table. The table looks like this:

<?php
echo '<table cellpadding="1" cellspacing="1" border="1">';
echo '<tr>';

foreach($results as $perp) {
echo '<td>' . $perp['gq_type'] . '</td>';
echo '<td>' . $perp['gq_online'] . '</td>';
echo '<td>' . $perp['gq_hostname'] . '</td>';
echo '<td>' . $perp['gq_address'] . '</td>';
echo '<td>' . $perp['gq_port'] . '</td>';
echo '<td>' . $perp['gq_numplayers'] . '</td>';
echo '<td>' . $perp['gq_mapname'] . '</td>';
}

echo '</tr>';
echo '</table>';
?>


So currently it's printing both the "Test Server 1" and "Test Server 2" on the same row in the table. How do I make a table that separates the two arrays on one row each?

Here you can see the table : http://zfrag.se/servers.php

Answer

Like this:

<?php
echo '<table cellpadding="1" cellspacing="1" border="1">';

foreach($results as $perp) { 
    echo '<tr>';
    echo '<td>' . $perp['gq_type'] . '</td>';
    echo '<td>' . $perp['gq_online'] . '</td>';
    echo '<td>' . $perp['gq_hostname'] . '</td>';
    echo '<td>' . $perp['gq_address'] . '</td>';
    echo '<td>' . $perp['gq_port'] . '</td>';
    echo '<td>' . $perp['gq_numplayers'] . '</td>';
    echo '<td>' . $perp['gq_mapname'] . '</td>';
    echo '</tr>';
}

echo '</table>';
?>