thewooleymammoth Posted September 14, 2007 Share Posted September 14, 2007 I wanted to have the site owner (probably me) create a text file named 1.txt or 2.txt or whatever and have the page show the contents of the file in order of smallest to largest (as decided by the name of the txt file) but nothing shows up at all <?php while($lastart!=0){ $lastart=1; $filename="$lastart".".txt"; $openfile=fopen($filename, "r"); $content=fread($openfile, filesize($filename)); fclose($openfile); if(!fread($openfile, 'r')){ $lastart=0; }else{ echo "<table> <tr> <td>"."date(F d Y H:i:s., filemtime($filename)"."</td> </tr> <tr> <td>"."$content"."</td> </tr> <tr> <td> </td> </tr> </table>"; $lastart=$lastart+1; } } ?> nothing shows up which im assuming means the while statement is satisfied? if that is the case i would like to know how i would go about looping it so that when there is no file with the number its looking for it ends the loop. if that is not the case i just dont know what is and help would be appreciated. thanks Eric Quote Link to comment Share on other sites More sharing options...
Aeglos Posted September 14, 2007 Share Posted September 14, 2007 First, your loop never gets to the table echo <?php ... fclose($openfile); if(!fread($openfile, 'r')){ $lastart=0; }else{ ... ?> Since you terminate de file resource, fread will always return false, wich validates the first condition resseting $lastart and rewinding the while loop, ad nausea. Second, I don't believe you can do this (do enlighten me if I'm wrong ). <?php "... <td>"."date(F d Y H:i:s., filemtime($filename)"."</td> ..." ?> Try this for the echo: <?php echo "<table> <tr> <td>".date(F d Y H:i:s., filemtime($filename)."</td> </tr> <tr> <td>".$content."</td> </tr> <tr> <td> </td> </tr> </table>"; ?> As for looping through all the numbered files, I belive you only need to increment $lastart right after the echo (inside the if) by one ( $lastart++ Remember to move $lastart=1 out of the while loop though, else you'd return to file 1.txt eternally. Hope it helps, cheers. Quote Link to comment Share on other sites More sharing options...
thewooleymammoth Posted September 14, 2007 Author Share Posted September 14, 2007 alright so i tried it like this <?php $lastart=1; while($lastart!=0){ $filename="$lastart".".txt"; $openfile=fopen($filename, "r") or die('error1'); $content=fread($openfile, filesize($filename)) or die ('error2'); echo "$lastart"; if(!fread($openfile, filesize($filename))){ $lastart=0; }else{ echo " <table> <tr> <td>".date(F d Y H:i:s., filemtime($filename)."</td> </tr> <tr> <td>".$content."</td> </tr> <tr> <td>written by eric wooley</td> </tr> </table>"; $lastart=$lastart+1; } } ?> but i got the same result Quote Link to comment Share on other sites More sharing options...
Aeglos Posted September 14, 2007 Share Posted September 14, 2007 I figured it out, this is working (although a bit ugly, can be optimized I guess). <?php $lastart = 1; while ($lastart != 0){ $filename = $lastart.'.txt'; if (is_readable($filename)){ $openfile = fopen($filename, 'r') or die('error1'); $content = fread($openfile, filesize($filename)) or die ('error2'); $date = date('F d Y H:i:s.', filemtime($filename)); } else{ $content =null; } if (!$content){ $lastart = 0; } else{ echo '<table> <tr> <td>'.$date.'</td> </tr> <tr> <td>'.$content.'</td> </tr> <tr> <td>written by eric wooley</td> </tr> </table>'; $lastart++; } } ?> Note that I'm not a double quote fan Cheers. Quote Link to comment Share on other sites More sharing options...
thewooleymammoth Posted September 14, 2007 Author Share Posted September 14, 2007 lol yea i saw that, thanks ill try it out for sure Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.