Thursday, February 23, 2012

Split name in phplist to first and last name

March 14, 2010 by Ayo Akinyemi · Leave a Comment 

If you’ve got your phplist set up as “name” and you are wondering how to split this field into first and last name (import on phplist seem to break), follow these instructions:

1. Export the users’ ID and Name,
2. Split the “name” into 2 columns in excel
3. Save the document as a text file, separating the users’ ID and names using commas
4. Create a First name and Last name attribute in phplist
5. Run this code to get the first and last name into your database:

put opening php tag here

$dbhost = ‘localhost’;
$dbuser = ‘database_user’;
$dbpass = ‘database_user_password’;

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);

$dbname = ‘database_name’;
mysql_select_db($dbname);

if ($myFile = fopen(‘/home/filelocation/public_html/user_list.txt’,'r’)) // opens the file in the location you put it
{
while (! feof($myFile)) {

if ($s = fgets($myFile,1048576)) {
$line_from_text_doc = preg_split(‘/\,/’, $s, -1 , PREG_SPLIT_NO_EMPTY);

$id = $line_from_text_doc[0];
$first_name = $line_from_text_doc[1];
$last_name = $line_from_text_doc[2];

$result = mysql_query(“SELECT *
FROM `phplist_user_user_attribute`
WHERE `userid` = ‘$id’
AND `attributeid` =1″);

while($row = mysql_fetch_array($result)) {

mysql_query(“UPDATE `chanel_phplist`.`phplist_user_user_attribute`
SET `value` = ‘$first_name’
WHERE `phplist_user_user_attribute`.`attributeid` =1
AND `phplist_user_user_attribute`.`userid` = $id “);

mysql_query(“UPDATE `chanel_phplist`.`phplist_user_user_attribute`
SET `value` = ‘$last_name’
WHERE `phplist_user_user_attribute`.`attributeid` =2
AND `phplist_user_user_attribute`.`userid` = $id “);

//comment: attributeid = 1 assumes that is where your first name field is stored
//comment: attributeid = 2 assumes that is where your last name field is stored
}// end if $s

}// end while
} // end if $myFile

fclose($myFile);
}
echo “Done splitting name into first name and last name!”;

Note: My code ignores the middle name, you can edit the code to insert the middle name as well, just change the attribute id.

Thanks to Ola Akinyemi for splitting the name field into first name and last name using excel. Saved me sometime figuring this myself.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.