Forum Moderators: coopster
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();
?>
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