Jump to content

php mysql select col1, col2 from tbl where col1 or col2 in string


antonyfal

Recommended Posts

Hi..

I am working with .csv files.

The app allows a user to import contacts into a database table. I would like to prevent duplicates being added under the users id in the table.

what i need is a function to make sure that the email in a string does/does not match the email in one of two columns IE: email and work email...

 

here is a simple breakdown of my code..

 $nlines = explode("\n",file_get_contents($our_name));  //gets each line from the csv file
    $arr_head = CSV::get_header_fields( $db, $our_name, 'utf8', $seperators, '"', '\\' ); // custom function to get the first line headers
    $num = count($arr_head);     // count the headers as some data columns do not match the headers columns
    $nwFile = fopen($new_file, 'w');
    $newarray ='';
foreach(array_values($nlines) as $value){
 $value = str_replace('"', '', $value);
 $value = str_replace('\'', '', $value);
 $value = str_replace($seperators, ',', $value); // i have a function that gets the seperator, here i change all to a comma
$exists = mysql_query("SELECT email, work_email FROM `contact` WHERE `userid` = '".$User->id."'"); //user id is determined prior
  while($row=mysql_fetch_array($exists)) {
  if(strpos(strtolower(str_replace(',',' ',$value)), strtolower($row['email'])) === false && strpos(strtolower(str_replace(',',' ',$value)), strtolower($row['work_email'])) === false)
  {
  $nnmu = explode(',', $value);
  $totnum = count($nnmu);  // count the value array to see if it is equal to the headers row.
  if($totnum == $num){
   $newarray .= trim($value).','.$thisJRUser->id."\n";
   } else {
   if (preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/si', $value))  // if it is not equal to headers row, we take only rows with emails in it and append the difference in columns to the end of the row.
   {
   $nval = $num - $totnum;
   $valfront = split_nth(implode(',',$arr_head),',', $nval);
   $string = preg_replace('~[^,]~','',trim($valfront[0]));   //here we take the cloumns and remove everything  but the comma
   $newarray .= trim($value).',,'.$string.',,,'.$thisJRUser->id."\n";   // here we add the extra columns to the short value string + 5 extra commas as we are always 5 short :-) it works perfectly..
   }
   }
   }
   }
}
 
fwrite($nwFile,$newarray); here we write it all to a new csv file named after userid..
fclose($nwFile);

 

problem im having with this function is that it repeats the header row before it shows the second row of data... so i have 2 headers and data..

 

Can anyone see my error, or have a cleaner solution to this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.