0

I've put my current code (not much) on this page for your reference (minus the real password, of course): http://mafgiftshop.org/MAF/queue/MP/for-reference.php

    $delimiter = ',';
$db = new mysqli('localhost', 'mafgifts_mp', '******', 'mafgifts_mp');

if (($handle = fopen("mpdata.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
        foreach($data as $i => $content) {
            $data[$i] = $db->real_escape_string($content);
        }
        echo $data[$i]."";
        $db->query("INSERT INTO `MP` 
                      (GiftAmount,GoalAmount,GiftDate,FundID,FundDescription) 
                      VALUES('" . implode("','", $data) . "');");
    }
    fclose($handle);
}

//THE ABOVE CODE PROPERLY PUTS ALL THE DATA INTO THE MySQL DATABASE, BUT I'M NOT 
//ABLE TO DO THE CALCULATIONS I WANT AND INSERT ONLY THE RESULTING DATA.

So I have a CSV file with 5 columns of information. There are no blank values. For example:

GiftAmount,GoalAmount,GiftDate,FundID,FundDescription (this line is not in the CSV; it is for reference)

  • "$10","$500","1/1/2012","8008","Description of 8008 Fund"
  • "$20","$500","2/10/2012","8008","Description of 8008 Fund"
  • "$62","$500","1/12/2012","8008","Description of 8008 Fund"
  • "$38","$1,000","3/31/2012","9102","Description of 9102 Fund"
  • "$21","$1,000","4/6/2012","9102","Description of 9102 Fund"

What I need to do is add the 'GiftAmount' column while the FundIDs are the same and place the result into a MySQL database. So, for the above data, here would be the resulting MySQL table:

  • "$92" | "$500" | "8008" | "Description of 8008 Fund" (notice the GiftDate is no longer necessary)
  • "$59" | "$1,000" | "9102" | "Description of 9102 Fund"

I'm using PHP to accomplish this and the PHP file will be run in a Cron job daily because the CSV file will change daily based on new gifts to the different funds.

By the way, the CSV file currently has 8,794 lines and many, many FundIDs.

3
  • Looks like all you need is a SUM(GiftAmount) with a GROUP BY. What query are you using? Commented Feb 4, 2013 at 16:36
  • @njk it is coming from a csv not a mysql database. Commented Feb 4, 2013 at 16:37
  • You can either do one of two things. On the PHP side, setup logic that will add if it's the same Fund. This will only work if it's sequential, otherwise you'll need an array. On the SQL side, you create a staging table, perform the calculations there and insert into a production table. Commented Feb 4, 2013 at 16:39

1 Answer 1

1

I would make the FundID a unique key in MYSQL, then use the INSERT INTO ... ON DUPLICATE KEY UPDATE query.

    $db->query("INSERT INTO `MP` (GiftAmount,GoalAmount,GiftDate,FundID,FundDescription) VALUES('" . implode("','", $data) . "') ON DUPLICATE KEY UPDATE GiftAmount=GiftAmount+".intval($data[0]).";");

This assumes GiftAmount is a numeric column, and you're not storing that as a string.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks so much for the quick reply. You mentioned the assumption that GiftAmount is numeric. In the CSV, it is just a string. How would I convert it to a decimal (right?) in the loop?
Change intval($data[0]) to sscanf($data[0], "\$%f").
Here's what my query looks like so far: $db->query("INSERT INTO MP (GiftAmount,GoalAmount,GiftDate,FundID,FundDescription) VALUES('" . implode("','", $data) . "') ON DUPLICATE KEY UPDATE GiftAmount=GiftAmount+".sscanf($data[0], "\$%f").";"); This does not put the data into the database. I noticed in my text editor that the ON DUPLICATE KEY UPDATE seems to be color-coded as though it's inside quotes. I'm not sure if that's the problem or not, though.
Here is a screenshot of my PHPMyAdmin, in case it will help: mafgiftshop.org/MAF/queue/MP/phpmyadmin.jpg
OK, I'm getting closer: $db->query("INSERT INTO MP (GiftAmount,GoalAmount,GiftDate,FundID,FundDescription) VALUES('" . implode("','", $data) . "') ON DUPLICATE KEY UPDATE GiftAmount=GiftAmount+".floatval(preg_replace("/[^-0-9\.]/","",$data[0])).";"); Unfortunately, the calculation seems to be doubling the first number in the group (in my example above, the 8008 FundID group would total 102 because it adds the first $10 to itself before adding the rest. Any thoughts on why that's happening?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.