Return to Snippet

Revision: 14398
at June 2, 2009 14:28 by kilrizzy


Initial Code
function tabulardata($tablename,$columnstring){
	$columns = explode("|",$columnstring);
	$field_query = mysql_query("SELECT * FROM ".$tablename."");
	if(mysql_num_rows($field_query) > 0){
		//Get field data
		echo("<table>\n");
		echo("<tr>\n");
		$columnnames = array();
		foreach($columns as $column){
			if(strstr($column,"=")){
				$columninfo = explode("=",$column);
				$columntitle = $columninfo[1];
				$columnnames[] = $columninfo[0];
			}else{
				$columntitle = $column;
				$columnnames[] = $column;
			}
			echo("<td>".$columntitle."</td>\n");
		}
		echo("</tr>\n");
		while($field_array = mysql_fetch_array($field_query)){
			echo("<tr>\n");
			foreach($field_array as $field_key=>$field_value){
				if(in_array($field_key,$columnnames) && $field_key != ""){
					echo("<td>".$field_value."</td>\n");
				}
			}
			echo("</tr>\n");
		}
		echo("</table>");
		
	}
}

Initial URL
http://www.jeffkilroy.com

Initial Description
Use this function to print out all the rows in a table. Specify columns to show using the columnstring parameter:
tablulardata(tablename, columnstring);
ie:
tabluardata("users","username=Username|fname=First Name|lname=Last Name");

Initial Title
PHP/MySQL - Get Table Data

Initial Tags
mysql, database, php, data

Initial Language
PHP