Help with PHP snippet

Discussion in 'PHP' started by ETbyrne, Aug 2, 2007.

  1. #1
    Hey, I made this PHP script that opens a file and uses the information from each line to print out links, info, and more. The problem is: In the middle of the scripts loop it tries to open a file, but for some reason it only opens the file successfully the first time. I want it to be able to open the file every time the script loops.

    Here is my script:
    
    <?php
    
    $path = 'robots/latest_robots.txt';
    $file = file_get_contents("$path");
    
      $y = explode("|", $file);
    
    $z = 0;
    $x = 0;
    while($z < 10)
    {
      
      $id = $y[$x];     $x++;  // Robot ID
      $type = $y[$x];   $x++;  // Robot Type
      $author = $y[$x]; $x++;  // Author
      $title = $y[$x];  $x++;  // Title
      
      // Comment Count
      $newpath = "robots/$type/doc/$id.count.txt";
      $comments = file_get_contents("$newpath");  //This is the file that open 1 time only (NOTE: I am opening new files every loop)
      
      echo '<tr>';
      echo "<td><a href='view.php?type=$type&id=$id'>$title</a></td>";
      echo "<td>$type</td><td>$author</td><td>$comments</td>";
      echo "</tr>\n";
    
      $x += 2;
      $z++;
    }
    ?> 
    PHP:
    Here is latest_robots.txt

    123456|rover|Evan|My Rover|
    654321|rover|Jason|Red Rover|
    432012|arm|Mike|Big Hand|
    Code (markup):
    Thanks!
    - Evan Byrne
     
    ETbyrne, Aug 2, 2007 IP
  2. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #2
    $file = file_get_contents("$path");
    Code (markup):
    This reads the whole file into a string value. I would do this instead.


    @file("$path");
    Code (markup):
    It will read each line into a single array. So $arr[1] will be line 1. Then you can just put that into a session varible and then each file use the start_session. and then just call that session varible when you need it?

    I think you should ditch the flat file system and go with a sql system. It would make it a lot easier for you to work with.
     
    exodus, Aug 2, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    I'd use fgets(), as you only need 10 lines. file() or file_get_contents() is a waste of resources.

    <?php
    
    $path = 'robots/latest_robots.txt';
    $file = fopen($path, 'r');
    
    $z = 0;
    while($z++ < 10)
    {
      // Robot info
      list($id, $type, $author, $title) = explode("|", fgets($fp));
      
      // Comment Count
      $newpath = "robots/$type/doc/$id.count.txt";
      $comments = file_get_contents($newpath);
     
      echo '<tr>';
      echo "<td><a href='view.php?type=$type&id=$id'>$title</a></td>";
      echo "<td>$type</td><td>$author</td><td>$comments</td>";
      echo "</tr>\n";
    }
    ?>
    PHP:
     
    krt, Aug 2, 2007 IP
  4. bloodredxxx

    bloodredxxx Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can explode the contents latest_robots.txt twice; 1st for each line, then for each "|" seprated value.

    - note that i used trim to remove whitespaces.
    - you can also add some validation in case some of the values are empty.

    <?php
    
    $path = 'robots/latest_robots.txt';
    $file = file_get_contents("$path");
    
    $z = explode("\n", $file);
    
    foreach ($z as $my_z)
    {
      $y = explode("|",trim($my_z));
     
      $id = trim($y[0]);       // Robot ID
      $type = trim($y[1]);     // Robot Type
      $author = trim($y[2]);   // Author
      $title = trim($y[3]);    // Title
     
      // Comment Count
      $newpath = "robots/$type/doc/$id.count.txt";
      $comments = file_get_contents("$newpath");  //This is the file that open 1 time only (NOTE: I am opening new files every loop)
     
      echo '<tr>';
      echo "<td><a href=\"view.php?type=$type&id=$id\">$title</a></td>";
      echo "<td>$type</td><td>$author</td><td>$comments</td>";
      echo "</tr>\n";
    }
    ?>
    PHP:
     
    bloodredxxx, Aug 2, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    bloodredxxx, I just explained why using file_get_contents() is not a good choice for this...
     
    krt, Aug 3, 2007 IP