Bill Flippen Bill Flippen - 1 month ago 8
MySQL Question

Set tables as side by side instead of straight down while doing a while-loop

include 'inc.php';
$varVeh=$_POST['Veh_num'];

$sql_course="select course_num from hc_course";
$results_course=mysql_query($sql_course);

$sql_vehName="select Veh_name from hc_vehicle_type where Veh_num=$varVeh ";
$result_vehName = mysql_query($sql_vehName);
$vehName=mysql_fetch_assoc($result_vehName);

echo "<table><tr><th>Best Scores for".$vehName['Veh_name']."</th> </tr></table>";

while($rc = mysql_fetch_array($results_course)){
$sql_HiScores = "SELECT c.course_name as course, e.distance as distance, e.score as score, e.time as time, e.user as User from hc_entries e left join hc_course c on e.course=c.course_num WHERE c.course_num=".$rc['course_num']." and e.vehicle=$varVeh ORDER BY course, score DESC Limit 3 ";
$result_HiScores = mysql_query($sql_HiScores);

?>
<table border='1'>
<tr>
<th>Course</th>
<th>Score</th>
<th>Distance</th>
<th>Player</th>
<th>Time</th>
</tr>
<?php
while($row = mysql_fetch_array($result_HiScores))
{
echo "<tr>";
echo "<td>" .$row['course'] . "</td>";
echo "<td>" .$row['score'] . "</td>";
echo "<td>" .$row['distance'] . "</td>";
echo "<td>" .$row['User'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>


Currently this is creating tables down the page. I was wondering if there is a way of getting 2 tables side by side :

Table 1 Table 2

Table 3 Table 4

Table 5 Table 6

Or maybe even 3 if they will fit on the page.

Table 1 Table 2 Table 3

Table 4 Table 5 Table 6

Answer Source

Wrap the result in another table.

echo "<table>";
$count = 0;
$num_columns = 2;  // or 3
while ($rc = mysql_fetch_array($results_course)) {
    if ($count++ % $num_columns == 0) {
        echo "<tr>";
    }
    echo "<td>";
    // previous table code here
    echo "</td>";
    if ($count % $num_columns == 0) {
      echo "</tr>";
    }
}
if ($count % $num_columns > 0) {
  echo "</tr>";
}
echo "</table>";