CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Looking for suggestions on splitting first name from middle initial
Pages: [1]

Author Topic: Looking for suggestions on splitting first name from middle initial  (Read 1405 times)

twowheeler

  • I post occasionally
  • **
  • Posts: 115
  • Karma: 11
    • Harrisburg Christian Performing Arts Center
  • CiviCRM version: 4.3.4
  • CMS version: Drupal 7.22
  • MySQL version: 5.1
  • PHP version: 5.3
Looking for suggestions on splitting first name from middle initial
February 23, 2008, 06:48:14 pm
We have had volunteers pounding data into our civicrm database, which sometimes creates quality control problems.  ::)  At the moment we have a lot of records where the first name field contains both the first name and a middle initial.  In fact, over 900 of them.  Editing each record to fix it would take a long time.  Is there a way to fix this en masse?  I can select the records in Search Builder with first name equal to "% %", but am not sure what to do with them.  I'd like to just move the middle initial over to the middle name field.  Any suggestions appreciated.


Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Looking for suggestions on splitting first name from middle initial
February 23, 2008, 09:47:26 pm

your best bet would be to write a php script to go in and modify the table directly. alternatively if you are good at sql, you might even be able to do this in sql. If so please post back your solution for our education :)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

twowheeler

  • I post occasionally
  • **
  • Posts: 115
  • Karma: 11
    • Harrisburg Christian Performing Arts Center
  • CiviCRM version: 4.3.4
  • CMS version: Drupal 7.22
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Looking for suggestions on splitting first name from middle initial
February 24, 2008, 06:43:12 am
Heh.  That's what I was afraid you would say.  I'll work on it.

twowheeler

  • I post occasionally
  • **
  • Posts: 115
  • Karma: 11
    • Harrisburg Christian Performing Arts Center
  • CiviCRM version: 4.3.4
  • CMS version: Drupal 7.22
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Looking for suggestions on splitting first name from middle initial
February 24, 2008, 02:09:48 pm
Ok, this worked.

Code: [Select]
<?php

// Script to find & fix contacts with first name and middle name concatenated in the first name field

if (module_exists('civicrm')) {
civicrm_initialize(TRUE);

// get ids of contacts
$sql = "SELECT contact_id FROM civicrm_individual WHERE first_name LIKE '% _' ";
if (!$result = mysql_query( $sql )) {
echo 'No records selected<br>';
exit;
}

// for each id get the contact 
$count = 0;
while ($item = mysql_fetch_assoc($result)) {
$data = crm_fetch_contact($item);
$contact = crm_get_contact($item);
if ( ! is_a( $contact, 'CRM_Core_Error' )) {
$name = $data["first_name"];
list($fname, $minitial) = sscanf($name, "%s %s");
$params = array("first_name" => $fname, "minitial" => $minitial);
echo "Name: $name  First: $fname  Middle: $minitial<br>";
//crm_update_contact(&$contact, $params); // uncomment this line when you are ready
$count++;
} else {
echo 'Failed to get contact id='.$item['contact_id'].'<br>';
}
}
echo "$count contacts updated<br>";
}
?>


Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Looking for suggestions on splitting first name from middle initial
February 25, 2008, 01:03:47 pm
Would be great if you added this as a child page in the Cookbooks section - I suspect others will need it :-) (Please specify the CiviCRM version you developed it for.)...

http://wiki.civicrm.org/confluence/display/CRMDOC/Cookbooks
Protect your investment in CiviCRM by  becoming a Member!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Looking for suggestions on splitting first name from middle initial

This forum was archived on 2017-11-26.