PHP Scripts

3 column (or 4 or 5 etc) display results

Read a database, and display results.
The php default display is one column - here are the results in 3 or more columns.

<html>
<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">


<?php
$db = @mysql_connect("host", "name", "password");
if( ! ($db = @mysql_connect(
"host", "name", "password")) ) {
} else {
mysql_select_db("database",$db) or die("Select DB Error: ".mysql_error());
}

//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 3; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = "SELECT * FROM table";
$mysql_result = mysql_query($query, $db);

// get each row
while($myrow = mysql_fetch_row($mysql_result))
{

//get data - eg, reading fields 0 and 1
$tn = $myrow[0];
$in= $myrow[1];

if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}

// output row from database
echo "<td>$in $tn</td>\n";

// bump up row counter
$numcolsprinted++;

} // end while loop

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

}
print "<TD></TD>\n";

?>


</table>
</html>

 

Back

Source