This class allows you to easily create multi-level ordered, unordered, definition, and custom lists based on an intuitive array structure. Every class I write is aimed at making programming easier. Putting together multi-level HTML lists that validate is one of those tedious things that I set out to simplify. So now, instead of coding:
<ul> <li>List <ul> <li>Item 1</li> <li>Item 2 <ul> <li>Sub Item 2</li> </ul></li> </ul></li> <li>More</li> </ul>
To get:
Your code will look like:
$li = array(); $li[] = 'List'; $li[][] = 'Item 1'; $li[][] = 'Item 2'; $li[][][] = 'Sub Item 2'; $li[] = 'More';
Creates a new lister object.
Example | $list = new Lister; |
Creates an ordered list from the $array provided.
$array | The array with which to create an ordered list. Can either be multi-dimensional, or as in the example below. |
$enclose | An array of html tags such as 'b', 'big', 'i' etc. that you want to enclose every list item with. I would recommend using a style sheet, but this option is available. |
Returns | Your ordered list. |
Example | $li = array(); $li[] = 'List'; $li[][] = 'Item 1'; $li[][] = 'Item 2'; $li[][][] = 'Sub Item 2'; $li[] = 'More'; /* The above array's multi-dimensional counterpart would be: $li[0] = array(1=>'List', 5=>'More'); $li[1] = array(2=>'Item 1', 3=>'Item 2'); $li[3] = array(4=>'Sub Item 2'); */ $html .= $list->ordered ($li, array('b', 'i')); |
Creates an unordered list from the $array provided.
$array | The array with which to create an unordered list. Same as in the ordered method. |
$enclose | An array of html tags that you would like to enclose every list item with. |
Returns | Your unordered list. |
Example | $html .= $list->unordered ($li); |
Creates a definition list from the $array provided. This method does not have the enclose option.
$array | This array is going to be different than the ones we made for our ordered and unordered lists. All we need are name and value pairs, so the name is the arrays key, and it's value is the array keys value. |
Returns | Your definition list. |
Example | $def['Coffee'] = 'Black hot drink'; $def['Milk'] = 'White cold drink'; $html .= $list->definition ($def); |
This method allows you to create a custom list in case you don't like the flavors that html offers you.
$delimit | This is what you would like to indent each line with - spaces, tabs, smiley faces, etc. |
$list | Your custom list's array. Can either be multi-dimensional, or as in the ordered method example above. |
$begin | Different from delimit in that it doesn't multiply itself, so this is where you would put your dash, checkmark.gif, multicheck form box, etc. |
$end | What you want at the end of the line - a <br /> tag most likely. |
$enclose | An array of html tags that you would like to enclose every list item with. |
Returns | Your custom list. |
Example | $html .= '<pre>' . $list->custom ("\t", $li, '- ', '<br />') . '</pre>'; |
This is mainly a privately used method, but it comes in so handy for other things that I made it public.
$array | The same array as in as in the ordered method example above. |
Returns | Your multi-dimensional array. |
Example | $array = $list->make_multi_dimensional ($li); |
<?php /* * author: Kyle Gadd * documentation: http://www.php-ease.com/classes/lister.html * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ class Lister { private $list = array(); private $enclose = array('begin'=>'', 'end'=>''); private $delimit = ''; public function ordered ($array, $enclose='') { $list = ''; if (!empty($array) && is_array($array)) { $this->set_vars ($array, $enclose); $list .= $this->create ('ol'); $this->reset_vars(); } return $list; } public function unordered ($array, $enclose='') { $list = ''; if (!empty($array) && is_array($array)) { $this->set_vars ($array, $enclose); $list .= $this->create ('ul'); $this->reset_vars(); } return $list; } public function definition ($array) { $list = ''; if (!empty($array) && is_array($array)) { $list .= $this->indent(1) . '<dl>'; foreach ($array as $tag => $define) { $list .= $this->indent(2) . '<dt>' . $tag . '</dt>'; $list .= $this->indent(3) . '<dd>' . $define . '</dd>'; } $list .= $this->indent(1) . '</dl>'; } return $list; } public function custom ($delimit, $array, $begin='', $end='', $enclose='') { $list = ''; if (!empty($array) && is_array($array)) { $this->set_vars ($array, $enclose, $delimit, $begin, $end); $list .= $this->delimiter(); $this->reset_vars(); } return $list; } public function make_multi_dimensional ($array) { if (isset($array[0]) && is_array($array[0])) return $array; // already multi-dimensional $new = array(); $multi_dimensional = array(); foreach ($array as $key => $value) { list($depth, $value) = $this->array_depth($value); $new[$key+1][$depth] = $value; } $parent[1] = 0; // $depth => $parent foreach ($new as $key => $value) { list($depth, $value) = each($value); $group = $parent[$depth]; $multi_dimensional[$group][$key] = $value; if (isset($new[$key+1])) { // let's see what we have next $next = each($new[$key+1]); $next_depth = $next['key']; if ($next_depth > $depth) { // then this may be a new parent if (!isset($parent[$next_depth])) { $parent[$next_depth] = $key; } } elseif ($next_depth < $depth) { $difference = $depth - $next_depth; for ($i=0; $i<$difference; $i++) { array_pop($parent); // no longer related } } } // end if $next } return $multi_dimensional; } private function set_vars ($array, $enclose='', $delimit='', $begin='', $end='') { $this->list = $this->make_multi_dimensional ($array); if (!empty($begin)) $this->enclose['begin'] .= $begin; if (!empty($enclose) && is_array($enclose)) { $this->enclose['begin'] .= '<' . implode('><' , $enclose) . '>'; krsort($enclose); $this->enclose['end'] .= '</' . implode('></' , $enclose) . '>'; } if (!empty($delimit)) $this->delimit = $delimit; if (!empty($end)) $this->enclose['end'] .= $end; } private function reset_vars () { $this->list = array(); $this->enclose = array('begin'=>'', 'end'=>''); $this->delimit = ''; } private function array_depth ($array) { $depth = 1; if (is_array($array)) { while (is_array($array)) { list($key, $array) = each($array); $depth++; } } $value = $array; return array($depth, $value); } private function create ($tag, $parent='') { $list = ''; if (empty($parent)) $parent = $this->list[0]; if (!empty($parent)) { static $num = 0; $num++; $list .= $this->indent($num) . '<' . $tag . '>'; foreach ($parent as $child => $show) { if (!empty($show) || isset($this->list[$child])) { $list .= $this->indent($num) . '<li>' . $this->enclose['begin'] . $show . $this->enclose['end']; if (isset($this->list[$child])) $list .= $this->create($tag, $this->list[$child]); $list .= '</li>'; } } $list .= $this->indent($num) . '</' . $tag . '>'; $num--; } return $list; } private function delimiter ($delimiter='', $parent='') { $list = ''; if (empty($parent)) $parent = $this->list[0]; $delimiter .= $this->delimit; if (!empty($parent)) { static $num = 0; $num++; foreach ($parent as $child => $show) { if (!empty($show) || isset($this->list[$child])) { $list .= $this->indent($num) . $delimiter . $this->enclose['begin'] . $show . $this->enclose['end']; if (isset($this->list[$child])) $list .= $this->delimiter ($delimiter, $this->list[$child]); } } $num--; } return $list; } private function indent ($num) { $indent = "\n"; for ($i=0; $i<$num; $i++) { $indent .= ' '; } return $indent; } } ?>