Splitting out existing records into separate fields

4 posts by 3 authors in: Forums > CMS Builder
Last Post: November 29, 2013   (RSS)

By gkornbluth - November 27, 2013

Hi zaba,

Take a look at my recent post at,

http://www.interactivetools.com/forum/forum-posts.php?extracting-an-email-address-from-a-text-field-79134

Dave's input might be of some help.

In that approach, I think the trick is going to be finding a common pattern (like the <br>) to get the individual segments out of the array.

Wish I had a good solution for you but hopefully someone will.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Daryl - November 28, 2013

Hi zaba,

We're in luck if the full addresses are separated by <br> tags as we can use explode with the <br> tag as our delimiter. For example:

$fullAddress = "address 1 <br> address 2 <br> city  <br> postcode <br> country";
$addressPieces = explode('<br>', $fullAddress);

The code above will give us an array values for $addressPieces that we can use to save the "separated" address to their own field:

/* array values of $addressPieces
Array (
[0] => address 1
[1] => address 2
[2] => city
[3] => postcode
[4] => country
)
*/
$columnToValues = array(
'address_line1' => $addressPieces[0],
'address_line2' => $addressPieces[1],
'city ' => $addressPieces[2],
'postcode ' => $addressPieces[3],
'country' => $addressPieces[4],
)
mysql_update($tableName, $recordNum, null, $columnToValues);

And using the sample code above, what I would do is I will write a one-time script  that I will run once to loop through all the old records that needs the full address field to be separated into several fields. Explode their full address, and put them in the right field.

For example:

foreach($accoutnRecords as $record){
    if ($record['full_address']){
        $addressPieces = explode('<br>', $record['full_address']);
        
        $columnToValues = array(
            'address_line1' => $addressPieces[0],
            'address_line2' => $addressPieces[1],
            'city'          => $addressPieces[2],
            'postcode'      => $addressPieces[3],
            'country'       => $addressPieces[4],
        )
        mysql_update($tableName, $record['num'], null, $columnToValues);
    }
}

Hope this help!

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com

By zaba - November 29, 2013

Daryl,

thanks for this, I will take a look at your suggestion and report back, many thanks!