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

旧巷老猫 提交于 2019-12-13 23:56:48

问题


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


回答1:


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>";



回答2:


Add the to the head of the page:

<script type="text/css">
  table{
    width: 30%;
    margin: 2%; 
    float: left;
  }
</script>

You may have to play around with the margins, or you may not need them at all, but that should put 3 tables next to eachother



来源:https://stackoverflow.com/questions/19865169/set-tables-as-side-by-side-instead-of-straight-down-while-doing-a-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!