Help Modifying A Code
Posted: Sat Jan 22, 2005 5:21 pm
Hey guys. This is my first post here. Sorry if this question was anwered before - I searched your forums but couldn't find anything.
Anyway, I'm trying to learn the basics of using PHP with text files. I've always used MySQL databases, but for my current project a database would be considered overkill.
Ultimately, I'm trying to make a flatfile shoutbox but I'll make up an example of my code right now.
THE PHP CODE:
THE TEXT FILE:
THAT WOULD OUTPUT THIS:
Like I said before, that's just an example. I'm acually going to be making a shoutbox. The problem is, for the life of me I don't know how to modify the code I have to make it only display "X" number of shouts. For obvious reasons, having 200 shouts on your website's main page isn't that good.
I'm trying to restrict it to show only the 6 most current shouts instead of all of them. I'm hoping this will be an easy fix, but I know very little about working with text files. In case I haven't explained what I'm trying to do clearly enough, here's what it would be IF I was working with a Database instead.
I'm just trying to get that same effect, but from a text file.
Any and all help would be greatly appreciated. If you can, please try to explain it because my knowledge of text files is very limited. It took me 3 days just to figure out everything I posted here!
Thank you for your time and help.
Anyway, I'm trying to learn the basics of using PHP with text files. I've always used MySQL databases, but for my current project a database would be considered overkill.
Ultimately, I'm trying to make a flatfile shoutbox but I'll make up an example of my code right now.
THE PHP CODE:
Code: Select all
<?php
$lines = file("test.txt");
krsort($lines);
foreach($lines as $data)
{
list($band,$song) = explode("||", $data);
echo "
$band ===> $song<br><br>
";
}
?>Code: Select all
Led Zeppelin||Stairway To Heaven
Van Halen||Jump
Aerosmith||Sweet Emotion
Rolling Stones||Paint It BlackCode: Select all
Rolling Stones ===> Paint It Black<br>
Aerosmith ===> Sweet Emotion<br>
Van Halen ===> Jump<br>
Led Zeppelin ===> Stairway To Heaven<br><br>I'm trying to restrict it to show only the 6 most current shouts instead of all of them. I'm hoping this will be an easy fix, but I know very little about working with text files. In case I haven't explained what I'm trying to do clearly enough, here's what it would be IF I was working with a Database instead.
Code: Select all
$result = mysql_query("select * from shoutbox order by id desc limit 6");Any and all help would be greatly appreciated. If you can, please try to explain it because my knowledge of text files is very limited. It took me 3 days just to figure out everything I posted here!
Thank you for your time and help.