PHPBuilder - Displaying records in 3 columns



RSS Twitter
Snippets Databases

Displaying records in 3 columns

by: Polina Soboleva
|
January 5, 2004

Version: 1

Type: Sample Code (HOWTO)

Category: Databases

License: GNU General Public License

Description: Very easy way for displaying records in 3 columns (horizontally & vertically)



Displaying results in 3 columns vertically:
<?

$Asql ="SELECT * FROM clients ORDER BY site";
		
	  	$cli_query = mysql_query($Asql) or die("Error");
      $cli_num = mysql_num_rows($cli_query);
if($cli_num > 0) { 
//we divide the number of records per 3 to see the number of records in the column
$column=round(($cli_num / 3),0);
$columns = $column * 2;

$x=0;
	
print("<table cellspacing=0 cellspacing=0 align=\"center\" border=0><tr><td valign=\"top\">");	  	  
	
		while($cli = mysql_fetch_object($cli_query)) {
		
$x=$x+1;

print("<a href=\"http://$cli->site\">www.$cli->site</a><br>");

if($x==round($column,0)||$x==round($columns,0)){
print("</td><td valign=\"top\">");
}

}//end while
print("</tr></table>");

}//end if num >0
 ?>
 If you have 8 records in TB "clients" you will get this:
 
 #################################
 # client1 #  client4 #  client7 #
 # client2 #  client5 #  client8 #
 # client3 #  client6 #          #
 #################################
 
 
 Displaying in 3 columns horizontally:
 <?
 $Asql ="SELECT * FROM clients ORDER BY site";
		
	  	$cli_query = mysql_query($Asql) or die("Error");
      $cli_num = mysql_num_rows($cli_query);
if($cli_num > 0) { //
	  $x=0;
print("<table cellspacing=0 cellspacing=0 align=\"center\">");	  	  	
      	while($cli = mysql_fetch_object($cli_query)) {
$x=$x+1;

if ($x % 3 == 0) {
print("<td><a href=\"http://$cli->site\">www.$cli->site</a></td></tr><tr>");
}else{
print("<td><a href=\"http://$cli->site\" >www.$cli->site</a></td>");
}

}//end while
print("</table>");
}//end if num >0
 ?>

  If you have 8 records in TB "clients" you will get this:
 
 #################################
 # client1 #  client2 #  client3 #
 # client4 #  client5 #  client6 #
 # client7 #  client8 #          #
 #################################

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Polina Soboleva

Comment:



Comment:

(Maximum characters: 1200). You have characters left.