Home Apache PHP Ubuntu MySQL Linux HTML Win CSS Perl Javascript Rants Retro
 
Print This Page
Date Posted: Saturday 27th of February 2010 | Category: MySQL
Output results horizontally using php & mysql
To output your results horizontally using php and mysql copy and paste the following code below.

Change the following code if(!($x%2)) to the number of cols you would like to output. This script is set to output 2 cols.

<?php
include "my_db_connection_details.php";
$dbcnx = @mysql_connect("$servername","$username", "$password");
if (!$dbcnx)
{ echo( "" );
exit(); }

// Connect to database
if (! @mysql_select_db("$database") ) { echo( "<P>Unable to locate the database " . "" );
exit();
}

// Request results from tables
$result = mysql_query("SELECT company_name FROM companies");

echo "<table width=\"100%\" cellspacing=\"15\"><tr>";

// Set Counter to 0
$x = 0;

while($row = mysql_fetch_array($result))
{
$x++;
echo "<td width=\"30%\">". $row['company_name']."></td>";

// Set the number of items to output before creating a new table
if(!($x%2))
{
//change the 3 to the nuber of cols you want
echo "</tr><tr>";
}
?>