How to handle IF statements for every field in a table?
Posted: Fri Jan 25, 2013 12:02 pm
How to handle an IF statement for every field in a table?
Because every client I've used this code for over the years wanted to play fast and loose with the values in most DB fields I've had to allow all kinds of values in most fields. For example there are a number of fields where you'd assume you can only enter an integer. But most clients want to enter short phrases or whole paragraphs.
So on many fields I have to test if the value is numeric or a string.
Display strings as-is but run numbers through number_format and add a dollar sign.
Some numbers just need to be run through number_format without the dollar sign.
Or run it through a square foot to acre converter and add 'acres' or "sq ft" suffix.
Or test if the value is a csv of strings and put a space between each comma.
Or if it's a csv of id numbers pull the related records from the database.
Or if it's a multi-line string convert it to an unordered list on some pages and a comma-space separated list on other pages.
If the value is zero, don't show a zero.
Some values are a zero or one and should be shown as yes or no.
Some values are y or n and should be shown as yes or no.
So what I've had to do is set up a bunch of IF statements for each field. The next problem was that there are many sections on the site where many of these fields would be displayed. So what I did next was to turn each field-specific IF statement into its own function. Later I grouped all those functions into a helper class.
Below is that helper class and an example of its usage.
Does this seem like a good idea? Is there a better way I could be handling this?
Usage:
Helper Class:
Because every client I've used this code for over the years wanted to play fast and loose with the values in most DB fields I've had to allow all kinds of values in most fields. For example there are a number of fields where you'd assume you can only enter an integer. But most clients want to enter short phrases or whole paragraphs.
So on many fields I have to test if the value is numeric or a string.
Display strings as-is but run numbers through number_format and add a dollar sign.
Some numbers just need to be run through number_format without the dollar sign.
Or run it through a square foot to acre converter and add 'acres' or "sq ft" suffix.
Or test if the value is a csv of strings and put a space between each comma.
Or if it's a csv of id numbers pull the related records from the database.
Or if it's a multi-line string convert it to an unordered list on some pages and a comma-space separated list on other pages.
If the value is zero, don't show a zero.
Some values are a zero or one and should be shown as yes or no.
Some values are y or n and should be shown as yes or no.
So what I've had to do is set up a bunch of IF statements for each field. The next problem was that there are many sections on the site where many of these fields would be displayed. So what I did next was to turn each field-specific IF statement into its own function. Later I grouped all those functions into a helper class.
Below is that helper class and an example of its usage.
Does this seem like a good idea? Is there a better way I could be handling this?
Usage:
Code: Select all
<?
$model = new ListingsModel(15671);
$ldh = new ListingDisplayHelper( $model );
$F = $ldh;
?>
<tr><td>Property Name</td><td><?=$F->property_name?></td></tr>
<tr><td>Categories</td><td><?=$F->category?></td></tr>
<tr><td>Sale Price</td><td><?=$F->price?></td></tr>
<tr><td>Rent Price</td><td><?=$F->rent_price?></td></tr>
<tr><td>List Date</td><td><?=$F->list_date?></td></tr>
<tr><td>Island</td><td><?=$F->island?></td></tr>
<tr><td>Location</td><td><?=$F->location;?></td></tr>
<tr><td>District</td><td><?=$F->district?></td></tr>
<tr><td>Property Types</td><td><?=$F->property_types?></td></tr>
<tr><td>Key Features</td><td><?=$F->key_features?></td></tr>
<tr><td>Building_style</td><td><?=$F->building_style?></td></tr>
<tr><td>Bedrooms</td><td><?=$F->bedrooms?></td></tr>
<tr><td>Bathrooms</td><td><?=$F->bathrooms?></td></tr>
<tr><td>Half Baths</td><td><?=$F->halfbaths?></td></tr>
<tr><td>Lot Size</td><td><?=$F->lot_size?></td></tr>
<tr><td>Living Area</td><td><?=$F->living_area?></td></tr>
<tr><td>NRA</td><td><?=$F->net_rentable?></td></tr>
<tr><td>GRA</td><td><?=$F->gross_rentable_area?></td></tr>
<tr><td>Land Price/Sq.Ft.</td><td><?=$F->price_sq_ft?></td></tr>
<tr><td>Building Rental Rate/Sq.Ft.</td><td><?=$F->sq_ft_rate?></td></tr>
<tr><td>Total Expenses</td><td><?=$F->total_expenses?></td></tr>
<tr><td>Vacancy Rate</td><td><?=$F->vacancy_rate?></td></tr>
Code: Select all
my 2nd code block keeps disappearing. will add in reply.