Getintothegame Posted March 12, 2007 Share Posted March 12, 2007 Hello! I have a very basic news script for the main page of my website. It's simply a text file that I append lines to in a separate file called newspost.php. However, whenever a new post is added, it adds itself to the bottom of the file, so it's old news first. I need a script that will read or append backwards. I think it needs to be split into an array, but I'm at a loss on how to do this with this situation. Here's my current news reading script: <?php $news = file("news.txt"); foreach($news as $mynews) { $n_title = strtok($mynews,"¶"); $n_text = strtok("¶"); echo "<span class=star>$n_title</span><br> $n_text<br><br>"; } ?> Thank you very much! Link to comment https://forums.phpfreaks.com/topic/42315-solved-need-help-reading-newstxt-backwards-or-appending-to-the-top-of-the-file/ Share on other sites More sharing options...
Getintothegame Posted March 12, 2007 Author Share Posted March 12, 2007 Oh, and some sample data in the news.txt file would be: 2/19/2007: Guess What?!¶I got bored, ... 2/15/2007: Server Downtime¶Okay, I promised more stability... 2/13/2007: Updated Server!¶Sorry for the downtime... Link to comment https://forums.phpfreaks.com/topic/42315-solved-need-help-reading-newstxt-backwards-or-appending-to-the-top-of-the-file/#findComment-205832 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 13, 2007 Share Posted March 13, 2007 I recommend that you use a mysql database instead of using a text file to store your news. I think there is also a way to append to the beginning of a file, instead of the bottom, I'll look into it right now, will edit. Link to comment https://forums.phpfreaks.com/topic/42315-solved-need-help-reading-newstxt-backwards-or-appending-to-the-top-of-the-file/#findComment-205839 Share on other sites More sharing options...
AndyB Posted March 13, 2007 Share Posted March 13, 2007 reverse an array ... http://ca.php.net/manual/en/function.array-reverse.php ... might help Link to comment https://forums.phpfreaks.com/topic/42315-solved-need-help-reading-newstxt-backwards-or-appending-to-the-top-of-the-file/#findComment-205846 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 13, 2007 Share Posted March 13, 2007 Or this, it might be slow though, it gets the data already in there by opening the file as read only, then opens the same file with read and write access and writes your new content, then adds the old content to it: <?php $path = "./test.txt"; $handle = fopen($path, 'r'); $oldcontent = fread($handle, filesize($path)); $handle = fopen($path, 'w+'); $content = "[NEW NEWS ITEM]" . $oldcontent; //echo $content; if(!fwrite($handle, $content)) { echo "File write failed."; } else { echo "Success!"; } fclose($handle); ?> Change $path, and [NEW NEWS ITEM] and it should work for you. Link to comment https://forums.phpfreaks.com/topic/42315-solved-need-help-reading-newstxt-backwards-or-appending-to-the-top-of-the-file/#findComment-205851 Share on other sites More sharing options...
Getintothegame Posted March 13, 2007 Author Share Posted March 13, 2007 Alright, that worked great! Thank you! Sometime tomorrow I'll upload the script. Maybe someone could use it, like those who haven't really had any experience with SQL? Link to comment https://forums.phpfreaks.com/topic/42315-solved-need-help-reading-newstxt-backwards-or-appending-to-the-top-of-the-file/#findComment-206021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.