Section Editor Default Values
2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 12, 2013 (RSS)
By furcat - July 11, 2013
If I set up a section editor, and for the field values enter a default value, is there a way to force any new and/pr existing entry to use the default value? Or, could I enforce the default value unless a different value is entered? So, for instance, I set the address to a default in the section editor. Then, if there are multiple entries, and the fields are left blank, I would like to use the default. However, if the field has a value entered (i.e. something different) in one of the entries, then show that instead of the default?
I'm trying to find a way to save a bunch of typing if a value is going to be the same most of the time, but in some cases will be different?
Thanks.
By gregThomas - July 12, 2013
Hi furcat,
If you set a default value, any new records created will start to use that default value. But to replace old values you'll have to write a script to replace old default values with the new default values. Something like this should work:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('C:/wamp/www/test/','','../','../../','../../../');
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 'blog'
list($blogs, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'loadUploads' => true,
'allowSearch' => false,
));
foreach($blogs as $blog){
echo "Checking record ".$blog['num']." <br>";
if($blog['title'] == "test"){
mysql_update('blog', $blog['num'], null, array('title' => 'Default Title'));
}
}
echo "Script Complete";
This is just example code, so you'll have to make a few changes to get it working with your site. Ensure you make a full back up of your database before running this script.
So I've set up a getRecords function to retrieve all of records from my blog section. Then it cycles through them using a foreach loop, and checks if the title matches an old value (in this case test). If it does, then a mysql_update function is used to replace the title of that record with a new value (in this case Default Title).
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com