Forum Moderators: coopster

Message Too Old, No Replies

PHP file Query

I need some help with including a variable

         

Symbios

7:22 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



I've been using a variation of the code below to query a text file and display the contents line by line, I wanted to limit the number of lines displayed using a variable so I substituted
$n < count($Data)
with
$n < $numberofresults

This didn't work and no results were displayed, although it works if I substitute
$n < count($Data)
with
$n < 20

But for the application I'm using I need a variable so that the variable can be given a value from somewhere else.

<?php
$numberofresults = 20;
function ReadFromFile () {
/* Function ReadFromFile displays all the information stored in an external file. */
$TheFile = "path to file";
$Open = fopen ($TheFile, "r");
if ($Open) {
$Data = file ($TheFile);
for ($n = 0; $n < count($Data) ; $n++ ){
$GetLine = explode("\t", $Data[$n]);
$file = trim($file);
if($file!="." && $file!=".." && $lnfile>="5")
echo "<li>$file</li>";
}
fclose ($Open);
} else {
print ("Unable to read from file!<BR>\n");
}
} // End of ReadFromFile Function.
ReadFromFile();
?>

Birdman

7:39 pm on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

Try passing the variable to the function when you call it:

ReadFromFile(20);

Then, in the function:

function ReadFromFile ($num) {
...
...
for ($n = 0; $n < count($num) ; $n++ ){

That should fix you up. Have a lok at the php.net manual on "variable scope" to understand why it wasnn't working.

Birdman

Symbios

7:41 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Birdman, thanks for the help:)