Page 1 of 1

Help Modifying A Code

Posted: Sat Jan 22, 2005 5:21 pm
by GunMac
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:

Code: Select all

<?php
$lines = file("test.txt");
krsort($lines);
foreach($lines as $data)
&#123;
list($band,$song) = explode("||", $data); 
echo "
$band ===> $song<br><br>
";
&#125;
?>
THE TEXT FILE:

Code: Select all

Led Zeppelin||Stairway To Heaven
Van Halen||Jump
Aerosmith||Sweet Emotion
Rolling Stones||Paint It Black
THAT WOULD OUTPUT THIS:

Code: Select all

Rolling Stones ===> Paint It Black<br>
Aerosmith ===> Sweet Emotion<br>
Van Halen ===> Jump<br>
Led Zeppelin ===> Stairway To Heaven<br><br>
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.

Code: Select all

$result = mysql_query("select * from shoutbox order by id desc limit 6");
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.

Re: Help Modifying A Code

Posted: Sat Jan 22, 2005 5:28 pm
by hunterhp
GunMac wrote: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:

Code: Select all

<?php
$lines = file("test.txt");
krsort($lines);
foreach($lines as $data)
&#123;
list($band,$song) = explode("||", $data); 
echo "
$band ===> $song<br><br>
";
&#125;
?>
THE TEXT FILE:

Code: Select all

Led Zeppelin||Stairway To Heaven
Van Halen||Jump
Aerosmith||Sweet Emotion
Rolling Stones||Paint It Black
THAT WOULD OUTPUT THIS:

Code: Select all

Rolling Stones ===> Paint It Black<br>
Aerosmith ===> Sweet Emotion<br>
Van Halen ===> Jump<br>
Led Zeppelin ===> Stairway To Heaven<br><br>
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.

Code: Select all

$result = mysql_query("select * from shoutbox order by id desc limit 6");
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.
I'm pretty sure a for loop will fix this.

Code: Select all

<?php
$lines = file("test.txt");
krsort($lines);
foreach($lines as $data)
&#123;
for ($i=0; $i < 6; $i++) &#123;
list($band,$song) = explode("||", $data); 
echo "
$band ===> $song<br><br>
";
&#125;
&#125;
?>
I'm pretty new to php, so I don't know if that'll work, I'm pretty sure it will.

Posted: Sat Jan 22, 2005 5:37 pm
by GunMac
Thanks for the quick reply! That didn't fix it exactly, but it's given me a new idea.

That looped every result 6 times instead of only showing 6 results. I didn't even think of doing something like that though...now I've got something new to look into for the time being.

Thanks for the help, I've got a few more ideas now.


Edit ----

I was playing around with it for a while and just figured it out. Thanks again for your help!

Posted: Sat Jan 22, 2005 5:51 pm
by hunterhp
Sad to see my code didn't work, great to see you capitalized on it. 8)