Solved

Import CSV and UPDATE the data into mySQL

Posted on 2014-01-21
6
488 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 109

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 109

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 109

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determining creation & modification dates on MySQL tables 4 40
hasing a url 16 28
PHP Syntax Error 4 27
Decrypt string by php 7 32
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
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…

809 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