Print_r to html table

i’ve been away from coding for many years and now im struggling to do the most basic things if anyone could help me display my php print_r results into a boot strap table i’d be mega great full the results i get are [gq_address] [gq_gametype] [gq_hostname] [players]

i’d like to display them into the following code, i’ve added markers to the table what needs to be called into which table.

the purpose of this code is to pull game server results such as ip,name,players,servername etc and display it into plain text once i’ve managed to print them into the tables ill remove the print_r from the php

<?php
require_once('GameQ/Autoloader.php');
$GameQ = new \GameQ\GameQ();
$GameQ->addServer([
    'type' => 'css',
    'host' => '46.105.73.18:27015',
]);
$results = $GameQ->process();
print_r($results);

?>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>Defcon Gaming</title>
        <link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
        <!-- Font Awesome icons (free version)-->
        <script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
        <!-- Google fonts-->
        <link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet" />
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet" />
        <!-- Core theme CSS (includes Bootstrap)-->
        <link href="css/styles.css" rel="stylesheet" />
    </head>
	
	

<table class="table table-striped table-dark">
  <thead>
    <tr>
      <th scope="col">List</th> 
	  <th scope="col">Address</th> 
      <th scope="col">Game Type</th> 
      <th scope="col">Server Name</th>
      <th scope="col">Players</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>List</td>
	   <td> <?php echo $results[gq_address];  ?> </td> <!-- Display print_r data table here-->
       <td> <?php echo $results[gq_gametype];  ?> </td> <!-- Display print_r data table here-->
       <td> <?php echo $results[gq_hostname];  ?> </td> <!-- Display print_r data table here-->
	   <td> <?php echo $results[players];  ?> </td> <!-- Display print_r data table here-->
    </tr>
    </tr>

  </tbody>
</table>
</html>


this is the data i get below, i want to display each array singly into the table like gq_address which is the ip then gq_hostname which is server name then players i had this before but lost all my files years ago just trying to recreate my old server page from 2016 xD

Array ( [46.105.73.18:27015] => Array ( [gq_address] => 46.105.73.18 [gq_dedicated] => [gq_gametype] => [gq_hostname] => [gq_joinlink] => steam://connect/46.105.73.18:27015/ [gq_mapname] => [gq_maxplayers] => [gq_mod] => [gq_name] => Counter-Strike: Source [gq_numplayers] => [gq_online] => [gq_password] => [gq_port_client] => 27015 [gq_port_query] => 27015 [gq_protocol] => source [gq_transport] => udp [gq_type] => css [players] => Array ( ) [teams] => Array ( ) ) )

image of the table obv bugged due to php error but i’d like to display the info to the table horizontally image

the erorr is

Fatal error: Uncaught Error: Undefined constant “gq_gametype” in E:\xampp\htdocs\test.php:44 Stack trace: #0 {main} thrown in E:\xampp\htdocs\test.php on line 44

line 44 <td> <?php echo $results[gq_gametype]; ?> </td>

Hi,

Change the <tbody> element like so:

<tbody>
  <?php foreach ($results as $index => $server) : ?>
  <tr>
    <th scope="row"><?php echo $index + 1; ?></th>
    <td><?php echo $server['gq_address'] ?? 'N/A'; ?></td>
    <td><?php echo $server['gq_gametype'] ?? 'N/A'; ?></td>
    <td><?php echo $server['gq_hostname'] ?? 'N/A'; ?></td>
    <td><?php echo count($server['players'] ?? []); ?></td>
  </tr>
  <?php endforeach; ?>
</tbody>

hi, thanks for the reply that has fixed it partially it does now display the server ip where i wanted it but doesn’t display anything else to the other tables just says na and 0 for players i also have the following error im on latest php

Warning: A non-numeric value encountered in E:\xampp\htdocs\test.php on line 43
47.105

line 43 = <th scope="row"><?php echo $index + 1; ?></th>

The warning indicates $index is not numeric.

Check the exact structure of $results using print_r or var_dump.

echo '<pre>';
print_r($results);
echo '</pre>';

What does that output?

i was using players array before i changed it to [num_players] which is a number but this is what it outputs

error i get now for using [num_players] is
Fatal error : Uncaught TypeError: count(): Argument #1

and
Warning : A non-numeric value encountered in E:\xampp\htdocs\test.php on line 44
46.235

Array
(
    [45.235.98.61:27019] => Array
        (
            [address] => 45.235.98.61:27019
            [dedicated] => d
            [game_descr] => ARGENTINA CS OFICIAL
            [game_dir] => cstrike
            [gq_address] => 45.235.98.61
            [gq_dedicated] => d
            [gq_gametype] => ARGENTINA CS OFICIAL
            [gq_hostname] => ARGENTINA CS INFERNO
            [gq_joinlink] => steam://connect/45.235.98.61:27019/
            [gq_mapname] => de_inferno
            [gq_maxplayers] => 32
            [gq_mod] => cstrike
            [gq_name] => Counter-Strike: Source
            [gq_numplayers] => 21
            [gq_online] => 1
            [gq_password] => 0
            [gq_port_client] => 27019
            [gq_port_query] => 27019
            [gq_protocol] => source
            [gq_transport] => udp
            [gq_type] => css
            [hostname] => ARGENTINA CS INFERNO
            [ismod] => 0
            [map] => de_inferno
            [max_players] => 32
            [num_bots] => 0
            [num_players] => 21
            [os] => l
            [password] => 0
            [players] => Array
                (
                )

            [port] => 27019
            [protocol] => 48
            [secure] => 1
            [steamappid] => 10
            [teams] => Array
                (
                )

            [version] => 1.1.2.7/Stdio
        )

)

Try this:

<tbody>
  <?php $counter = 1; ?>
  <?php foreach ($results as $server) : ?>
    <tr>
      <th scope="row"><?php echo $counter++; ?></th>
      <td><?php echo $server['gq_address'] ?? 'N/A'; ?></td>
      <td><?php echo $server['gq_gametype'] ?? 'N/A'; ?></td>
      <td><?php echo $server['gq_hostname'] ?? 'N/A'; ?></td>
      <td><?php echo $server['num_players'] ?? 0; ?></td>
    </tr>
  <?php endforeach; ?>
</tbody>
1 Like

you legend thats fixed it <3

No worries! What was causing the problem was that:

  • $index was a string because the array keys were server IPs, so adding +1 caused a warning. This we replaced with a $counter variable to get a proper row number.
  • count() was being used on num_players, which is an integer, not an array. We fixed it by echoing num_players directly.

Also, we added ?? 'N/A' to avoid warnings for missing keys like gq_hostname.

1 Like

thanks again

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.