The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Fgetcsv help!
Discuss Fgetcsv help! in the PHP Development forum on Dev Shed. Fgetcsv help! PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 19th, 2013, 06:21 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
Time spent in forums: 24 m 37 sec
Reputation Power: 0
|
|
|
Fgetcsv help!
im stuck in a bubble, im trying to learn php and here is where im stuck.
im learning to read a .csv file. here is my code
PHP Code:
<?php
$fh = fopen("courses.csv", 'r') or die("File does not exist or you lack permission to open it");
$heading = fgetcsv($fh, 1000, ",");
echo $heading;
if (($handle = fopen("courses.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
and i get this:
http : / / s7.postimage.org/4eevdxil7/Screen_shot_2013_02_19_at_7_19_57_PM.png
and what im trying to accomplish is in this picture
http: / / s12.postimage.org/oqwiz6h6l/image.jpg
i separated the forward slashes
|

February 19th, 2013, 07:07 PM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
The first line is going to be your field names, so you need to read that and save the names for use later. It appears you have done this with your $heading variable.
After you've got the headers, you just keep reading the file line by line by calling fgetcsv in your loop. You should not be re-opening before the loop, as that will cause you to re-read the header row.
For each line you read with fgetcsv, you need to loop over the field names you got previously and echo them out + the appropriate value from that row. The value for a given field will be at the same index in the row's array as the header is in the heading array.
Finally after each row, echo a <hr> to create the separator line.
For example:
Code:
$fh = fopen("courses.csv", 'r') or die("File does not exist or you lack permission to open it");
$heading = fgetcsv($fh, 1000, ",");
while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) {
foreach ($heading as $idx=>$title){
echo "<strong>{$title}</strong>: {$data[$idx]}<br>\n";
}
echo "<hr>";
}
fclose($fh);
|

February 19th, 2013, 08:18 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
Time spent in forums: 24 m 37 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by kicken The first line is going to be your field names, so you need to read that and save the names for use later. It appears you have done this with your $heading variable.
After you've got the headers, you just keep reading the file line by line by calling fgetcsv in your loop. You should not be re-opening before the loop, as that will cause you to re-read the header row.
For each line you read with fgetcsv, you need to loop over the field names you got previously and echo them out + the appropriate value from that row. The value for a given field will be at the same index in the row's array as the header is in the heading array.
Finally after each row, echo a <hr> to create the separator line.
For example:
Code:
$fh = fopen("courses.csv", 'r') or die("File does not exist or you lack permission to open it");
$heading = fgetcsv($fh, 1000, ",");
while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) {
foreach ($heading as $idx=>$title){
echo "<strong>{$title}</strong>: {$data[$idx]}<br>\n";
}
echo "<hr>";
}
fclose($fh);
|
thanks for the help! and the explanation!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|