PHP transform array to an HTML table

PHP transform array to an HTML table

//---Show an array content (1st level) in a HTML table format
// $option = array(
//		'order' => '{field}',	- list fields on which order and show
//		'options' => array(
//			'field_color_row' => 'red' / '#FFFFFF'	// row color
//			'cellpadding' => '5' / '#FFFFFF'		// cellpadding table
//			'cellspacing' => '0' / '#FFFFFF'		// ininfluente con bordercollapse
//			'classtable' => 'nameClassTable' 		// class table
//		),
//		'{field}' => array(
//			'header' => '{Name Field 1}'				//Header column
//			'type' => 'date' / 'integer', 'income'		//type data
//			'align' =>	'left' / 'center' / 'right'		//align
//			'class' => 'nameClass'					//column class <td>
//			'unit' => 'kg'								//unit measure
//		)
// $id - id table to assign
function showTable($array, $options = array(), $id = ''){
	if(is_array($array))  {
		//---reorder $array defined in $options['order']
		if(isset($options['order'])) {
			
			$array1 = array();
			$array_order = $options['order'];
			foreach ($array as $key => $item){
				$array2 = array();
				foreach ($array_order as $field){
					$array2[$field] = $item[$field];
				}
				$array1[$key] = $array2;
			}
			
			$array = $array1;
		}
		
		//---options field on which set color (if not defined alternate color)
		$field_color_row = "";
		if(isset($options['options']['field_color_row'])) {
			$field_color_row = $options['options']['field_color_row'];
		}

		//---options cellpadding
		$cellpadding = "0";
		if(isset($options['options']['cellpadding'])) {
			$cellpadding = $options['options']['cellpadding'];
		}
		//---options cellspacing
		$cellspacing = "0";
		if(isset($options['options']['cellspacing'])) {
			$cellspacing = $options['options']['cellspacing'];
		}
		//---options cellpadding
		$classtable = "";
		if(isset($options['options']['classtable'])) {
			$classtable = $options['options']['classtable'];
		}
								
		?>
        <table <?= $id ? 'id="'.$id.'"' : ''; ?> <?= $classtable ? 'class="'.$classtable.'"' : '';?>" border="1" cellpadding="<?= $cellpadding;?>" cellspacing="<?= $cellspacing;?>" style="border-collapse:collapse">
        
        <?
		$n = 0;
		$prev = "";
		$flag_header = true;
		foreach ($array as $key => $item){

			if($field_color_row) {
				if($item[$field_color_row] != $prev) {
					$prev =	$item[$field_color_row];
					$n++;
				}	
			} else {
				$n++;
			}
			
			$class = "odd";
			if ($n % 2 == 0){
				$class = "even";
			}
				
			
			if($flag_header) {
				?>
                <thead>
                <tr class="head">
                <?
				foreach(array_keys($item) as $campo) { 
					if(isset($options[$campo]['header']) && $options[$campo]['header']) {
						$campo = $options[$campo]['header'];
					}
				
				?>
					<th><?= $campo ?></th>
                <?
				}
				?>
                </tr>
                </thead>
                <tbody>
                <?
				$flag_header = false;
			}//End if($flag_header) {
			?>
            	<tr class="<?= $class; ?>">
            <?
			if(is_array($item)) {
			foreach($item as $key => $el) { 
				$align = 'left';
				if(isset($options[$key]['type']) && $options[$key]['type']) {
					//---tipo formato
					switch($options[$key]['type']) {
//						case 'checkbox':
//							$align = 'center';
//							$el = '<input name="chk" type="checkbox" ' . ($el ? 'checked="checked"' : '') . ' value="' . $el . '" />';
//							break;
						case 'date':
							$el = RigiraData(EpuraData($el));
							break;
						case 'integer':
							$align = 'right';
							$el = frwt_printNumber($el, $dec = 0);
							break;
                                                case 'currency':
						case 'income':
							$align = 'right';
							$el = frwt_printNumber($el, $dec = 2);
							break;	
					}

					//echo "el=".$el."<br />";
					//die("TEST");
				}

				if(isset($options[$key]['unit']) && $options[$key]['unit']) {
					//---unit measure
					$unit = $options[$key]['unit'];
					$el .= " " . $unit;
				}

				if(isset($options[$key]['align']) && $options[$key]['align']) {
					//---align
					$align = $options[$key]['align'];
				}
				
				//---class
				$td_class = "";
				if(isset($options[$key]['class']) && $options[$key]['class']) {
					$td_class = $options[$key]['class'];
				}
				?>
					<td <?= $td_class ? 'class="'.$td_class.'"' : ''; ?> valign="top" align="<?= $align;?>"><?= $el ?></td>
                <?
			}//End foreach($item as $el) {
			}
			?>
            	</tr>
			<?
			
			
		}//End foreach ($array as $key => $item){
		?>
        </tbody>
        </table>
        <?
	}
}

Example with MySQL

$SQL = "SELECT name, surname, city FROM users");
$res = mysql_query($SQL);
while($item = mysql_fetch_assoc($res)) {
  $array[] = $item;
);

showTable($array);

Results

name surname city
Kobe Bryant Los Angeles
Mike Smith New York
Mark Anthony Chicago
Dave Sullivan San Francisco
This entry was posted in PHP and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s