Jump to: navigation, search

Php mylist

From w3cyberlearnings

Contents

PHP function mylist

This function makes HTML OL OR UL List from array.

Syntax mylist

  • array: array input for ol or ul list
  • option (optional):
    • option=1 for un-order list (Default)
    • option=2 for order list
  • return (optional):
    • return=1: return
    • return=0: not return but echo (Default)
mylist(array, $option=1, $return=0);

Function mylist()

<?php

function mylist($array, $option=1, $return=0) {
	$str_list = "";
	if (count($array) > 0) {
		// generate list
		foreach ($array as $li) {
			$str_list .='<li>';
			$str_list .= $li;
			$str_list .='</li>';
		}
		// for un-order list
		if ($option == 1) {
			$str_list = '<ul>' . $str_list . '</ul>';
		} else if ($option == 2) { // order list
			$str_list = '<ol>' . $str_list . '</ol>';
		}

		// check for return
		if ($return == 1) {
			return $str_list;
		} else {
			echo $str_list;
		}
	} else {
		return;
	}
}

?>

Example 1

<?php
$my_array = array('BMW', 'Ford', 'Honda', 'Toyota', 'Lexus', 'KIA', 'SCION');
mylist($my_array,2);

?>

Output: HTML Source

<ol>
<li>BMW</li>
<li>Ford</li>
<li>Honda</li>
<li>Toyota</li>
<li>Lexus</li>
<li>KIA</li>
<li>SCION</li></ol>

Template:Php html custom functions

Navigation
Web
SQL
MISC
References