Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IT READS BACKWARDS!!!!??????

Status
Not open for further replies.

stc7outlaw

Programmer
Mar 7, 2002
3
0
0
US
I wrote a code to read my text file from my server and my friend tested it and so did I, and it reads upside down. I am a very new PHP programmer. Starting today was my first program. How will I be able to make it so the text file read from top to bottom and displays it on the webpage top to bottom Here is my code that Reads UpSiDeDoWn:
<?php

$filename =&quot;main_news.txt&quot;;

$myFile = fopen($filename, &quot;r&quot;); //open the file for reading, file pointer will be at the beginning of the file

if(! $myFile){ // Make sure the file was opened successfully

print (&quot;File could not be opened.&quot;);

exit;
}




$fcontents = file($filename);


$totalines = count($fcontents); //how many lines of data the file contains


$count = $totalines;

for ($i = 0; $i <= $count; $i++, $totalines--){


$line = ($fcontents[$totalines]);

if($line != &quot;&quot;){ // if the line from the file is not blank print it
echo &quot;$line &quot;;
}

}

fclose($myFile);

?>
 
When you use file() to read you don't need to open/close the file.

to read a file and displaying it in the screen, just do this:

echo implode(&quot;&quot;,file(&quot;filename.txt&quot;));

nothing more is needed.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Think about it...
$totalines = count($fcontents); // Say you have 50 lines in your file..

$count = $totalines;//$count and $totalines=50

for ($i = 0; $i <= $count; $i++, $totalines--){
$line = ($fcontents[$totalines]); $line = line # 50 of $fcontents

if($line != &quot;&quot;){ // if the line from the file is not blank print it
echo &quot;$line &quot;;
}

}



This should work...

for ($i = 0; $i <= $count; $i++){
$line = ($fcontents[$i]);
if($line != &quot;&quot;){
echo &quot;$line &quot;;
}

}
-Nukoi
I know my stuff.. Hire me.
 
I did this and found that a useful function to add while reading textfiles is the nl2br() function.
This takes your text and makes is appear correctly in a webpage.
-------------------------------------------
[pre]
<?php
// find the time of the last file mod

$filemod = filemtime($newsfile);

$filemodtime = date(&quot;F j Y h:i:s A&quot;, $filemod);

echo &quot;<table width=\&quot;85%\&quot; border=\&quot;0\&quot; cellspacing=\&quot;0\&quot; cellpadding=\&quot;0\&quot;><tr><td><font size=\&quot;+2\&quot;><b>News</b></font></td><td><div align=\&quot;right\&quot;>$filemodtime</div></td></tr></table>&quot;;

echo &quot;<div align=left><hr></div>&quot;;

// read in the news file

$fcontents = file($newsfile);

while (list ($line_num, $line) = each ($fcontents)) {

if ($line_num == &quot;0&quot;) { //finding line 1 of the file

printf(&quot;<h3> $line</h3>&quot;); // making line 1 the title for the news
// use H3 {color: black;text-decoration: underline } in a css to get the h3 effect to work

} else{

print nl2br($line);
}
}


?>
[/pre] ***************************************
Party on, dudes!
 
Just a note, beware of the use of nl2br function. Suppose the file you are reading is written in HTML, and you have a table in it. This function replaces ALL the newlines for the <br> tag. Doing this, it replaces even the newlines inside the table, so the table can be taken too much down cause the browser writes the <br> outside and before the table (the ones outside <td> tags).
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Heh, I learn more everyday.
Lucky I was using plain text only.

Thank Anikin for pointing out pitfalls in this. ***************************************
Party on, dudes!
 
One more point - probably insignificant in comparison to everything above - but it's not a very good idea to use $count as a variable becuase this may conflict with using the count() function. It is generally dangerous to use variable names that are the same as any function names. -gerrygerry
Go To
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top