Updating WordPress mySQL db with data from CSV file - every day, automatically?
===

I'm trying to update specific tables in a WordPress database (tables were created by Formidable Pro WP Plugin) with data from a CSV file - this process needs to run automatically every day.

Here is the following code (placed in the functions.php file of my WordPress site):


    if (!wp_next_scheduled('my_csvtask_hook')) {
	wp_schedule_event( current_time(), 'hourly', 'my_csvtask_hook' );
    }

    add_action( 'my_csvtask_hook', 'my_csvtask_function' );
   

    function my_csvtask_function() {
       global $wpdb;
    $csv_url = "http://ribprototype2.mobilusdesign.com/wp-content/silverware/YUST_S01.csv";

    $fp = fopen($csv_url,'r') or die("can't open file");
    while($csv_line = fgetcsv($fp,1024)) {
      $custCode = $csv_line['0'];
      $points = $csv_line['13'];
      $cardNumber = $csv_line['33'];
      $qrySel = "select * from wp_frm_items where meta_value = ".$custCode;
      $itemdata = $wpdb->get_row($qrySel);
      $itemid = $itemdata->item_id;
      $updateQry = "update wp_frm_item_metas set meta_value = '".$points."' where  field_id='179' and item_id='".$itemid."'";
      $run_qry = $wpdb->query($updateQry);
      $updateQry1 = "update wp_frm_item_metas set meta_value = '".$cardNumber."' where field_id='180' and item_id='".$itemid."'";
      $run_qry1 = $wpdb->query($updateQry1);
      }
    fclose($fp) or die("can't close file");	
    }


The CSV file contains multiple columns - but the only ones that I need are the first (0), the fourteenth (13) and the thirty-fourth (33).

The Formidable Pro table in mySQL is called "wp_frm_item_metas" and contains several columns:

(i tried to upload an image - but I'm new to stacker :)

    id, meta_value, field_id, item_id

    1, 202020, 178, 4503
    2, 230, 179, 4503
    3, 303330, 180, 4503
    4, 202021, 178, 4504
    5, 354, 179, 4504
    6, 444404, 180, 4504

This table contains all the data from each form entry for each user.  Each user only has one entry.  The "item_id" column refers to the form submission for each user.  The specific fields (from the form) are found in the "field_id" column.  So basically, I need to update the appropriate meta_value for each user - for field_id 179 and 180 - the customer code is field 178.

So, in the CSV, I need to "UPDATE" the wp_frm_item_metas meta_values for field 179 and 180 by matching the customer code meta_value (for field 178) where the "item_id" are all equal (ie. all from the same user submission.  Each row of the CSV file refers to a specific "user" and the corresponding data is found in column 1 (field_id 178), column 14 (field_id 179) and column 34 (field_id 180).

And I need the update to run daily (as the CSV is pushed to the server every morning).

Any thoughts?

Mike