Вывод массива таблицей

Программирование, которое не связано с API и Flash.

Вывод массива таблицей

Сообщение Fedya » 15 авг 2012, 01:50

Здравствуйте! :)

Часть кода мониторинга сервера(игрового)
  1. <?php
  2. function A2S_SERVERQUERY_GETCHALLENGE($host, $port) {
  3.     $st = request("\x57",$host,$port);
  4.     if (!$st) return false;
  5.     $st = substr($st, 4);
  6.     if (substr($st, 0, 1) != "\x41") return false; else return cutlong(substr($st, 1));
  7.   }
  8.   function A2S_RULES($host, $port, $challenge) {
  9.     $st = request("\x56".pastelong($challenge),$host,$port);
  10.     if (!$st) return false;
  11.     $st=substr($st, 4);
  12.     if (substr($st, 0, 1) == "\x41") {
  13.       $challenge = cutlong(substr($st, 1));
  14.       $st = request("\x56".pastelong($challenge),$host,$port);
  15.       if (!$st) return false;
  16.       $st = substr($st, 4);
  17.     }
  18.     if (substr($st, 0, 1) != "\x45") return false;
  19.     $result['Type'] = cutchar($st); // Char: Should be equal to 'E'
  20.     $result['Num Rules'] = cutshort($st); // Short: The number of rules reported in response
  21.     for ($i = 1; $i <= $result['Num Rules']; $i++) {
  22.       $result['Rule Name'][$i] = cutstring($st); // String: The name of the rule
  23.       $result['Rule Value'][$i] = cutstring($st); // String: The rule's value
  24.     }
  25.     return $result;
  26.   }
  27.  
  28.   function A2S_PLAYER($host, $port, $challenge) {
  29.     $st = request("\x55".pastelong($challenge),$host,$port);
  30.     if (!$st) return false;
  31.     $st = substr($st, 4);
  32.     if (substr($st, 0, 1) == "\x41") {
  33.       $challenge = cutlong(substr($st, 1));
  34.       $st = request("\x55".pastelong($challenge),$host,$port);
  35.       if (!$st) return false;
  36.       $st = substr($st, 4);
  37.     }
  38.     if (substr($st, 0, 1) != "\x44") return false;
  39.     $result['Type'] = cutchar($st); // Char: Should be equal to 'D'
  40.     $result['Num Players'] = cutbyte($st); // Byte: The number of players reported in response
  41.     for ($i = 1; $i <= $result['Num Players']; $i++) {
  42.       $result['Index'][$i] = cutbyte($st); // Byte: The index into [0.. Num Players] for this entry
  43.       $result['Player Name'][$i] = cutstring($st); // String: Player's name
  44.       $result['Kills'][$i] = cutlong($st); // Long: Number of kills this player has
  45.       $result['Time connected'][$i] = cutfloat($st); // Float: The time in seconds this player has been connected
  46.     }
  47.     return $result;
  48.   }
  49.    print("\n");
  50.    print_r(A2S_PLAYER("212.76.130.203",27252,A2S_SERVERQUERY_GETCHALLENGE("212.76.130.203",27252)));
  51.  
  52. ?>

он выводит
Array ( [Type] => D [Num Players] => 7 [Index] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) [Player Name] => Array ( [1] => _#MyXa [2] => Серый [3] => hack3p [4] => floma [5] => clev3R SLADKIY :* [6] => *V@Dee4*-=$34rus$=-²º¹² [7] => qvatrium ) [Kills] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 100 [5] => 200 [6] => 0 [7] => 0 ) [Time connected] => Array ( [1] => 10017.969726562 [2] => 1925.5032958984 [3] => 1903.1833496094 [4] => 1872.3732910156 [5] => 1503.3454589844 [6] => 1190.0704345703 [7] => 746.75134277344 ) )


Как сделать, чтобы он выводил таблицей, а не списком

первый столбец - Номер игрока
второй - ник
третий - счет
четвертый - время игры

Весь код мониторинга
  1. <?
  2.   // Simple Source/GoldSRC server info collector by morpheus
  3.   function cutchar(&$string)
  4.   {
  5.     $char = substr($string, 0, 1);
  6.     $string = substr($string, 1);
  7.     return $char;
  8.   }
  9.   function cutbyte(&$string)
  10.   {
  11.     $byte = ord(substr($string, 0, 1));
  12.     $string = substr($string, 1);
  13.     return $byte;
  14.   }
  15.   function cutstring(&$string)
  16.   {
  17.     $str = substr($string, 0, StrPos($string, chr(0)));
  18.     $string = substr($string, StrPos($string, chr(0))+1);
  19.     return $str;
  20.   }
  21.   function cutshort(&$string)
  22.   {
  23.     $short = substr($string, 0, 2);
  24.     list(,$short) = @unpack("S", $short);
  25.     $string = substr($string, 2);
  26.     return $short;
  27.   }
  28.   function cutlong(&$string)
  29.   {
  30.     $long = substr($string, 0, 4);
  31.     list(,$long) = @unpack("l", $long);
  32.     $string = substr($string, 4);
  33.     return $long;
  34.   }
  35.   function pastelong($long)
  36.   {
  37.     return pack("l", $long);
  38.   }
  39.   function cutfloat(&$string)
  40.   {
  41.     $float = substr($string, 0, 4);
  42.     list(,$float) = @unpack("f", $float);
  43.     $string = substr($string, 4);
  44.     return $float;
  45.   }
  46.   function request($request,$host,$port)
  47.   {
  48.     $request = "\xFF\xFF\xFF\xFF".$request."\x00";
  49.     $fp = @fsockopen('udp://'.$host, $port);
  50.     if (!$fp) return false;
  51.     @fwrite($fp, $request);
  52.     socket_set_timeout($fp, 1);
  53.     $string=fread($fp, 10240);
  54.     @fclose($fp);
  55.     return $string;
  56.   }
  57.  
  58.   function A2A_PING($host, $port) {
  59.     $st = request("\x69",$host,$port);
  60.     if (!$st) return false;
  61.     $st = substr($st, 4);
  62.     if (substr($st, 0, 1) != "\x6A") return false; else return true;
  63.   }
  64.   function A2S_INFO($host, $port) {
  65.     $st = request("\x54Source Engine Query",$host,$port);
  66.     if (!$st) return false;
  67.     $st = substr($st, 4);
  68.     if (substr($st, 0, 1) == "\x49") {
  69.       $result['Type'] = cutchar($st); // Char: 'I' (0x49) - For Source
  70.       $result['Version'] = cutbyte($st); // Byte: Network version
  71.       $result['Server Name'] = cutstring($st); // String: The server's name, eg: "Recoil NZ CS Server #1"
  72.       $result['Map'] = cutstring($st); // String: The current map being played, eg: "de_dust"
  73.       $result['Game Directory'] = cutstring($st); // String: The name of the folder containing the game files, eg: "cstrike"
  74.       $result['Game Description'] = cutstring($st); // String: A friendly string name for the game type, eg: "Counter Strike: Source"
  75.       $result['AppID'] = cutshort($st); // Short: Steam Application ID
  76.       $result['Number of players'] = cutbyte($st); // Byte: The number of players currently on the server
  77.       $result['Maximum players'] = cutbyte($st); // Byte: Maximum allowed players for the server
  78.       $result['Number of bots'] = cutbyte($st); // Byte: Number of bot players currently on the server
  79.       $result['Dedicated'] = cutchar($st); // Char: 'l' for listen, 'd' for dedicated, 'p' for SourceTV
  80.       $result['OS'] = cutchar($st); // Char: Host operating system. 'l' for Linux, 'w' for Windows
  81.       $result['Password'] = cutbyte($st); // Byte: If set to 0x01, a password is required to join this server
  82.       $result['Secure'] = cutbyte($st); // Byte: if set to 0x01, this server is VAC secured
  83.       $result['Game Version'] = cutstring($st); // String: The version of the game, eg: "1.0.0.14"
  84.     } elseif (substr($st, 0, 1) == "\x6D") {
  85.       $result['Type'] = cutchar($st); // Char: 'm' (0x6D) - For GoldSrc
  86.       $result['Game IP'] = cutstring($st); // String: Game Server IP address and port
  87.       $result['Server Name'] = cutstring($st); // String: The server's name, eg: "Recoil NZ CS Server #1"
  88.       $result['Map'] = cutstring($st); // String: The current map being played, eg: "de_dust"
  89.       $result['Game Directory'] = cutstring($st); // String: The name of the folder containing the game files, eg: "cstrike"
  90.       $result['Game Description'] = cutstring($st); // String: A friendly string name for the game type, eg: "Counter  Strike: Source"
  91.       $result['Number of players'] = cutbyte($st); // Byte: The number of players currently on the server
  92.       $result['Maximum players'] = cutbyte($st); // Byte: Maximum allowed players for the server
  93.       $result['Version'] = cutbyte($st); // Byte: Network version
  94.       $result['Dedicated'] = cutchar($st); // Char: 'l' for listen, 'd' for dedicated, 'p' for SourceTV
  95.       $result['OS'] = cutchar($st); // Char: Host operating system. 'l' for Linux, 'w' for Windows
  96.       $result['Password'] = cutbyte($st); // Byte: If set to 0x01, a password is required to join this server
  97.       $result['IsMod'] = cutbyte($st); // Byte: If set to 0x01, this byte is followed by ModInfo
  98.       $result['Secure'] = cutbyte($st); // Byte: if set to 0x01, this server is VAC secured
  99.       $result['Number of bots'] = cutbyte($st); // Byte: Number of bot players currently on the server
  100.       if ($result['IsMod'] == 1) {
  101.         $result['URLInfo'] = cutstring($st); // String: URL containing information about this mod
  102.         $result['URLDL'] = cutstring($st); // String: URL to download this mod
  103.         $result['Nul'] = cutbyte($st); // Byte: 0x00
  104.         $result['ModVersion'] = cutlong($st); // Long: Version of the installed mod
  105.         $result['ModSize'] = cutlong($st); // Long: The download size of this mod
  106.         $result['SvOnly'] = cutbyte($st); // Byte: If 1 this is a server side only mod
  107.         $result['ClDLL'] = cutbyte($st); // Byte: If 1 this mod has a custom client dll
  108.       }
  109.     } else return false;
  110.     return $result;
  111.   }
  112.   function A2S_SERVERQUERY_GETCHALLENGE($host, $port) {
  113.     $st = request("\x57",$host,$port);
  114.     if (!$st) return false;
  115.     $st = substr($st, 4);
  116.     if (substr($st, 0, 1) != "\x41") return false; else return cutlong(substr($st, 1));
  117.   }
  118.   function A2S_RULES($host, $port, $challenge) {
  119.     $st = request("\x56".pastelong($challenge),$host,$port);
  120.     if (!$st) return false;
  121.     $st=substr($st, 4);
  122.     if (substr($st, 0, 1) == "\x41") {
  123.       $challenge = cutlong(substr($st, 1));
  124.       $st = request("\x56".pastelong($challenge),$host,$port);
  125.       if (!$st) return false;
  126.       $st = substr($st, 4);
  127.     }
  128.     if (substr($st, 0, 1) != "\x45") return false;
  129.     $result['Type'] = cutchar($st); // Char: Should be equal to 'E'
  130.     $result['Num Rules'] = cutshort($st); // Short: The number of rules reported in response
  131.     for ($i = 1; $i <= $result['Num Rules']; $i++) {
  132.       $result['Rule Name'][$i] = cutstring($st); // String: The name of the rule
  133.       $result['Rule Value'][$i] = cutstring($st); // String: The rule's value
  134.     }
  135.     return $result;
  136.   }
  137.  
  138.   function A2S_PLAYER($host, $port, $challenge) {
  139.     $st = request("\x55".pastelong($challenge),$host,$port);
  140.     if (!$st) return false;
  141.     $st = substr($st, 4);
  142.     if (substr($st, 0, 1) == "\x41") {
  143.       $challenge = cutlong(substr($st, 1));
  144.       $st = request("\x55".pastelong($challenge),$host,$port);
  145.       if (!$st) return false;
  146.       $st = substr($st, 4);
  147.     }
  148.     if (substr($st, 0, 1) != "\x44") return false;
  149.     $result['Type'] = cutchar($st); // Char: Should be equal to 'D'
  150.     $result['Num Players'] = cutbyte($st); // Byte: The number of players reported in response
  151.     for ($i = 1; $i <= $result['Num Players']; $i++) {
  152.       $result['Index'][$i] = cutbyte($st); // Byte: The index into [0.. Num Players] for this entry
  153.       $result['Player Name'][$i] = cutstring($st); // String: Player's name
  154.       $result['Kills'][$i] = cutlong($st); // Long: Number of kills this player has
  155.       $result['Time connected'][$i] = cutfloat($st); // Float: The time in seconds this player has been connected
  156.     }
  157.     return $result;
  158.   }
  159.    print("\n");
  160.    print_r(A2S_PLAYER("212.76.130.203",27252,A2S_SERVERQUERY_GETCHALLENGE("212.76.130.203",27252)));
  161.  
  162. ?>
Fedya

 
Автор темы
Сообщения: 174
Зарегистрирован: 21 июл 2012, 19:56
Благодарил (а): 22 раз.
Поблагодарили: 6 раз.

Чтобы убрать блок с рекламой, зарегистрируйтесь на форуме или войдите.

Google
 



Re: Массивы

Сообщение alittlepieceofheaven » 16 авг 2012, 19:45

  1. $statistics = A2S_PLAYER("212.76.130.203",27252,A2S_SERVERQUERY_GETCHALLENGE("212.76.130.203",27252));
  2.  
  3. $players_nickname = $statistics['Player Name'];
  4. $players_kills = $statistics['Kills'];
  5. $players_time = $statistics['Time connected'];
  6.  
  7. echo "<table>";
  8. foreach ($players_nickname as $key=>$nickname)
  9. {
  10.   echo "<tr>";
  11.     echo "<td>{$key}</td>";
  12.     echo "<td>{$nickname}</td>";
  13.     echo "<td>{$players_kills[$key]}</td>";
  14.     echo "<td>{$players_time[$key]}</td>";
  15.   echo "</tr>";
  16. }
  17. echo "</table>";
alittlepieceofheaven

 
Сообщения: 1
Зарегистрирован: 23 июл 2012, 11:37
Благодарил (а): 0 раз.
Поблагодарили: 0 раз.


Вернуться в Программирование на PHP и других языках (без использования API)



Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 0

cron