Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Reading only 5 lines of a file.
Old 01-06-2006, 07:29 AM Reading only 5 lines of a file.
Skilled Talker

Posts: 68
Trades: 0
title said it all,
i have made a comment script and the comments are stored in a text file on a different line, i want to make it so it only sows the last 5 comments made.
heres what i'v go so far
PHP Code:
<?php
$handle 
= @fopen("comments.txt""r");
if (
$handle) {
   while (!
feof($handle)) {
       
$buffer fgets($handle4096);
       echo 
$buffer;
   }
   
fclose($handle);
}
?>
any ideas?
mrpaul is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-06-2006, 01:35 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
PHP Code:
$lines file("comments.txt");
$numlines count($lines);
for(
$i $numlines-5$i $numlines$i++) {
  echo 
$lines[$i];

file() reads the whole file into one big array.

If you're worried about the file being big and slow then you can use fgets, but the code gets messier.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 01-06-2006, 04:44 PM
Skilled Talker

Posts: 68
Trades: 0
Thats great but say the comments are like this;
1
2
3
4
5
6
7
8
9

It echos
5
6
7
8
9

i want it to echo
9
8
7
6
5

how do i d that?
mrpaul is offline
Reply With Quote
View Public Profile
 
Old 01-06-2006, 05:00 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Just flip the loop round so it starts at count-1 (since arrays start at 0) stops at count-5, and counts down by one every time instead of up.
PHP Code:
$lines file("comments.txt");
$numlines count($lines);
for(
$i $numlines-1$i >= $numlines-5$i--) {
  echo 
$lines[$i];

__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 01-06-2006, 06:41 PM
Skilled Talker

Posts: 68
Trades: 0
Ok, now i have a problem writeing comments to new lines with \n it keeps dividing them with a [] when i look inside comments.txt the comments are on the same line.
mrpaul is offline
Reply With Quote
View Public Profile
 
Old 01-07-2006, 05:13 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
How is the writing being done?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Reading only 5 lines of a file.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46991 seconds with 12 queries