将数组值插入到html表中

时间:2013-05-12 18:41:09

标签: php html arrays html-table

我有一个数组,其中包含从两个游戏服务器查询的信息。它看起来像这样:

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  
    ) 
)

然后我有一个表格,显示数组中的信息。但是,目前它在表中的同一行显示两个服务器。该表如下所示:

<?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>';
?>

所以目前它正在打印表中同一行的“Test Server 1”和“Test Server 2”。如何创建一个表格,分别在一行上分隔两个数组?

您可以在此处查看表格:http://zfrag.se/servers.php

1 个答案:

答案 0 :(得分:4)

像这样:

<?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>';
?>