EXTRACTING DATA FROM A TEXT FIELD AND INSERTING IT INTO ANOTHER FIELD - Dec 29th, 2018
|
My client had a text box field called internal_notes in the accounts section which held information about that user.
I needed to check for certain existing data in the text field (in this case the word ‘free’) and insert a value of ‘2' into another field (membership_level) in that user’s record.
Daryl Maximo, a programmer at Interactive Tools came to the rescue with the following function, which will run as soon as the web page containing the code is loaded.
CAUTION: Back up Your database(s) in Admin>general Settings before attempting any changes so that you can restore the unaltered data if there’s a problem.
<?php // load viewer library $libraryPath = 'cmsAdmin/lib/viewer_functions.php'; $dirsToCheck = array('path_to_your_root_directory/','','../','../../','../../../'); foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }} if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load records from 'accounts' list($accountsRecords, $accountsMetaData) = getRecords(array( 'tableName' => 'accounts', 'loadUploads' => true, 'allowSearch' => false, ));
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head>
<body>
<?php function extract_membership_level_from($string){ $myword = 'free' ; preg_match_all("/\b" . $myword . "\b/i", $string, $matches); return $matches[0]; } ?> <?php foreach ($accountsRecords as $record) { $allLevel = extract_membership_level_from( $record['internal_notes'] ); if ($record['membership_level'] || !$allLevel ) { continue; }// skip records that already have a membership level set (or where there’s no match for $myword in internal_notes ) $firstLevel = '2'; print "User {$record['username']} will get this level: $firstLevel\n"; mysql_update('accounts', $record['num'], null, array('membership_level' => $firstLevel )); } ?>
</body> </html>
TESTING The print line of code will show you what the result of your efforts will be. Comment out the mysql_update code by adding double forward slashes // before the line until you’re sure that the code will render the appropriate results and then remove the double slashes and reload the page. MODIFICATION This function can be easily modified to extract any information string from any text field in any table and insert that value or any other value into another field by changing one value in the function itself: $myword = 'the_string_you’re_searching_for '
And a few values in the foreach loop: The table name that you’re looping through (in the foreach). The field you’re searching for (in the $allLevel variable). What to replace the string with if found (in the $first_level variable). And the table and field where you want to insert that value (in the mysql_update line).
CAVEAT: Don’t forget to adjust the Load Viewer Library path and Load Records values at the top of the page to suite your needs.
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.