Multiple Columns in PHP
This tutorial displays the results of a MySQL search in x number of columns in a table.
Example:
Try out following example to display 10 records per page.
<?php
// Connect database.
$host="localhost"; // Host name.
$db_user="root";
$db_password="root";
$database="programmersbank"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
$columns=3;
$query="select * from tutorials";
$result=mysql_query($query);
print "<table border='0' width='100%'>";
$x = 1;
while($cats = mysql_fetch_assoc($result)){
if($x == 1){ echo '<tr>';}
echo "<td><a href='#'>".$cats['title']."</a></td>";
if($x == $columns){ echo '</tr>';$x=0;}
$x++;
}
if($x != 1){
for($x=$x;$x<=3;$x++){ echo '<td></td>';}
echo '</tr>';
}
print "</table>";
?>
// Connect database.
$host="localhost"; // Host name.
$db_user="root";
$db_password="root";
$database="programmersbank"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
$columns=3;
$query="select * from tutorials";
$result=mysql_query($query);
print "<table border='0' width='100%'>";
$x = 1;
while($cats = mysql_fetch_assoc($result)){
if($x == 1){ echo '<tr>';}
echo "<td><a href='#'>".$cats['title']."</a></td>";
if($x == $columns){ echo '</tr>';$x=0;}
$x++;
}
if($x != 1){
for($x=$x;$x<=3;$x++){ echo '<td></td>';}
echo '</tr>';
}
print "</table>";
?>
