PDA

View Full Version : [php] giving me a headache - IF and WHILE function



DaveMania
March 29th, 2005, 08:35 AM
I have a code at the moment that prints all the lines from a text document and sparates the with delimeters.


$fp = fopen('TEXT_FILE','r');
if (!$fp) {echo 'File Doesn\'t Exist'; exit;}

while (!feof($fp)) {
$line = fgets($fp, 1024);
list ($field1, $field2, $field3) = split ('\|', $line);
echo '
<p>'.$field1.'</p>
<p>'.$field2.'</p>
<p>'.$field3.'</p>
</div>
';
++$fp;
}
fclose($fp);

Can someone tell me how to make it so that it only loops a certain number of times (say $max amount of times) and thus only displaying the first $max number of lines?

I've tried several different methods myself, all of which don't work

andr.in
March 29th, 2005, 08:54 AM
use a for() loop ;)

for($i=0;$i<$max;$i++) {
//display one line here
}

mpelland
March 29th, 2005, 08:58 AM
instead of 'while(!feof($fp)) ' try somthing like 'while($fp < 10)

the feof means file-end of file, which is what is allowing the loop to run through to the end of the file.

DaveMania
March 29th, 2005, 10:11 AM
@ syko: I'm not quite sure what you mean how owuld I encorporate that into the code?
@ mpelland: that just makes the thing loop and infinate amount of times with nothing to display just the paragraphs... strangely

andr.in
March 29th, 2005, 10:18 AM
umm ok the way I would do it:


$data = file_get_contents("TEXT_FILE"); //get the data from the file
if($data) {
$line = split("\|", $data); //split it into an array
for($i=0;$i<$max;$i++) { //The loop runs $max number of times
echo '<p>'.$line[$i].'</p>'; //echo the $i-numbered array element (line)
}
} else {
echo 'File doesn\'t exist';
}


edit: added the file-exist check

DaveMania
March 29th, 2005, 10:26 AM
Just had a brainstorm - I'm gonna use a stupidly annoying idea that will work a charm (in theory).
It includes several different documents containing the recent posts.
When I submit a new thing then it puts that into doc1.txt and it puts the contents of doc1.txt into doc2.txt and so one untill doc three and discards doc5 (or places that into another older file)

It's screwy but it'll work

nobody
March 29th, 2005, 12:30 PM
Why in the heck would you want to do that when you could just use a for loop like mentioned?

DaveMania
March 29th, 2005, 12:36 PM
becasue i'm a noob :) and I missed Syko's post :D
Thanks one hell of a lot Syko! U da man!

'tworks fine now

Cheers a bunch ppl (spesh Syko)

DaveMania
March 29th, 2005, 01:11 PM
that's great but it doesn't allow for multiple sections on one line
so it will either display head1 content1 foot1
but none others like 2 3 or 4
or
it will show just head1 for all the sections in the first set and just contents1 for all the sections in the second set

i'm trying to sort it

andr.in
March 29th, 2005, 01:23 PM
well umm... it all depends on how your data is structured in the text file... That code will just split it up and print it one by one...
Could you give us an example of the text file is like and what exactly and howyou want it to print it..?

DaveMania
March 29th, 2005, 01:26 PM
got it sorted THANKS!

andr.in
March 29th, 2005, 01:30 PM
umm.. ok no prob :)

DaveMania
March 29th, 2005, 01:56 PM
My final code looks like this

<?php
$max = "5";
$data = fopen('data.txt','r');
if (!$data) {echo 'ERROR: Unable to open file.'; exit;}

for($i=0;$i<$max;$i++) {
$line = fgets($data, 1024); //use 2048 if very long lines
list ($field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $line);

echo '
<p class="head">'.$field1.'</p>
<p class="content">'.$field1.'</p>
<p class="foot">'.$field1.'</p>
<br>
';
}

fclose($data);

?>

my data is sorted like this

head1|content1|foot1
head2|content2|foot2
head3|content3|foot3
head4|content4|foot4
head5|content5|foot5
head6|content6|foot6
head7|content7|foot7
head8|content8|foot8
head9|content9|foot9

I've got the printing sorted and I have another question
How can I print one specific line? from any point in the text file - if possible

Thanks for all the help so far

DaveMania
March 30th, 2005, 04:28 AM
Actually - it matters not as I have decided that I shall use one file for the code above to show the articles and the seperate files for the articles themselves - that way I have more space (instead of limiting it down to <1024 characters)
I shall probably then use XML for the article itself but stick to txt files for the description and other stuff :)