Function to Check if Data Exists - With Lables
1 posts by 1 authors in: Forums > CMS Builder
Last Post: August 14, 2009 (RSS)
By InHouse - August 14, 2009
This is in reference to a thread found at:
http://www.interactivetools.com/forum/gforum.cgi?post=73660#73660
It's off-topic for that thread but might be helpful for some in it's own right.
Original Issue:
Response:
Just a comment here, but in the past I recall writing a function that would check if the field had data. Then, where you would normally have your IF statement you would just use something like this instead:
This saves you a lot of IF stmts and helps keep your presentation code really clean.
If memory serves I also added arguments to the function where you could pass controls or text strings to use as defaults or labels if you needed them.
I found one version... A bit overkill for the simple "if !empty" situation but you can easily pare it back to basics. It also includes the presentation label.
http://www.interactivetools.com/forum/gforum.cgi?post=73660#73660
It's off-topic for that thread but might be helpful for some in it's own right.
Original Issue:
I use CMSB for many classified/listings style sites so there might be 50 fields for a record in the database but only an average of 24 are used with each record...so that all of the fields don't reveal to the end users, only the ones that have data, nearly every field needs coding like:
<?php if ($inventoryRecord['fieldname_1']): ?>
<?php echo $inventoryRecord['fieldname_1'] ?><br/><br/>
<?php endif ?>
I would pay for a plugin for the automatic code generator that put in the php if statement for me on those items. It would save me a bunch of cut and paste time on the sites I build.
Response:
Just a comment here, but in the past I recall writing a function that would check if the field had data. Then, where you would normally have your IF statement you would just use something like this instead:
<?php echo hasData($record['fieldname']); ?>
This saves you a lot of IF stmts and helps keep your presentation code really clean.
If memory serves I also added arguments to the function where you could pass controls or text strings to use as defaults or labels if you needed them.
I found one version... A bit overkill for the simple "if !empty" situation but you can easily pare it back to basics. It also includes the presentation label.
#USAGE: echo isAvailable($record['address_1'], 'Address');
#USAGE: echo isAvailable($record['address_1'], 'spacer'); // For fields without a label.
# FUNCTION TO HANDLE THE DISPLAY OF LABELS & DATA
# ONLY DISPLAYS ON SCREEN IF DATA IS PRESENT
function isAvailable($dataSet, $label) { // place '' in label argument if not needed.
if (!empty($dataSet)) { // Check for some data in the field.
if (!empty($label)) { // Check for a label name to use.
$dataLabel = $label.': ';
if ($label == 'spacer') { # manages address lines 2&3 and other cases where we don't want a label but we do want the spacing.
$dataLabel = ' '; // inserts blank character - needed to stop span from collapsing. Do not remove!
}
} else {
$dataLabel = '';
}
# Everything is ready. Create the string to display. Include format & styles.
if (strstr($dataSet, '@')) { #data is an email addy. Format live link and overwrite dataSet.
$dataSet = '<br /><span class="dataLabel">' . $dataLabel . '</span><a href="mailto:' . $dataSet . '">' . $dataSet . '</a>';
} else { # Data is just a normal string.
$dataSet = '<br /><span class="dataLabel">' . $dataLabel .'</span>' . $dataSet;
}
} else {
$dataSet = ''; // No data found. Display nothing.
}
return $dataSet;
}
I hope this is useful for someone.
J.