Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fgetcsv and array problem

Status
Not open for further replies.

lilokster

Technical User
Jul 23, 2011
2
US
I am trying to pull information from a csv file on my server and place it into a mysql table. I am able to pull the file using fopen and fgetcsv and get the following to display:

Array
[1] 7/23/2011
[2] 22012
[3] 100005

Array
[1] 7/23/2011
[2] 21022
[3] 100006

In this scenario [2] would be a campaign number I am pulling from a network. When I enter this in to mysql I need to assign a value to it to place in another column in the database.

So basically it would loop through the arrays and say if
[2]=22012 then $price=1.00
or if
[2]=21022 $price=1.50

and so on for several other numbers.

I had written the if statement and was able to assign a value to $price but when I echo the statement to see if it's working right it only shows the first array and no others and will only display what is in $price but not any other fields. And will only show the price for the first, but not any others.

Sorry if this is something simple but I am fairly new to all of this and have been trying to figure it out for 3 days on my own.

Thanks in advance for any help you can give.
 
To be more specific, here is the code I am currently using to view the file:

$handle = fopen("uploads/22.csv", "r");
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE)
{ echo "<pre>"; print_r($data); echo "<pre>"; }

This is the output:

Array
(
[0] => Date
[1] => CampaignID
[2] => Commission
[3] => ReferenceID
[4] => Status
)
Array
(
[0] => 7/8/2011
[1] => 20112
[2] => $0.00
[3] => 12569104
[4] => Credit
)
Array
(
[0] => 7/13/2011
[1] => 20021
[2] => $0.00
[3] => 12597255
[4] => Credit
)
Array
(
[0] => 7/14/2011
[1] => 20112
[2] => $0.00
[3] => 12605521
[4] => Credit
)

I need to create a variable that lets me assign a value for each differnt number in the [1] field. I also only need the information from field[1] and [3]
 
Hi

As you always read into $data, you must put the processing inside the [tt]while[/tt]'s block. So based on what I understood so far, I would do something like this :
PHP:
[navy]$pricetable[/navy][teal]=[/teal][b]array[/b][teal]([/teal]
  [purple]22012[/purple][teal]=>[/teal][purple]1.00[/purple][teal],[/teal]
  [purple]21022[/purple][teal]=>[/teal][purple]1.50[/purple]
[teal]);[/teal]
[navy]$sqlinsert[/navy][teal]=[/teal][green][i]"insert into table_name (CampaignID,ReferenceID,price) values ('%s','%s','%s')"[/i][/green][teal];[/teal]

[navy]$handle[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]fopen[/color][teal]([/teal][green][i]"uploads/22.csv"[/i][/green][teal],[/teal] [green][i]"r"[/i][/green][teal]);[/teal]   
[b]while[/b] [teal](([/teal][navy]$data[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]fgetcsv[/color][teal]([/teal][navy]$handle[/navy][teal],[/teal] [purple]5000[/purple][teal],[/teal] [green][i]","[/i][/green][teal]))[/teal] [teal]!==[/teal] FALSE[teal])[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal]
    [COLOR=darkgoldenrod]sprintf[/color][teal]([/teal]
      [navy]$sqlinsert[/navy][teal],[/teal]
      [COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$data[/navy][teal][[/teal][purple]1[/purple][teal]]),[/teal]
      [COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$data[/navy][teal][[/teal][purple]3[/purple][teal]]),[/teal]
      [COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][COLOR=darkgoldenrod]array_key_exists[/color][teal]([/teal][navy]$data[/navy][teal][[/teal][purple]2[/purple][teal]],[/teal][navy]$pricetable[/navy][teal])?[/teal][navy]$pricetable[/navy][teal][[/teal][navy]$data[/navy][teal][[/teal][purple]2[/purple][teal]]]:[/teal][purple]0[/purple][teal])[/teal]
    [teal])[/teal]
  [teal]);[/teal]
[teal]}[/teal]
[COLOR=darkgoldenrod]fclose[/color][teal]([/teal][navy]$handle[/navy][teal]);[/teal]
[small][maroon]Warning[/maroon] The above code was not tested[/small]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top