Jump to content

Recommended Posts

G'day Again all,

I have firstly googled and searched this forum, but no resolution found.

 

Trying to do:

1) Upload CSV (done)

2) Parse CSV

3) Set an Array Where the Keys are the values of the CSV header row

4) Assign the values of the following rows to a variable.

5) Update an MySQL Database

 

($SplitImportString[x] is the line I am dealing with from the CSV file...parsing it one line at a time)

 

Example 1 (CSV):

Col_1,Col_2,Col_3

Val_A1,Val_A2,Val_A3

Val_B1, Val_B2, Val_B4

 

Example 2 (HeaderArray) :

$SplitHeaderRow = split(",", $SplitImportString);

(this works fine)

 

Now I want to create an array that uses those as keys.

Below is an example of what I want in the array

Example 3 (RowArray) :

(Key = Value)

Col_1 = Val_A1

Col_2 = Val_A2

Col_3 = Val_A3

 

I'm just not really sure what I need to do to make the KEYS of RowArray be the Values of HeaderArray.

 

In the end i am will be performing the following SQL:

"UPDATE `table` SET `" . $HeaderArray[0] . "` = '" . $RowArray[$HeaderArray[0]] . "',`" . $HeaderArray[1] . "` = '" . $RowArray[$HeaderArray[1]] . "' WHERE `" . $HeaderArray[0] . "` = '" . $RowArray[$HeaderArray(0)] . "' LIMIT 1"

 

and again using the predifined values above

 

"UPDATE `table` SET `Col_1` = 'Val_A1',`Col_2` = 'Val_A2' WHERE `Col_3` = 'Val_A3' LIMIT 1"

 

I hope at the very least you can understand what I am trying to do and point in a better / different direction.

 

Using in PHP 4.x

 

I don't like using plugins and functions from online, nor 'toolboxes' as I don't like the idea that the development of them may stop.

 

I am happy to use predifned php or MySQL functions, statements etc.

 

Thanks for your help, let me know if you need further explanations.

 

UPDATE:

Sorry; The whole purpose is that the user will upload a CSV and as long as it has Headers I know what fields the user wants updated and each line will in turn store the values.

The user has a need for MANY different updates via CSV and this seemed far easier / better / flexible than hard-coding for each need and then having to do it again if they find a new CSV 'layout' / 'order' for some other update.

  • 2 weeks later...

sorry it took so long to reply.

 

fgetcsv was the answer.

 

I had no issues with it based on the examples provided.

 

I had to do some funky coding with array_flip for the header rows so that I could get the index # for the items i needed as a 'key' if you will.

 

I also found complatibility issues is the CSV was saved via MS Office for Mac.  I'm sure there would be ways around it but in this scenario there is no need for that.

 

The code is a little lengthy so I won't paste it in here, however if someone needs some help with the array_flip method that I used then by all means contact me via a PM to notify me of your post and I will get back to you in your post.

 

THANKS FOR THE HELP AND GUIDANCE guys !

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.