...

View Full Version : Parsing CSV files using foreach / arrays / fgetscsv



curb
02-23-2007, 08:44 PM
I'm trying to avoid using MySQL by using my CSV file on the server. I need to pull out my students information from each class based on their age. I've been reading about the use of 'fgetscsv' but would not know how to apply it.

Here's how the csv file looks like
(TEACHER,AGE,SUBJECT,NAME)

MS James,10,math,john
MS James,10,math,jamie
MS James,9,english,judy
MS James,10,english,jordan
MS James,9,english,david
MR Henington,10,english,tina
MR Henington,9,math,mike
MR Henington,9,math,mathew

Example:
I'm trying to echo all the data that has students that are 10 years old to come out like this:

"
Hi I'm John and I'm 10 years old. My favorite subject is math
Hi I'm Tina and I'm 10 years old. My favorite subject is english
etc.
"


Any help would be appreciated.

CFMaBiSmAd
02-23-2007, 08:59 PM
The following generic code will read your file into an array of arrays -

<?php
$handle = fopen("curb.csv", "r");
$data = array();
while (($line = fgetcsv($handle)) !== FALSE) {
$data[] = $line;
}
?>The $data array will look like this -

Array
(
[0] => Array
(
[0] => MS James
[1] => 10
[2] => math
[3] => john
)

[1] => Array
(
[0] => MS James
[1] => 10
[2] => math
[3] => jamie
)

[2] => Array
(
[0] => MS James
[1] => 9
[2] => english
[3] => judy
)

[3] => Array
(
[0] => MS James
[1] => 10
[2] => english
[3] => jordan
)

[4] => Array
(
[0] => MS James
[1] => 9
[2] => english
[3] => david
)

[5] => Array
(
[0] => MR Henington
[1] => 10
[2] => english
[3] => tina
)

[6] => Array
(
[0] => MR Henington
[1] => 9
[2] => math
[3] => mike
)

[7] => Array
(
[0] => MR Henington
[1] => 9
[2] => math
[3] => mathew
)

)You would need to loop through each element of the $data array, testing the age element as you go, and produce the output that you want.

Edit: You could also put a test inside of the while loop that only returns the lines you want, then loop through the resultant lines and produce the output that you want.

curb
02-23-2007, 10:06 PM
Thankyou for the fast response. I tried out the code but it's giving me this error:

Warning: Wrong parameter count for fgetcsv()

CFMaBiSmAd
02-23-2007, 10:10 PM
I'm guessing you are using PHP4. The length parameter is optional in PHP5.

Change the line of code as follows (the length must be longer than your longest single line in the file) -

while (($line = fgetcsv($handle, 1000)) !== FALSE) {

curb
02-23-2007, 11:28 PM
How do I pull and sort out only rows that contains a specific age from "$data[1]"?




echo "<table>";

$handle = fopen("students.csv", "r");
$data = fgetcsv($handle, 1000, ",");
# data rows:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{

$teacher=$data[0];
$age=$data[1];
$subject=$data[2];
$name=$data[3];



echo "<tr>";

echo "<td>Hi I\m $name and I\'m $age years old. My favorite subject is $subject.</td>";

echo "</tr>\n";
}
echo "</table>\n";

Fumigator
02-24-2007, 03:46 AM
I think an "if" statement will do the trick.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum