PDA

View Full Version : Display Result from Query in more then 1 column


vingne
08-25-2005, 09:38 AM
Hey.

I want to make a query to a database, and display it on my php page either through php code or with 2 queries to my mysql database.
This is how i want it displayed:

Data 1 Data 2
Data 3 Data 4
Data 5 Data 6
Data 7 Data 8
... ....
Data X Data X


This is my current code which i use and get it displayed in 1 row:

$db->Query("SELECT * FROM Applications WHERE ugp_id='".$ugp."' ORDER BY app_name","applications");
echo "<table class='main'>\n";
while ( $applications = mysql_fetch_assoc($db->res['applications'])) {
echo " <tr>\n";
echo " <td>\n";
echo " <a href='index.php?s=applications&applications=show&id=" .$applications['app_id'] ."'>" . $applications['app_name'] . "</a>\n";
echo " </td>\n";
echo " </tr>\n";
}
echo "</table>\n";

vingne
08-25-2005, 10:26 AM
After googling on the subject for some time i found a simple and good script to this:

$db->Query("SELECT * FROM Applications WHERE ugp_id='".$ugp."' ORDER BY app_name","applications");
$num_rows = mysql_num_rows($db->res['applications']);

$columns = 3;
echo "<table class='main'>\n";
for($i = 0; $i < $num_rows; $i++) {
$application = mysql_fetch_assoc($db->res['applications']);
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo " <tr>\n";
}
echo " <td width=50>\n";
echo " &nbsp;\n";
echo " </td>\n";
echo " <td>\n";
echo " <a href='index.php?s=applications&applications=show&id=" .$application['app_id'] ."'>" . $application['app_name'] ."</a>\n";
echo " </td>\n";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//if there is a remainder of 1, end the row
//or if there is nothing left in our result set, end the row
echo "</tr>\n";
}
}
echo "</table>\n";