| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 
 |  
<?php
function cmp ($a, $b) {
	$sort = !empty($_GET["sort_by"]) ? $_GET["sort_by"] : "Artist";
	return strcmp($a[$sort], $b[$sort]);
}
usort($songs, "cmp");
 
function array_to_table($array, $printable) {
	//expects multi-dimensional array, all with the same keys
	$first_time=TRUE;
	$str = "<table border='1'>\n";
	$str .= "<tr>\n";
	foreach($array as $elem_key => $element) {
			if($first_time) {
				$header_items=array_keys($element);
					foreach($header_items as $header) {
						if(in_array($header, $printable)) {
							$str .= "<th><a href='" . $_SERVER["PHP_SELF"]
								. "?sort_by=" . urlencode($header) . "'>" . $header
								. "</a></th>\n";
						}
					}
				$str .= "</tr>\n";
				$first_time=FALSE;
			}
			$str .= "<tr>\n";
			foreach($element as $k => $v) {
				if(in_array($k, $printable)) {
					$str .= "<td>" . $v . "</td>\n";
				}
			}
			$str .= "</tr>\n";
	}
	$str .= "</table>";
	return $str;
}
$printable= array("Name", "Artist", "Album", "Size");
echo array_to_table($songs, $printable);
?> | 
Partager