View previous topic :: View next topic |
Author |
Message |
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Tue Feb 13, 2007 10:08 pm Post subject: Help with fgetcsv |
|
|
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 |
|
|
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Tue Feb 13, 2007 10:50 pm Post subject: |
|
|
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 |
|
|
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Wed Feb 14, 2007 12:34 am Post subject: |
|
|
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 |
|
|
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Mon Feb 19, 2007 3:39 am Post subject: |
|
|
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 |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Mon Feb 19, 2007 2:20 pm Post subject: |
|
|
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 |
|
|
|
|
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
|
|