PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 19th, 2013, 06:21 PM
z3r01 z3r01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 z3r01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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($fh1000",");

    echo 
$heading;

if ((
$handle fopen("courses.csv""r")) !== FALSE) {
    while ((
$data fgetcsv($handle1000",")) !== 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

Reply With Quote
  #2  
Old February 19th, 2013, 07:07 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,654 kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 2 Months 2 Weeks 2 Days 5 h 1 m 44 sec
Reputation Power: 3436
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);
__________________
Recycle your old CD's, don't just trash them


Spidermonkey Tutorial;

If I helped out out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

Reply With Quote
  #3  
Old February 19th, 2013, 08:18 PM
z3r01 z3r01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 z3r01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Fgetcsv help!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap