What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Generating and displaying a PHP array as a table?

Generating and displaying a PHP array as a table?
Author: Message:
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
Joined: Jul 2006
O.P. Generating and displaying a PHP array as a table?
What I am trying to do, is read a file containing data such as:

code:

Admins
{
"[ツツ]ア Furry Indigo"
       {
        "auth"      "steam"
        "identity"      "STEAM_0:0:1"
        "group"     "levelmax"
        "immunity"      "998"
    }

"Cherry"
       {
        "auth"      "steam"
        "identity"      "STEAM_0:1:1"
        "group"     "levelmax"
        "immunity"      "997"
    }
  etc
}


How would I go about, reading each entry, and displaying the name, in this case "[ツツ]ア Furry Indigo" or "Cherry", the STEAMID, in this case "STEAM_0:0:1" or "STEAM_0:1:1", the Group, in this case "levelmax", and the immunity level, in this case "998" or "997", and then displaying these in a table?

So far, with the help of others, I've managed to come up with:

code:
$file = 'admins.cfg';

$mode = 0; $section = $user = $key = "";
$array = array();

for ($i = 0; $i < strlen($file); $i++) {
    $c = $file[$i];
    switch ($mode) {
        case 0: // the section name
            if ($c == "{") {
                $section = trim($section);
                $array[$section] = array();
                $mode = 1; // look for a username
            } else {
                $section .= $c;
            }
            break;
        case 1: // user name (before the first ")
            if ($c == "}") {
                $mode = 0; // section is empty
            } else if ($c == '"') {
                $mode = 2; // get the username
            }
            break;
        case 2: // user name (inside the quotes)
            if ($c == '"') {
                $array[$section][$user] = array();
                $mode = 3;
            } else {
                $user .= $c;
            }
            break;
        case 3: // waiting for the {
            if ($c == "{") $mode = 4;
            break;
        case 4: // key (before the first ")
            if ($c == '"') $mode = 5;
            break;
        case 5: // key
            if ($c == '"') {
                $array[$section][$user][$key] = "";
                $mode = 6;
            } else {
                $key .= $c;
            }
            break;
        case 6: // value (before the first ")
            if ($c == '"') $mode = 7;
            break;
        case 7: // value
            if ($c == '"') {
                $key = "";
                $mode = 8;
            } else {
                $array[$section][$user][$key] .= $c;
            }
            break;
        case 8: // waiting for a " (key) or a }
            if ($c == '"') {
                $mode = 5;
            } else if ($c == "}") {
                $user = "";
                $mode = 9;
            }
            break;
        case 9: // waiting for a " (username) or a }
            if ($c == '"') {
                $mode = 2;
            } else if ($c == "}") {
                $section = "";
                $mode = 0;
            }
            break;
    }
}
var_dump($array);

Which produces:
code:
array(1) {
  ["Admins"]=>
  array(2) {
    ["[ツツ]ア Furry Indigo"]=>
    array(4) {
      ["auth"]=>
      string(5) "steam"
      ["identity"]=>
      string(11) "STEAM_0:0:1"
      ["group"]=>
      string(8) "levelmax"
      ["immunity"]=>
      string(3) "998"
    }
    ["Cherry"]=>
    array(4) {
      ["auth"]=>
      string(5) "steam"
      ["identity"]=>
      string(11) "STEAM_0:1:1"
      ["group"]=>
      string(8) "levelmax"
      ["immunity"]=>
      string(3) "997"
    }
  }
}
But then I have no idea how to extract the name, steamid, group and immunity from this array, display in a table and sort by immunity level?

This post was edited on 08-23-2009 at 11:07 PM by Jimbo.
08-23-2009 10:54 PM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: Generating and displaying a PHP array as a table?
why not just use regex to get all the data into an array? should be simple enough for someone on this forum (i cant do it atm because of this fucking heat wave and a massive headache...)

if someone doesnt do it soon, i will help you out... but surely WDZ, spunky, meetc, matty, or someone can help
08-24-2009 07:35 AM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
Joined: Jul 2006
O.P. RE: Generating and displaying a PHP array as a table?
Regex would be another option, yes, but still, due to my lack of PHP knowledge, I then wouldnt be able to create a table using that array.
08-24-2009 09:57 AM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: Generating and displaying a PHP array as a table?
ok this is what i came up with, it may not be the most efficient but it still works (with the example that you gave me...)

PHP code:
<?php
 
$file = './path/to/file.ext';
 
preg_match_all('/"([^"]*)"\n\s*\{\n\s*"auth"\s*"([^"]*)"\n\s*"identity"\s*"([^"]*)"\n\s*"group"\s*"([^"]*)"\n\s*"immunity"\s*"([^"]*)"/',file_get_contents($file),$matches);
 
echo '<table border="1px solid #000">';
for($i=0;$i<count($matches[0]);$i++){
    echo '<tr>';
    echo '<td>'.$matches[1][$i].'</td>';
    echo '<td>'.$matches[2][$i].'</td>';
    echo '<td>'.$matches[3][$i].'</td>';
    echo '<td>'.$matches[4][$i].'</td>';
    echo '<td>'.$matches[5][$i].'</td>';
    echo '</tr>';
}
echo '</table>';
 
?>


note: also i assumed that you are running on a linux box seeing as it is a css config... if you need a fix for windows just ask... [=

This post was edited on 08-24-2009 at 10:43 AM by NanaFreak.
08-24-2009 10:42 AM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
Joined: Jul 2006
O.P. RE: Generating and displaying a PHP array as a table?
Im actually running on a windows box, so yeah, a fix for windows would be awesome :)
Also, would there be a way to sort by immunity, descending?

This post was edited on 08-24-2009 at 10:49 AM by Jimbo.
08-24-2009 10:49 AM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: Generating and displaying a PHP array as a table?
quote:
Originally posted by Jimbo
Also, would there be a way to sort by immunity, descending?
there is but i cant think of a way to do it at the moment...

PHP code:
<?php
 
$file = './path/to/file.ext';
 
preg_match_all('/"([^"]*)"\n\s*\{\n\s*"auth"\s*"([^"]*)"\n\s*"identity"\s*"([^"]*)"\n\s*"group"\s*"([^"]*)"\n\s*"immunity"\s*"([^"]*)"/',str_replace("\r\n","\n",file_get_contents($file)),$matches);
 
echo '<table border="1px solid #000">';
for($i=0;$i<count($matches[0]);$i++){
    echo '<tr>';
    echo '<td>'.$matches[1][$i].'</td>';
    echo '<td>'.$matches[2][$i].'</td>';
    echo '<td>'.$matches[3][$i].'</td>';
    echo '<td>'.$matches[4][$i].'</td>';
    echo '<td>'.$matches[5][$i].'</td>';
    echo '</tr>';
}
echo '</table>';
 
?>

08-24-2009 10:52 AM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
Joined: Jul 2006
O.P. RE: Generating and displaying a PHP array as a table?
Thanks, for some reason though, its now only displaying some entries. The php I am using:
PHP code:
<?php
 
$file = 'admins.cfg';
 
preg_match_all('/"([^"]*)"\n\s*\{\n\s*"auth"\s*"([^"]*)"\n\s*"identity"\s*"([^"]*)"\n\s*"group"\s*"([^"]*)"\n\s*"immunity"\s*"([^"]*)"/',str_replace("\r\n","\n",file_get_contents($file)),$matches);
 
echo '<table border="1px solid #000">';
for($i=0;$i<count($matches[0]);$i++){
    echo '<tr>';
    echo '<td>'.$matches[1][$i].'</td>';
    echo '<td>'.$matches[2][$i].'</td>';
    echo '<td>'.$matches[3][$i].'</td>';
    echo '<td>'.$matches[4][$i].'</td>';
    echo '<td>'.$matches[5][$i].'</td>';
    echo '</tr>';
}
echo '</table>';
 
?>

Website the table is being displayed on: http://66.90.103.33/website/admins.php

And I shall PM you a link to "admins.cfg"

Also is there any way to make those characters that arent working appear? I tried setting the php page to utf-8, but it still displayed them as that question mark.


This post was edited on 08-24-2009 at 11:10 AM by Jimbo.
08-24-2009 11:01 AM
Profile E-Mail PM Find Quote Report
Felu
Veteran Member
*****


Posts: 2223
Reputation: 72
30 / Male / Flag
Joined: Apr 2006
Status: Away
RE: Generating and displaying a PHP array as a table?
I once did something similar. Here is what I used. You might have to make some modifications though. Feel free to ask for help.

http://www.terrawebdesign.com/multidimensional.php
08-24-2009 01:20 PM
Profile E-Mail PM Web Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
Joined: Jul 2006
O.P. RE: Generating and displaying a PHP array as a table?
Probably should have posted, but Nana has basically perfected his PHP for me, nearly everything works how I hoped it would.
08-24-2009 01:48 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On