Skip to content

definition list helper

World Wide Web Server edited this page Jul 4, 2012 · 6 revisions

This is an extension of the HTML_helper to include definition list

capability.

The function dList() can be called with an array in the following format:

[code]$list = array( array('dt' => 'Definition Term'), array('dd' => 'Definition definition') )[/code]

Also, if you would like to make a definition list at the end of a series of regular list items, you may stick the definition list array at the end of a series of ordered or unordered list arrays. This function will not allow any other lists to be nested within a definition list.

[code] <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /**

// ------------------------------------------------------------------------

/**

  • Definition List
  • Generates an HTML definition list from an associative array. Use "dt"
  • and "dd" as keys and set the value as an array.
  • @author Bradford Mar
  • @access public
  • @param array
  • @param mixed
  • @return string */
    if ( ! function_exists('dlist')) { function dlist($list, $attributes = '') { return _list('dl', $list, $attributes); } }

// ------------------------------------------------------------------------

/**

  • Generates the list

  • Generates an HTML ordered list from an single or multi-dimensional array.

  • @access private

  • @param string

  • @param mixed

  • @param mixed

  • @param intiger

  • @return string */
    if ( ! function_exists('_list')) { function _list($type = 'ul', $list, $attributes = '', $depth = 0) { // If an array wasn't submitted there's nothing to do... if ( ! is_array($list)) { return $list; }

     // Set the indentation based on the depth
     $out = str_repeat(" ", $depth);
    
     // Were any attributes submitted?  If so generate a string
     if (is_array($attributes))
     {
         $atts = '';
         foreach ($attributes as $key => $val)
         {
             $atts .= ' ' . $key . '="' . $val . '"';
         }
         $attributes = $atts;
     }
    
     //flag to substitue type with definition list
     $dltype = array_key_exists('dt',$list[key($list)]);
    
     // Write the opening list tag or replace with "dl"
     $out .= "<". (($dltype) ? "dl" : $type).$attributes.">\n";
    
     // Cycle through the list elements.  If an array is 
     // encountered we will recursively call _list()
    
     static $_last_list_item = '';
     foreach ($list as $key => $val)
     {    
         $_last_list_item = $key;
    
         $out .= str_repeat(" ", $depth + 2);
    
         //Don't bracket definition items with an <li>
         if ( ! $dltype)
         {
             $out .= str_repeat(" ", $depth + 2);
             $out .= "<li>";
         }
             
         if ( ! is_array($val))
         {
             $out .= $val;
         }
         
         //Definition list loop
         elseif ($dltype) 
         {
             foreach ($val as $dltag => $dlitem)
             {
                 $out .= str_repeat(" ", $depth + 2)."<". $dltag .">".$dlitem."</". $dltag .">\n";
             }
         }
         //Return to normal list loop
         
         else
         {
             $out .= $_last_list_item."\n";
             $out .= _list($type, $val, '', $depth + 4);
             $out .= str_repeat(" ", $depth + 2);
         }
         
         //No <li> brackets around definition items
         if ( ! $dltype) $out .= "</li>\n";        
     }
    
     // Set the indentation for the closing tag
     $out .= str_repeat(" ", $depth);
    
     // Write the closing list tag replace with dl if necessary
     $out .= "</". (($dltype) ? "dl" : $type).">\n";
    
     return $out;
    

    } }

/* End of file MY_html_helper.php / / Location: ./system/application/helpers/MY_html_helper.php */ [/code]

Clone this wiki locally