How can I make the following code return more then one line from the file read in? right now it will only return the first line from the file and thats it
<?php
function getFile( $FN )
{
if(!file_exists($FN))
{
echo 'Bad file name';
return;
}
$fp = fopen ($FN, "r");
while($line = fgets($fp, 1024))
{
list($project_number,$workpackage_number,$employee_number,$date,$number_of_hours)= explode(",",$line);
$oneline = array($project_number,$workpackage_number,$employee_number,$date,$number_of_hours);
return $oneline;
}
fclose($fp);
}
$man = array();
$man = getFile("timerecords.txt");
for($i = 0; $i < count($man); $i++)
{
echo $man[$i];
}
?>
File Function (returning multiple lines from a file)
Moderator: General Moderators
- Templeton Peck
- Forum Commoner
- Posts: 45
- Joined: Sun May 11, 2003 7:51 pm
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
You could try this:
test.php:
timerecords.txt:
teste.php output:
I hope it helps.
Cheers,
Scorphus.
test.php:
Code: Select all
<?php
function getFile( $FN ) {
if(!file_exists($FN)) {
echo 'Bad file name';
return;
}
$fp = fopen ($FN, "r");
while($line = fgets($fp, 1024)) {
list($project_number,$workpackage_number,$employee_number,$date,$number_of_hours)= explode(",",$line);
$oneline[] = array($project_number,$workpackage_number,$employee_number,$date,$number_of_hours); //append to $oneline
}
fclose($fp);
return $oneline;//return on function's end
}
$man = array();
$man = getFile("timerecords.txt");
for($i = 0; $i < count($man); $i++)
print_r($man[$i]);
?>Code: Select all
a1,b1,c1,d1,e1
a2,b2,c2,d2,e2Code: Select all
Array
(
ї0] => a1
ї1] => b1
ї2] => c1
ї3] => d1
ї4] => e1
)
Array
(
ї0] => a2
ї1] => b2
ї2] => c2
ї3] => d2
ї4] => e2
)Cheers,
Scorphus.
- Templeton Peck
- Forum Commoner
- Posts: 45
- Joined: Sun May 11, 2003 7:51 pm