Jump to content

Recommended Posts

Hi Everybody,

 

I am trying to do something simple and am just messing it up. I am pulling data from a file and then inserting / updating my db with it. I want to use a counter so that if the data id number is the same as the data set before it, it will skip the processing.

 

But I don't seem to be writing that part correctly. Here is my code:

 

<?php 

$fields = array('myid', 'episode_number', 'episode_name', 'secondary_name', 'series_id', 'category', 'subcategory', 'first_run', 'active', 'length', 'notes', 'ep', 'source','pdate', 'adate', 'purchase_date', 'exp_date', 'distributor', 'msn', 'msn_description', 'msn_program_type', 'msn_status', 'msn_filename');
$handle = fopen("VLM_master.txt", "r");
$counter = 1;
while (($data = fgetcsv($handle, 10000, "	")) !== FALSE) {
   
   $data = array_combine($fields, $data);
   $reduced = $counter -1;

if($data['myid'][$counter] != $data['myid'][$reduced]) {
// processing goes here
}
$counter++;
} // end in file
fclose($handle);
?>

 

Any help is much appreciated!

sw

Link to comment
https://forums.phpfreaks.com/topic/112057-solved-count-syntax-is-wrong/
Share on other sites

$data will be over-written with the new row of data...keep the ID in a separate variable:

 

<?php 

$fields = array('myid', 'episode_number', 'episode_name', 'secondary_name', 'series_id', 'category', 'subcategory', 'first_run', 'active', 'length', 'notes', 'ep', 'source','pdate', 'adate', 'purchase_date', 'exp_date', 'distributor', 'msn', 'msn_description', 'msn_program_type', 'msn_status', 'msn_filename');
$handle = fopen("VLM_master.txt", "r");
$last_id = null;
while (($data = fgetcsv($handle, 10000, "	")) !== FALSE) {
  $data = array_combine($fields, $data);
  if($data['myid'] != $last_id) {
    $last_id = $data['myid'];
    // processing goes here
  }
} // end in file
fclose($handle);
?>

Hi, thanks for the help. I will try to better explain....this didn't solve my problem.

 

So the data['myid'] variable is an arbitrary id number. It can be '10', '54377', '6109854', etc. I need to check in my loop if the $data['myid'] that I am currently on is equal to the last $data['myid']. If it is, then I want it to skip the processing. Is this possible?

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.