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
$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.
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:
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: