title: Import dynamic csv into MySQL
body: <p>I am looking for a way to import a csv file that have columns that may or may not be in the same order every time. also there may be extra columns some times..</p>
  
  <p>This is what I have so far...
  it loops through each row and inserts it into the mysqldb.</p>
  
  <p>my problem is that the columsn and column orders change sometime, NEVER the name of the columns tho. they just add and remove columns.</p>
  
  <p>i am trying to avoid having my client have to modify the csv extensivly before they import.</p>
  
  <p>I woulds like the script to see the column name in the csv and match it automatically to the column in the table..</p>
  
  <p>rather than relying on $data[1]... that specifies a specific column. </p>
  
  <pre><code>// Process Scrubbed acounts import
  if ($_FILES['scrubImport']['tmp_name']) {
  
      // Get the Rep Account ID
      ${'Rep Account'} = $_POST['importToRep'];
  
      // Get our total # of contacts
      $contacts = mysql_result(mysql_query('SELECT count(0) FROM `accounts` WHERE `owner` = '.${'Owner'}), 0);
  
      // Get our import file extension
      ${'Extension'} = strtolower(array_pop(explode('.', $_FILES['scrubImport']['name'])));
  
      // Store our contacts array
      ${'Accounts'} = array();
      // If our file is a CSV
      if (${'Extension'} == 'csv') {
  
          // Get our row data
          ini_set('auto_detect_line_endings',true);
          if (($handle = fopen($_FILES['scrubImport']['tmp_name'], "r")) !== FALSE) {
              while (($data = fgetcsv($handle)) !== FALSE) {
  
                  // Sanitize all our data
                  foreach ($data as $i =&gt; $v) { $data[$i] = inputCleanText($v); }
  
                  // Add the data to our contacts array
                  if($data[0] != 'Keyword'){
                      ${'Accounts'}[] = array('Keyword' =&gt; $data[0], 'Company Name' =&gt; $data[1], 'Owner First Name' =&gt; $data[2], 'Owner Last Name' =&gt; $data[3], 'Email' =&gt; $data[4], 'Phone' =&gt; str_replace('-','',str_replace(' ','',str_replace('(','',str_replace(')','',$data[5])))), 'Ranking On The 1st Page' =&gt; $data[6], 'Citations' =&gt; $data[7], '1st Category' =&gt; $data[8], 'Categories' =&gt; $data[9], 'Google+ Score' =&gt; $data[10], 'Google Reviews #' =&gt; $data[11], 'Google Reviews Found' =&gt; $data[12], '3rd Party Site Details' =&gt; $data[13], '3rd Party Site Summary' =&gt; $data[14], 'Google Bad Reviews #' =&gt; $data[15], 'Google Bad Reviews' =&gt; $data[16], 'IP Bad Reviews #' =&gt; $data[17], 'IP Bad Reviews' =&gt; $data[18], 'Images #' =&gt; $data[19], 'Videos #' =&gt; $data[20], 'Google+ Offers #' =&gt; $data[21], 'Google+ Offers Found' =&gt; $data[22], 'Groupon Offer' =&gt; $data[23], 'Groupon Offer Title' =&gt; $data[24], 'Groupon Purchase' =&gt; $data[25], 'Groupon Offer Expiration' =&gt; $data[26], 'Living Social Offer' =&gt; $data[27], 'Living Social Offer Title' =&gt; $data[28], 'Living Social Purchases' =&gt; $data[29], 'Living Social Expiration' =&gt; $data[30], 'Website URL' =&gt; $data[31], 'Keywords In Homepage' =&gt; $data[32], 'Mobile Optimized Site' =&gt; $data[33], 'Website Backlinks' =&gt; $data[34], 'Website Page Rank' =&gt; $data[35], 'Google+ URL' =&gt; $data[36], 'Owner Verified' =&gt; $data[37], 'Facebook Page URL' =&gt; $data[38], 'FB Likes #' =&gt; $data[39], 'FB Talking About This #' =&gt; $data[40], 'Twitter Page URL' =&gt; $data[41], 'Twitter Tweets #' =&gt; $data[42], 'Twitter Followers #' =&gt; $data[43], 'Twitter Page Found?' =&gt; $data[44], 'Address' =&gt; $data[45], 'City' =&gt; $data[46], 'State/Region' =&gt; $data[47], 'Postal Code' =&gt; $data[48], 'Country' =&gt; $data[49]);
  
                  }
              }
          }
          fclose($handle);
      }
  
      // Start looping thru the rows
      $row = 0;
      $valid_count = 0;
      foreach (${'Accounts'} as ${'Account'}) {
  
          // Increase our valid count
          ++$valid_count;
  
          // Add the account
          mysql_query('INSERT INTO `accounts` (`owner`, `company_name`, `contact_fname`, `contact_lname`, `office_phone`, `email_primary`, `address_1`, `city`, `state`, `zip`, `relationship`, `acquired_via`, `website`) VALUES ("'.${'Rep Account'}.'", "'.${'Account'}['Company Name'].'", "'.${'Account'}['Owner First Name'].'", "'.${'Account'}['Owner Last Name'].'", "'.${'Account'}['Phone'].'", "'.${'Account'}['Email'].'", "'.${'Account'}['Address'].'", "'.${'Account'}['City'].'", "'.${'Account'}['State/Region'].'", "'.${'Account'}['Postal Code'].'", "Scrubbed Prospect", "Scrubbed Account", "'.${'Account'}['Website URL'].'")') or die(mysql_error());
  
          ${'New Account ID'} = mysql_insert_id();
  
          // Add the additional scrub info                        
          mysql_query('INSERT INTO `crm`.`accounts_scrubbed` (`account_id`,`keyword`,`ranking_first_page`,`citations`,`1st_category`,`categories`,`google_plus_score`,`google_reviews`,`num_google_reviews`, `3rd_party_site_details`,`3rd_party_site_summary`,`#_bad_google_reviews`,`google_bad_reviews`,`#_ip_bad_reviews`,`ip_bad_reviews`,`#_images`,`#_videos`,`google_plus_offers`,`google_plus_offers_found`,`groupon_offer`,`groupon_offer_title`,`groupon_purchase`,`groupon_offer_expiration`,`living_social_offer`,`living_social_offer_title`,`living_social_purchases`,`living_social_expiration`,`keywords_in_homepage`,`mobile_optimized_site`,`website_backlinks`,`website_page_rank`, `google_plus_url`,`owner_verified`,`facebook_page_url`,`#_facebook_likes`,`#_facebook_talking_about_this`,`twitter_page_url`,`#_twitter_tweets`,`#_twitter_followers`,`twitter_page_found`,`country`) VALUES ("'.${'New Account ID'}.'", "'.${'Account'}['Keyword'].'", "'.${'Account'}['Ranking On The 1st Page'].'", "'.${'Account'}['Citations'].'", "'.${'Account'}['1st Category'].'", "'.${'Account'}['Categories'].'", "'.${'Account'}['Google+ Score'].'", "'.${'Account'}['Google Reviews Found'].'", "'.${'Account'}['Google Reviews #'].'", "'.${'Account'}['3rd Party Site Details'].'", "'.${'Account'}['3rd Party Site Summary'].'", "'.${'Account'}['Google Bad Reviews #'].'", "'.${'Account'}['Google Bad Reviews'].'", "'.${'Account'}['IP Bad Reviews #'].'", "'.${'Account'}['IP Bad Reviews'].'", "'.${'Account'}['Images #'].'", "'.${'Account'}['Videos #'].'", "'.${'Account'}['Google+ Offers #'].'", "'.${'Account'}['Google+ Offers Found'].'", "'.${'Account'}['Groupon Offer'].'",  "'.${'Account'}['Groupon Offer Title'].'", "'.${'Account'}['Groupon Purchase'].'", "'.${'Account'}['Groupon Offer Expiration'].'", "'.${'Account'}['Living Social Offer'].'", "'.${'Account'}['Living Social Offer Title'].'", "'.${'Account'}['Living Social Purchases'].'", "'.${'Account'}['Living Social Expiration'].'", "'.${'Account'}['Keywords In Homepage'].'", "'.${'Account'}['Mobile Optimized Site'].'", "'.${'Account'}['Website Backlinks'].'", "'.${'Account'}['Website Page Rank'].'", "'.${'Account'}['Google+ URL'].'", "'.${'Account'}['Owner Verified'].'", "'.${'Account'}['Facebook Page URL'].'", "'.${'Account'}['FB Likes #'].'", "'.${'Account'}['FB Talking About This #'].'", "'.${'Account'}['Twitter Page URL'].'", "'.${'Account'}['Twitter Tweets #'].'",  "'.${'Account'}['Twitter Followers #'].'", "'.${'Account'}['Twitter Page Found?'].'", "'.${'Account'}['Country'].'")');
  
          // Add the generated report information
          mysql_query('INSERT INTO `crm`.`report` (`account_id`, `generated`, `sent`, `report_id`) VALUES ("'.${'New Account ID'}.'", "0", "0", "'.md5(microtime()).'")');
  
          // Add the last touched Record
          mysql_query('INSERT INTO `last_touched` (`account_id`, `user_id`, `reason`) VALUES ("'.${'New Account ID'}.'", "'.$_SESSION['userID'].'", "Imported Scrub")');
  
          ++$row;
      }
  
      // Post a message
      ${'Message'} = '&lt;div class="success"&gt;Import successful. '.number_format($valid_count).' account(s) were imported sucessfully!&lt;/div&gt;';
  
  }
  </code></pre>
  
  <p>Im not sure if i am being completely clear on what i am trying to do.</p>