Help with fgetcsv

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
RTAdams89
-


Joined: 06 Nov 2005
Posts: 102

PostPosted: Tue Feb 13, 2007 10:08 pm    Post subject: Help with fgetcsv Reply with quote

I have a CSV file as follows:
Code:

"player1",5'11,150,R,R,10,OF,17
"player2",5'6,119,R,R,10,2B,13


I am trying to create a PHP page which will look for a specified name in the first "column", and upon finding it, return the rest of the info in that line as separate variables. So, for example, if i specified the name as "player1", the script would store "5'11" as $height, "150" as $weight, etc. I tried playing around with fgetcsv, but I don't know enough PHP to get it working right. Any help would be greatly appreciated.
Back to top View user's profile Send private message
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Tue Feb 13, 2007 10:50 pm    Post subject: Reply with quote

Here's an example how to split the data into an array:

Code:
<?php
$playerdata = file("your_file.csv");
foreach($playerdata as $void => $d) {
   $array[] = explode(",", $d);
}

print_r($array);

?>


The above would output the following array:

Quote:
Array
(
[0] => Array
(
[0] => "player1"
[1] => 5'11
[2] => 150
[3] => R
[4] => R
[5] => 10
[6] => OF
[7] => 17

)

[1] => Array
(
[0] => "player2"
[1] => 5'6
[2] => 119
[3] => R
[4] => R
[5] => 10
[6] => 2B
[7] => 13
)

)


If you'd like to make a list of all player, you could use a foreach loop:

Code:
foreach($array as $number => $data) {
   echo $data[0]; // $data[0] is for the players name. You could change it to [1] instead if you'd like to output 5'11 instead.
}


If you just want to find one player, use the following code:

Code:
<?php
$playerdata = file("your_file.csv");
foreach($playerdata as $void => $d) {
   $array[] = explode(",", $d);
}

foreach($array as $number => $data) {
   if ($data[0] == "\"player1\"") {
      $info1 = $data[1];
      $info2 = $data[2];
      $info3 = $data[3];
      //etc...
   }
}

?>
Back to top View user's profile Send private message
RTAdams89
-


Joined: 06 Nov 2005
Posts: 102

PostPosted: Wed Feb 14, 2007 12:34 am    Post subject: Reply with quote

That is semi-working now. Is there a way I can use PHP to search the CSV file for a particular value (a player's name) and return which line that that value appears in?
Back to top View user's profile Send private message
RTAdams89
-


Joined: 06 Nov 2005
Posts: 102

PostPosted: Mon Feb 19, 2007 3:39 am    Post subject: Reply with quote

Is there a way I can use PHP to search the CSV file for a particular value (a player's name) and return which line that that value appears in?
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Mon Feb 19, 2007 2:20 pm    Post subject: Reply with quote

RTAdams89 wrote:
Is there a way I can use PHP to search the CSV file for a particular value (a player's name) and return which line that that value appears in?


We don't think so. You'll have to read the file and fill a table with the information then lookup for the player name in that table.
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group