Skip to content
Derek Jones edited this page Jul 5, 2012 · 8 revisions

Introduction

I noticed my view files get confusing because of all the little checks that have to be made when displaying values. For example

// data
array(
   array('start_date'=>'2008-01-01','total'=>'10'),
   array('start_date'=>'0000-00-00','total'=>'0')
);
// view
<?php foreach($rows as $row): ?>
<tr>
<td>&lt;?php if($row['start_date'] != '0000-00-00') echo $row['start_date'] ?&gt;</td>
<td>&lt;?php if($row['total'] != '0') echo $row['start_date'] ?&gt;</td>
</tr>
&lt;?php endforeach ?&gt;

This is why i created the display helper.

Code The code is split up in two parts. There are constants and there is the display_helper.php file

The constants are added to the config/constants.php files and make it easier for you to change the default value where ever you used it.

/*
| -----------------------------------------------------------------------
| Defaults
| -----------------------------------------------------------------------
*/
define('DEFAULT_DATETIME',                       '0000-00-00 00:00:00');
define('DEFAULT_DATETIME_NO_SEC',                '0000-00-00 00:00');
define('DEFAULT_DATE',                           '0000-00-00');
define('DEFAULT_TIME',                           '00:00:00');
define('DEFAULT_NUMBER',                         '0');

Add as many constants as you need.

The display_helper.php file contains the functions used in the view file

/* 
| ----------------------------
| show value if it's not the same as the default value
| ----------------------------
*/
function display($value,$default,$replacement = NULL)
{
    return ($value == $default)?(( ! is_null($replacement))?$replacement:''):$value;
}
/* 
| ----------------------------
| check if value is (not) the same as the default value
| ----------------------------
*/
function is_default($value,$default)
{
    return ($value == $default)?TRUE:FALSE;
}
/* 
| ----------------------------
| show value or a formatted string with the value in it when the value exists
| ----------------------------
*/
function display_isset($value,$str='')
{
   if( ! isset($value))
   {
       return '';
   }

   if($str != '')
   {
      return sprintf($str,$value);
   }

   return $value; 
}

It are only a few functions but they make view files a lot easier to read.

Usage The example view code with the helper is

&lt;?php foreach($rows as $row): ?&gt;
<tr>
<td>&lt;?php echo display($row['start_date'],'0000-00-00') ?&gt;</td>
<td>&lt;?php echo display($row['total'],'0') ?&gt;</td>
</tr>
&lt;?php endforeach ?&gt;
// or with the constants
&lt;?php foreach($rows as $row): ?&gt;
<tr>
<td>&lt;?php echo display($row['start_date'],DEFAULT_DATE) ?&gt;</td>
<td>&lt;?php echo display($row['total'],DEFAULT_NUMBER) ?&gt;</td>
</tr>
&lt;?php endforeach ?&gt;

An example of the display_isset function

// without helper
<p&lt;?php if(isset($class)): ' class="'.$class.'"'; endif ?&gt;>hello world</p>
// with helper
<p&lt;?php echo display_isset($class,' class="%s"') ?&gt;>hello world</p>

Updates 29-08-2008 : added replacement parameter to display function. Usage ;

display($row['start_date'],DEFAULT_DATE,date('Y-m-d'))

Category:Contributions::Helpers::Miscallenous

Clone this wiki locally