Jump to content

Working with Csv File data


joeshanley

Recommended Posts

Hi,

 

I am updating a few parts of my current website.

Currently, the website updates user accounts by uploading CSV data reports.

 

I currently use fgetcsv to deal with each row and this is taking forever to deal with the data as this is currently the code for each row.

 

 

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)

{

$grabinfo = "SELECT * FROM transactions WHERE id = '$data[$i]'";

$grabinforesult = mysql_query($grabinfo) or die(mysql_error());

while($row = mysql_fetch_array($grabinforesult)){

$merchant = $row['merchant'];

$username = $row['username'];

$datetime = $row['datetime'];

$letthem = $row['letthem'];

$validate = "SELECT * FROM transactions WHERE merchant = '$merchant' and username = '$username' and (status='pending' or status='success' or status='confirmed') and datetime='$datetime'";

$validator = mysql_query($validate) or die(mysql_error());

if(mysql_num_rows($validator) >= $letthem)

{

$import="UPDATE transactions SET amount='0.00' WHERE id='$data[$i]' and status ='awaiting'";

mysql_query($import) or die(mysql_error());

}}

 

 

 

 

mysql_query($import) or die(mysql_error());

} fclose($handle);

 

 

This is performing lots of checks on the csv data and checking it with the system. It checks each row.

Is there a quicker way of reading the csv data? There are lots of checks and while loops within the fgetcsv while loop. It can take upto half an hour to process a file with 1000 csv rows.

 

Hope someone could offer some advise.

 

Thanks

Link to comment
Share on other sites

I've just created this. I think this will be quicker to store and then read the CSV data.

 

function load_csv($file,$identifier,$commission,$purchase_value)

    {

    $lines = file($file) or die('Could not open file');

foreach($lines as $line)

        {

$row_data = explode(",", $line);

$new_row[0] = $row_data[$identifier];

$new_row[1] = $row_data[$commission];

$new_row[2] = $row_data[$purchase_value];

        $transaction_data[] = $new_row;

 

} return $transaction_data;

}

 

Is there any better way than this? Or can this code be easily improved?

 

Help appreciated :) Thanks.

Link to comment
Share on other sites

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.