Breaking 2 CSVs containing some identical data and insert into mysql database.

Posted on 2006-04-22
Medium Priority
Last Modified: 2011-04-14
My PHP is a little broken at the moment, i'm still learning and not having much luck with this problem.

I have 2 files both CSV files containing data.

One contains data in this format
PLU1, Cola ,53, 25

The other one contains data in this format
Pepsi, Cola, PLU1

I want to end up with
PLU, Pepsi, Cola, 53, 25

I was thinking (Pseudo Code)

Load each line into an array of file 1
Load each line into an array of file 2

Compare array_file1[1] && array_file1[2] with array_file2[3] && array_file2[3] if they match, write the SQL Query to file, when complete write the file to data.sql ready to load into SQL when required.

I assume i would just write "array_file1[1], array_file2[1], array_file2[2], array_file1[3], array_file1[4] \n" to the file.

The problem i am having is looping the data to check ALL the files against ALL the others, and then Any that dont match having them put out separately, so i end up with 5 part CSV and 2 left overs files, of data that didnt match.

I must apologise that i dont have kind of working code, as i have no idea how to start this.

Any idea's ?

Question by:Cenobitez
1 Comment
LVL 19

Accepted Solution

Barthax earned 500 total points
ID: 16666229
I think directly working with the SQL will give you far less of a headache:

Set up an SQL table (1) to house CSV file 1.
Set up an SQL table (2) to house CSV file 2.
Set up an SQL table (3) to house your combined data.
PHP script to import CSV file 1 to SQL table 1.
PHP script to import CSV file 2 to SQL table 2.
INSERT INTO table3 SELECT table1.field1, table2.field1, table1.field2, table1.field3, table1.field4 FROM table1, table2 WHERE table1.field1 = table2.field3

Note that I have assumed a typo in your combined dataset:

>I want to end up with
>PLU, Pepsi, Cola, 53, 25
PLU1, Pepsi, Cola, 52, 25
^ my assumption

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question