Recursive File Parser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pdoersch
Forum Newbie
Posts: 12
Joined: Sun Mar 06, 2005 1:09 pm

Recursive File Parser

Post by pdoersch »

Hi guys, I'm working on a family tree web site. Here are the basics. Every person gets a file, with a list of thier children and thier filenames in it (along with other info). What I need to do, is write a bit of code that goes through all the files by following the links. This will be used for several things (drop down menu of all people in the tree, drawing of the complete table, etc.) Now the catch is I don't know how many generations down it will end up going, and it would be essentially the same code anyway, so how could I go about looping it to go through and find all the people at once? Here's what I have so far, but its not exactly complete.

Code: Select all

$f = fopen('$filename', "r");
$c = fgets($f);
$c = fgets($f);
     while (!feof($f)) {
          $c = fgets($f);
          $filename = str_replace("\n", "", fgets($f));
          $realname = str_replace("\n", "", fgets($f));
          echo "<option value='" . $filename . "'>" . $realname . "\n";  //menu in this case
          $g = fopen($filename, "r");    //this has to somehow loop back to the top without forgetting what the last file and the one before that was
        }

and here is an example file:

Code: Select all

3         //file type identifyer
John Smith    //primary perant

1123267570  //file name for this kid
Kid One        //kid's name

1123267491  //file name for this kid
Kid Two        //kid's name

1123264571  //file name for this kid
Kid Three        //kid's name
3                  //marker of end of child list
other info
other info
other info
Thanks for the help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This doesn't sound like a security issue...

Moved to PHP - Code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it seems to be that this is best done with a database of normalized data.. say:

People
personID
firstName
middleName(s)
lastName
nickname
birthday
deathDay
notes
additional
sex

Relation
relationID
person->personID
who->personID
relationType (things like, parent, marriage, divorce, whatever...)
pdoersch
Forum Newbie
Posts: 12
Joined: Sun Mar 06, 2005 1:09 pm

Post by pdoersch »

Sorry about posting in the wrong forum, all my other posts were for security so I had that forum bookmarked. Sorry. About this database thing, I suppose I would do that but that would require something like SQL (which I have no background in) and I already have this file based system worked out from a previous project. All I need is to figure out how to write that code snippet to recursively move through the files. I'm not sure if i'd need subroutines or what. Thanks for the help so far though!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here's the basic idea in semi-code

Code: Select all

$files = array('firstFile');
$i = 0;
do
{
  $file = $files[$i];
  $data = file_get_contents($file);
  // parse the file for other files to load and the data it holds, store off the data somewhere.
  // use array_search() to determine if a file you've found in the data is new
  if(array_search($newFile, $files) === false)
  { // the file isn't in the list, add it
    $files[] = $newFile;
  }

  // after processing increment the count and check where we are
  $i++;
}
while($i < count($files));
pdoersch
Forum Newbie
Posts: 12
Joined: Sun Mar 06, 2005 1:09 pm

Post by pdoersch »

Hey! You know what, that might just work! I'll fix it up a little, but I think that will do it. Thanks! I'll post what I finally use when I get around to writing it. This is great, thanks!

-Paul
pdoersch
Forum Newbie
Posts: 12
Joined: Sun Mar 06, 2005 1:09 pm

Post by pdoersch »

OK, I got it. Here is what I used:

Code: Select all

$array = array('1Start.txt'); 
        $i = 0; 
        while($i < count($array))  { 
            $file = $array[$i]; 
            $f = fopen($file, "r");
            $c = fgets($f);
            $c = fgets($f);
            $drop = 0;
            while (!feof($f) && ($drop != 1)) {
                $c = str_replace("\n", "", fgets($f));
                if ($c == 3 ) {
                    $drop = 1;
                } else {
                    $filename = str_replace("\n", "", fgets($f));
                    $realname = str_replace("\n", "", fgets($f));
                    echo "<option value='" . $filename . "'>" . $realname . "\n";
                    $array[] = $filename;
                }
            }
              $i++; 
        }
Thnaks again!
Post Reply