PHP Scripts

2 column display results

This example reads a database, and displays results.
The php default display is one column, whereas this neatly packages the results in 2 columns.

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


<?php


//db connect and select
$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());
}
$result=mysql_query ("select * from table");

$count = 1;
$column = 1;

//loop statement
while ($myrow = mysql_fetch_array ($result))
{
// first column display
if ($column == 1)
{

//field is the column in your table
printf("<tr><td>%s</td>",$myrow["field"]);
}

else{
//second column display 
printf("<td>%s</td></tr>",$myrow["field"]);
}

$count += 1;

$column = $count % 2;
}
?>


</table>
</html>

 

Back

Source