Solved

Import CSV and UPDATE the data into mySQL

Posted on 2014-01-21
6
471 Views
Last Modified: 2014-01-27
Hi, I have some data in Excel (CSV format) that looks like this:

Material........Component.......Quantity
123456.........aaa.....................1
123456.........bbb....................2
123456.........ccc.....................1
555555.........ddd....................1

My mySQL database rows look like this (I inherited this structure):

Material....Component1...Component2...Component3...Quantity1...Quantity2...Quantity3
123456..............................................................................................................................
555555..............................................................................................................................

What I'm trying to do is import the CSV and UPDATE the data into the relevant columns, so that it looks like:

Material....Component1...Component2...Component3...Quantity1...Quantity2...Quantity3
123456.....aaa....................bbb....................ccc.....................1...................2..................1
555555.....ddd........................................................................1........................................

Can someone please give some examples on how the PHP should look?

Thank you
0
Comment
Question by:kbit
  • 3
  • 3
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39796531
You should redesign the data base!  What if you get a Material that has four Components?  And what if there is only one Component?  What would you put into the other columns?

But that aside, I think I can show you something that could translate the CSV into some kind of query.  I'll see if I can come up with a code sample and I'll post back in a little while.
0
 

Author Comment

by:kbit
ID: 39796541
Thanks very much Ray, that would be much appreciated.

Yes the structure is a nightmare...I've posted the stripped down version there...you dont want to see the real thing!
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39796716
This code snippet may transform the original test data into something that is usable for a data base update, but the data structure and process is so fraught with risk, that I'm almost reluctant to post it here.  Issues include...

1. What if there are more or fewer Components per Material?
2. What if there is a duplicate Material in the input file?
3. What if Component 1 should be empty, with the Quantity applicable to Component 2?
4. What if the input CSV is not sorted by Material?
5. What if there is a Material without any Component?
6. What if there is a mismatch in the count of Components and Materials?

I could go on, but I'm sure you get the point.  This would be a good time to get a DBA involved to help design a table structure that will not suck.

<?php // RAY_temp_kbit.php
error_reporting(E_ALL);
echo '<pre>';


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28343790.html#a39796531


/*
Material.......Component.......Quantity
123456.........aaa..............1
123456.........bbb..............2
123456.........ccc..............1
555555.........ddd..............1

Material...Component1...Component2...Component3...Quantity1...Quantity2...Quantity3
123456.....aaa..........bbb..........ccc...........1...........2...........1
555555.....ddd..........NULL.........NULL..........1...........NULL........NULL
*/

Class MaterialRows
{
    public function __construct()
    {
        $this->rows = array();
    }
    public function inject(array $row)
    {
        $mat = $row[0];
        $com = $row[1];
        $qty = $row[2];
        $this->rows[$mat]['com'][] = $com;
        $this->rows[$mat]['qty'][] = $qty;
    }
    public function collapse()
    {
        ksort($this->rows);
        reset($this->rows);
        var_dump($this->rows);
        foreach ($this->rows as $key => $row)
        {
            // PROCESS THE COMPONENTS
            $com = implode(',', $row['com']);

            // IF FEWER THAN 3 ADD EMPTY POSITIONS
            $xxx = 3 - count($row['com']);
            if ($xxx) $com .= str_repeat(',', $xxx);

            // PROCESS THE QUANTITIES
            $qty = implode(',', $row['qty']);

            // IF FEWER THAN 3 ADD EMPTY POSITIONS
            $xxx = 3 - count($row['qty']);
            if ($xxx) $qty .= str_repeat(',', $xxx);

            $out[]
            = $key
            . ','
            . $com
            . ','
            . $qty
            ;
        }
        var_dump($out);
    }
}

// TEST DATA SIMULATING WHAT YOU GET WITH FGETCSV()
$rows = array
( array(123456, 'aaa', 1)
, array(123456, 'bbb', 2)
, array(123456, 'ccc', 1)
, array(555555, 'ddd', 1)
, array(555555, 'eee', 3)
)
;

$aggregate = new MaterialRows;
foreach ($rows as $row)
{
    $aggregate->inject($row);
}
$aggregate->collapse();

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Assisted Solution

by:kbit
kbit earned 0 total points
ID: 39797524
Thanks a lot Ray, I think you're right, its a method that has a few ways of going wrong although some of your questions will not be issues in reality as the Excel file will be built by SAP.

However that piece of code will be very useful for a different import I'll have to do after this one.

For this one, I've put together a workaround as follows:

1) Import the file as-is into a temporary table (which is wiped with each run)
2) PHP reads this table and for each change in "Material", it does a loop, building a long UPDATE statement.
3) The UPDATE statement is then run which updates the target table
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39797560
I think I like your way better!
0
 

Author Closing Comment

by:kbit
ID: 39811484
The expert approved the method I used
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now