The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
FILO (First In Last Out)
Discuss FILO (First In Last Out) in the PHP Development forum on Dev Shed. FILO (First In Last Out) PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 13th, 2000, 07:58 AM
|
|
Junior Member
|
|
Join Date: Jun 2000
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hello,
I have just written a small guestbook which is working well, however the first guests' comments
are always the 1st (top).
My prefered option is to always have the latest comments on top.
Is there an (easy!!!) way of doing this, like (hopefully) 1 built-in funtion.?
Thanking you in advance,
------------------
With my kind regards,
Peter.
|

July 13th, 2000, 08:24 AM
|
|
Contributing User
|
|
Join Date: Apr 1999
Posts: 114
Time spent in forums: 42 m 44 sec
Reputation Power: 15
|
|
|
Well, you didn't provide much info about how it works, but if you're using SQL to store the entries, just try:
select comments from guest order by entrydate desc;
|

July 13th, 2000, 08:33 AM
|
|
Junior Member
|
|
Join Date: Jun 2000
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Sorry, it is just a plain old text file!
Name: Email: & Comments:
These are saved to a text file with the html <br> and other formatting added.
As I mentioned it works fine but the FILO is a refinement.
I do not have any SQL on board nor experience!
------------------
With my kind regards,
Peter.
|

July 13th, 2000, 10:02 AM
|
|
Contributing User
|
|
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
Peter,
This is not the most elegant way to do it (in the absence of push and pop functions) but it may get you started.
<?
if(!($myfile=fopen("guestbook.txt", "r")))
{
print"Could not open file";
exit;
}
while($myline=fgets($myfile, 255))
{
$bytes[]=ftell($myfile); // get the file pointer vals
}
for($i=0; $i<count($bytes)-1; $i++)
{
// copy to separate array minus the
// last value which is EOF
$bytelist[]=$bytes[$i];
}
rsort($bytelist); // reverse it
$bytelist[]=0; // pad the end with 0, the beginning of the file
for($i=0; $i<count($bytelist); $i++)
{
fseek($myfile, $bytelist[$i]);
$myline=fgets($myfile, 255);
print"$myline<BR>";
}
fclose($myfile);
?>
If your file has more than 255 per line, adjust accordingly....
|

July 13th, 2000, 12:07 PM
|
|
Contributing User
|
|
Join Date: Jun 2000
Posts: 59
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
hmm, or you could use file()
// Put Guest Book into an Array
$myarray = file("myguests.txt");
// Reverse Array
$myarray = array_reverse($myarray);
For this to work you must have PHP4 and each entry for the guest book on it's own serperate line.
Chris
[This message has been edited by Chris Pickett (edited July 13, 2000).]
|

July 13th, 2000, 01:22 PM
|
|
Contributing User
|
|
Join Date: Jul 2000
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
You could (something like the above comment)
$file = file("text.txt");
$size = sizeof($file) - 1;
for($i=$size;$i>=0;$i--)
echo $file[$i];
whatever floats your boat
|

July 13th, 2000, 02:21 PM
|
|
Contributing User
|
|
Join Date: Apr 1999
Posts: 114
Time spent in forums: 42 m 44 sec
Reputation Power: 15
|
|
I agree that the file() function (with one of the two solutions above) is the best bet; but just for the record, PHP4 does have array_push() and array_pop():
http://www.php.net/manual/ref.array.php
[This message has been edited by scollo (edited July 13, 2000).]
|

July 13th, 2000, 04:40 PM
|
|
Junior Member
|
|
Join Date: Jun 2000
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks peoples,
I will play around with the provided options and see what happens.
Am unable to compile PHP4 so using PHP 3.0.12.
Again thanks,
------------------
With my kind regards,
Peter.
|

July 13th, 2000, 10:45 PM
|
|
Junior Member
|
|
Join Date: Jun 2000
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
FILO fixed.
I ended up using a slight variation of scollo's reply.
<?
$myarray=file("GBFile.txt");
for($1=count($myarray)-1;$i>=0;$i--) {
echo $myarray[$i];
}
and it appears to work.
Thanks again.
------------------
With my kind regards,
Peter.
|

July 13th, 2000, 11:09 PM
|
|
Junior Member
|
|
Join Date: Jun 2000
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
OOOPS!!!! Sorry,
I had my names mixed.
The FILO problem was solved using polman's reply.
Sorry polman.
------------------
With my kind regards,
Peter.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|