Although it easy fairly easy to pull results from your database and display them, often times you might want to display them in a crisp table format. The below script will help you display query results evenly through columns.
-
<?php
-
include 'config.php';
-
include 'opendb.php';
-
$table = 'your table here';
-
$query = "SELECT name FROM $table WHERE variable='$var' GROUP BY name"; //you can get rid of variable if not needed
-
$result = mysql_query($query);
-
-
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
-
$count = 0;
-
$columns = 3; //column number declaration
-
if(mysql_num_rows($result)) {
-
while($myrow = mysql_fetch_array($result)) {
-
// display list if there are records to display
-
if($count == 0) echo "<tr>\n";
-
printf("<td><a href=\"%s%s.html\">%s %s</a></td>\n", $php_SELF, $myrow["name"], $myrow["name"], $myrow[""]);
-
if($count == $columns - 1) echo "</tr>\n";//here without the!
-
$count = ++$count % $columns;
-
}
-
if($count!= 0) echo "</tr>\n";
-
echo "</table>\n";
-
} else {
-
// no records to display
-
echo "Sorry, no records were found!";
-
}
-
include 'closedb.php';
-
?>
Posted under Web Design
This post was written by Coyol on September 12, 2008
