Good day. There is a certain .ini file which contains a large array of information about users. It is necessary to display all the information in the file in a readable form (per user by line) on the site page. Then add several conditions (for example: in one of the additional fields, if one of the user parameters = from 0 to 2500, then output one image, from 2501 to 5000 - another, etc.). Explained somewhat crooked =) I hope you understand ...

Here are 2 entries from the .ini file:

[76561198777556446 StatsObject] PlayerName=Puma PlayerIP=86.102.208.50:14079 SelectedVeterancy=ServerPerksV3.SRVetSharpshooter DamageHealedStat=0 WeldingPointsStat=2620 ShotgunDamageStat=5159 HeadshotKillsStat=68 StalkerKillsStat=0 BullpupDamageStat=0 MeleeDamageStat=1859 FlameThrowerDamageStat=0 SelfHealsStat=0 SoleSurvivorWavesStat=0 CashDonatedStat=0 FeedingKillsStat=0 BurningCrossbowKillsStat=0 GibbedFleshpoundsStat=1 StalkersKilledWithExplosivesStat=0 GibbedEnemiesStat=16 BloatKillsStat=15 SirenKillsStat=6 KillsStat=298 ExplosivesDamageStat=10406 TotalZedTimeStat=135.000000 [76561200266252308 StatsObject] PlayerName=Arko74 PlayerIP=81.20.166.175:54019 SelectedVeterancy=ServerPerksV3.SRVetSupportSpec DamageHealedStat=1282 WeldingPointsStat=22490 ShotgunDamageStat=9086 HeadshotKillsStat=944 StalkerKillsStat=239 BullpupDamageStat=321378 MeleeDamageStat=12972 FlameThrowerDamageStat=0 SelfHealsStat=279 SoleSurvivorWavesStat=9 CashDonatedStat=3440 FeedingKillsStat=0 BurningCrossbowKillsStat=0 GibbedFleshpoundsStat=0 StalkersKilledWithExplosivesStat=35 GibbedEnemiesStat=132 BloatKillsStat=199 SirenKillsStat=63 KillsStat=4295 ExplosivesDamageStat=59712 TotalZedTimeStat=2199.000000 

Here is my solution:

here is my solution, but the result is rather sad ...

 <body bgcolor="#fff"> <? $num_of_mes = 500; // количество сообщений на странице $num_of_mes2 = 15000; $i=-28;$i2=-7;$i3=-24;$i4=-25;$i5=-4;$i6=-5;$ii2=-7; $messages_array = file("ServerPerksStat.ini", true); $overall_output = $messages_array; srand ((double) microtime() * 10000000); echo "<table align=Left bgcolor=#fff cellpadding=8 cellspacing=0 border=1 style='font-size :10px'>"; echo "<tr><td>".Игрок."</td><td>".Убийств."</td><td>".Сварка."</td><td>".Медицина."</td><td>".Очки."</td><td>".Игровое_время."</td></tr>"; for($m=0;$m<$num_of_mes;$m++) {$i=$i+29;$i2=$i2+29;$i3=$i3+29;$i4=$i4+29;$i5=$i5+29;$i6=$i6+29; if($num_of_mes==1) { echo "<tr><td>".$overall_output[$i]."</td><td>".$messages_array[$i2]."</td><td>".$messages_array[$i3]."</td><td>".$messages_array[$i4]."</td><td>".$messages_array[$i5]."</td><td>".$messages_array[$i6]."</td></tr>"; } else { echo "<tr><td>".$overall_output[$i]."</td><td>".$messages_array[$i2]."</td><td>".$messages_array[$i3]."</td><td>".$messages_array[$i4]."</td><td>".$messages_array[$i5]."</td><td>".$messages_array[$i6]."</td></tr>"; } } echo "</table>"; ?> 

I tried to do everything through the function parse_ini_file (), but somehow unsuccessfully ...
Help me to understand

    5 answers 5

    PPC you are tractor drivers.

     $data = parse_ini_file($filename, true); foreach($data as $key => $item) ... 

    I hope the essence is clear and what is needed next.

    Here the code is nine times less necessary than you do.

    • Late for a minute =) - Ilya Pirogov
     foreach (parse_ini_file("ServerPerksStat.ini", true) as $id => $statsObject) { // выводим $statsObject['PlayerName'], $statsObject['PlayerIP'], etc } 

      hmm, I see no problems with parse_ini_file () . This function has the second optional parameter $process_sections , by setting which to true you will get an associative array of ini sections of the file:

       array(2) { ["76561198777556446 StatsObject"]=> array(24) { ["PlayerName"]=> string(4) "Puma" ["PlayerIP"]=> string(19) "86.102.208.50:14079" ... } ["76561200266252308 StatsObject"]=> array(24) { ["PlayerName"]=> string(6) "Arko74" ["PlayerIP"]=> string(19) "81.20.166.175:54019" ... } } 

      Then we draw with uncomplicated gestures:

       // начинаем вывод // допустим, выводим Имя, Урон с Шотгана и хедшоты echo '<table width="100%">'; echo '<tr><td>Имя</td><td>Урон с Шотгана</td><td>Хедшоты</td></tr>'; // ServerPerksStat.ini - это путь до твоего ini-файла foreach(parse_ini_file('ServerPerksStat.ini', true, INI_SCANNER_RAW) as $key => $playerStats) { list($playerStats['NUM'], ) = explode(' ', $key); // выводим нужные поля echo '<tr>'; echo '<td>'.$playerStats['PlayerName'].'</td>'; echo '<td>'.$playerStats['ShotgunDamageStat'].'</td>'; echo '<td>'.$playerStats['HeadshotKillsStat'].'</td>'; echo '</tr>'; } echo '</table>'; // конец вывода 

      UPD. Forgive me @ Sh4dow , took his piece of code for output. What fields to display, I think, you decide, the keys of the associative array $ playerStats, as you see, coincide with the names of the fields from the ini-file.

      • Thanks for the answer, but could you give a full working example based on the existing code, or what you offer? The fact is that in php I am a layman ... - chi100v
      • leaving only your code from the last block in the file and specifying the file name received the following errors when trying to open the page: depositfiles.com/files/lekazbcra - chi100v
      • show this very ServerPerksStat.ini it looks like this is some kind of incorrect ini file - GLAGOLA
      • here it is: depositfiles.com/files/t1tdxfvnq - chi100v
      • Made changes, try again - should work - GLAGOLA
       <? header('Content-Type: text/html; charset=utf8')?> <html> <head> </head> <body><? error_reporting(E_ALL); $path = 'myfile.ini'; // наш файл $objlist = array(); $ini_content = file_get_contents($path); // берем содержимое $n = 0; foreach(explode("\n", $ini_content) as $line) { // разбиваем на линии if (empty($objlist[$n])) $objlist[$n] = new stdClass(); // если еще не создали, создаем объект if (trim($line) == '') { $n++; continue; } // если линяя пустая - пошел след. объект if ($line{0} == '[') { // если первый символ "[", значит, это заголовок list($num) = explode(' ', substr($line, 1), 2); $objlist[$n]->NUM = $num; continue; } // в других случаях пишем в объект пары ключ->значение if (strpos($line, '=') < 1) continue; @list($code, $val) = explode('=', $line, 2); $objlist[$n]->$code = $val; } // начинаем вывод // допустим, выводим Имя, Урон с Шотгана и хедшоты echo '<table width="100%">'; echo '<tr><td>Имя</td><td>Урон с Шотгана</td><td>Хедшоты</td></tr>'; foreach ($objlist as $playerStats) { // а тут у вас объект $playerStats, с которым уже можно делать что угодно // выводим нужные поля echo '<tr>'; echo '<td>'.$playerStats->PlayerName.'</td>'; echo '<td>'.$playerStats->ShotgunDamageStat.'</td>'; echo '<td>'.$playerStats->HeadshotKillsStat.'</td>'; echo '</tr>'; } echo '</table>'; // конец вывода ?> <pre> Структура объекта: <? var_dump($objlist[1]); ?> </pre></body></html> 

      Just in case, attach the file . I had no mistakes.

      • thanks for the answer! but if you take into account the already finished operating time, what should be corrected in it? - chi100v
      • To be honest, I saw your version later and would simply not understand it) I will update the answer, make an approximate conclusion, and you already see there but analogies. This is how it is. Actually, you only need to specify the path to the file in the first line. - Sh4dow
      • thank! using the code you provided, I received an error: depositfiles.com/files/fxapy5p42 here are the lines that have the error: 15: list ($ code, $ val) = explode ('=', $ line, 2); 16: $ objlist [$ n] -> {$ code} = $ val; - chi100v
      • added error_reporting (0), when starting the page I get the following error: (!) Parse error: syntax error, unexpected T_VARIABLE in C: \ Program Files \ VertrigoServ \ www \ index.php on line 3 3: $ path = 'ServerPerksStat.ini '; // our file is chi100v
      • Updated the answer - Sh4dow

      The index.php and ServerPerksStat.ini files are in the same directory.
      Contents of the index.php file:

        <?php // начинаем вывод // допустим, выводим Имя, Урон с Шотгана и хедшоты echo '<table width="100%">'; echo '<tr><td>Имя</td><td>Урон с Шотгана</td><td>Хедшоты</td></tr>'; // ServerPerksStat.ini - это путь до твоего ini-файла foreach(parse_ini_file('ServerPerksStat.ini', true, INI_SCANNER_RAW) as $key => $playerStats) { list($playerStats['NUM'], ) = explode(' ', $key); // выводим нужные поля echo '<tr>'; echo '<td>'.$playerStats['PlayerName'].'</td>'; echo '<td>'.$playerStats['ShotgunDamageStat'].'</td>'; echo '<td>'.$playerStats['HeadshotKillsStat'].'</td>'; echo '</tr>'; } echo '</table>'; // конец вывода ?> 

      Addressing the browser to this page we have the following errors:

      Warning: syntax error, unexpected TC_LABEL, expecting '=' in ServerPerksStat.ini on line 1 in C: Program Files (x86) VertrigoServwwwstatindex.php on line 8
      VertrigoServwwwstatindex.php on line 8 Warning: Invalid argument for foreach () in C: Program Files (x86)

      Code of the eighth line: foreach(parse_ini_file('ServerPerksStat.ini', true, INI_SCANNER_RAW) as $key => $playerStats)

      How it all looks in the browser: http://rghost.ru/12961351

      • Thanks everyone for the help! But in the end, nothing works anyway =) No one has any more implementation ideas? - chi100v