Jump to content

Archived

This topic is now archived and is closed to further replies.

3s2ng

CSV Parser

Recommended Posts

Hello Freaks,

I want to create a page like http://www.royalgojiteam.com/recognition_advancements.php but no database only CSV file.

How can I manage a page like that? The page is categories based on 1 field in the CSV in this case it the Title(ex. Royal Ambassadors, Ambassador V, etc.)

Sample Xls file:
[table]
[tr]
[td]Primary Last Name[/td]
[td]Primary First Name[/td]
[td]Secondary Last Name[/td]
[td]Secondary First Name[/td]
[td]Email[/td]
[td]Title[/td]
[td]State[/td]
[td]Password[/td]
[/tr]

[tr]
[td]McEachern[/td]
[td]Keith[/td]
[td]Secondary Last Name[/td]
[td]Secondary First Name[/td]
[td]keithmceachern@aol.com[/td]
[td]Royal Ambassador IV[/td]
[td]CT[/td]
[td]pwd[/td]
[/tr]

[tr]
[td]Estong[/td]
[td]Capio[/td]
[td][/td]
[td][/td]
[td]test@aol.com[/td]
[td]Royal Ambassador[/td]
[td]AZ[/td]
[td]123[/td]
[/tr]
[/table]

Sample CSV
Primary Last Name,Primary First Name,Secondary Last Name,Secondary First Name,Email,Title,ST,Password
McEachern,Keith,,,keithmceachern@aol.com,Royal Ambassador IV,CT,km5555
Moreland,Keith,,,free4lifekm@ris.net,Royal Ambassador IV,CO,klmsmm

How can I parse the csv to be placed in that kind of layout?

Thanks in advance.

Share this post


Link to post
Share on other sites
file() reads the file into an array

explode("," ...) will explode each line into separate elements

Share this post


Link to post
Share on other sites
Thanks for the Reply AndyB.

I want to display categorically.

Like if 1 listing has a title "Ambasador" I want to get that line and save it in an array. So it will display all listings with title "Ambassador" in the Ambassado category.

Share this post


Link to post
Share on other sites
as long as you're not using incredibly large CSV files, you'll be fine. you'll still have to parse through each record to sort since there is no engine within a flat file to sort by or filter. check out fgetcsv() as well:
[code]
<?php
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  $num = count($data);
  echo "<p> $num fields in line $row: <br /></p>\n";
  $row++;
  for ($c=0; $c < $num; $c++) {
      echo $data[$c] . "<br />\n";
  }
}
fclose($handle);
?>
[/code]

hope that helps

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.