-2

I know this question is repeated, but I cannot follow the examples for my situation. I'm querying Quake 2 servers for a personal project

 foreach ($results as $data) {
         echo "<pre>";
         print_r($data['players']);
    }

the info obtained:

[0] => Array
    (
        [frags] => 7
        [ping] => 28
        [nick] => Player
        [gq_name] => Player
        [gq_score] => 7
        [gq_ping] => 28
    )

[1] => Array
    (
        [frags] => 27
        [ping] => 31
        [nick] => lE'Heineken.
        [gq_name] => lE'Heineken.
        [gq_score] => 27
        [gq_ping] => 31
    )

)

So how can I move this info in a simple table?

<table>
<tr>
    <th>Players</th>
    <th>Frags</th>
    <th>Ping</th>
</tr>
<tr>
    <td></td>
    <td></td>
    <td></td>
</tr>

I'm trying to do something like q2servers

Thanks for your time, and sorry for my english. Regards

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    Possible duplicate of [How to create a HTML Table from a PHP array?](http://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array) –  Oct 15 '15 at 03:05

3 Answers3

0

I don't really what you need, but if I understood well, what you need is:

<table>
<tr>
    <th>Players</th>
    <th>Frags</th>
    <th>Ping</th>
</tr>
<?php foreach ($results['players'] as $data) {
<tr>
    <td><?php echo $data['gq_name']; ?></td>
    <td><?php echo $data['gq_frags']; ?></td>
    <td><?php echo $data['ping']; ?></td>

</tr>
<?php } ?>
</table>
Bak
  • 262
  • 1
  • 4
  • 11
  • I applied this example for the data server info (hostname, mapname, number of players) and works fine, but the player info is not displaying, cells of the table are empty, any thoughts? – Esteban Aliaga Oct 15 '15 at 02:37
  • try changing $results as $data, for $results['players'] as $data – Bak Oct 15 '15 at 02:50
  • Players Frags Ping Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\new.php on line 118 I dont know what is wrong there – Esteban Aliaga Oct 15 '15 at 15:52
  • what if you do $results['players'][0] as $data – Bak Oct 15 '15 at 16:03
  • "Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\new.php on line 97" and the info from the print_r: Array ( [0] => Array ( [frags] => 0 [ping] => 5 [nick] => WallFly[BZZZ] [gq_name] => WallFly[BZZZ] [gq_score] => 0 [gq_ping] => 5 ) ) (there is one player online now) – Esteban Aliaga Oct 15 '15 at 16:11
  • ok you have to show what is in $results, that way I can see how is the array format – Bak Oct 15 '15 at 16:22
  • my index: require_once 'GameQ.php'; in gameq.php : $results = array(); foreach ($servers as $sid => $server) { $results[$sid] = array(); } foreach ($packets as &$packet) { if (!isset($packet['response'])) $packet['response'] = null; $results[$packet['sid']][$packet['name']] = $packet['response']; } return $results; } in my index: $results = $gq->requestData(); is this the info you need? – Esteban Aliaga Oct 15 '15 at 16:35
  • no, make a echo "
    "; print_r($results); echo "
    ";
    – Bak Oct 15 '15 at 16:46
0

I think you need this:

echo "
<table>
<tr>
    <th>Players</th>
    <th>Frags</th>
    <th>Ping</th>
</tr>";

foreach ($results as $data) {
    // print_r($data['players']);
    echo "
    <tr>
        <th>".$data['players']['nick']."</th>
        <th>".$data['players']['frags']."</th>
        <th>".$data['players']['ping']."</th>
    </tr>"
}

echo "</table>";
Berriel
  • 12,659
  • 4
  • 43
  • 67
0

Try this then,

 <table>
    <tr>
        <th>Players</th>
        <th>Frags</th>
        <th>Ping</th>
    </tr>
    <?php foreach ($results as $data) {
     <?php foreach($data['players'] as $item){ ?>
        <tr>
           <td><?php echo $item['gq_name']; ?></td>
           <td><?php echo $item['gq_frags']; ?></td>
           <td><?php echo $item['ping']; ?></td>

        </tr>
   <?php } ?>
    <?php } ?>
    </table>
Bak
  • 262
  • 1
  • 4
  • 11