Output mySQL data in columns

Submitted by:David Villa

Date added:05 April, 2015

Category:PHP

A simple PHP script that outputs data into formatted columns.

Tags: output data , mysql data

Code Snippet:

<?PHP

// show data from a mysql query in columns
// change the query below as needed, and the
// "$cols" variable to set the number of columns

$the_query = "SELECT * FROM mytable";

$result = MYSQL_QUERY($the_query);

$rows = MYSQL_NUM_ROWS($result);
$rcounter = 1;
$cols = 2;
PRINT ('<table>\n');

FOR($i = 0; $i < $rows / $cols; $i++) {
PRINT ('<tr>');
FOR($j=0; $j < $cols && $rcounter <= $rows ;$j++, $rcounter++) {
PRINT ("<td>(data to be displayed goes here)</td>\n");
}
PRINT ('</tr>\n');
}

PRINT ('</table>\n');

?>
 
 

Comments