如何水平显示/选择MYSQL查询

时间:2017-06-18 12:08:09

标签: php mysql

我目前正在从我的数据库中提取一些时间表信息。 我遇到一个问题,我需要行中的cols和cols中的行。

查询如下:

SELECT `courses`.`course_id`,`faculty`.`uName`,`rooms`.`room_name`,`slot_allocs`.`slot_num`
FROM `faculty`
    LEFT JOIN `courses` ON `courses`.`fac_id` = `faculty`.`uName`
    LEFT JOIN `depts` ON `faculty`.`dept_code` = `depts`.`dept_code`
    LEFT JOIN `slot_allocs` ON `slot_allocs`.`course_id` = `courses`.`course_id`
    LEFT JOIN `rooms` ON `slot_allocs`.`room` = `rooms`.`room_name`
    LEFT JOIN `slots` ON `slot_allocs`.`table_name` = `slots`.`table_name` AND         `slot_allocs`.`day` = `slots`.`day` AND `slot_allocs`.`slot_num` =     `slots`.`slot_num`
WHERE `slot_allocs`.`day` = 1  AND `depts`.`dept_code` = 100

ORDER BY `slots`.`slot_num` ASC

在我执行我的PHP文件后,在这个查询执行它的顺序检索和显示中,我希望得到输出 - >一个接一个地

> > **I want to align in the sequential manner of the period  per day like this:
> >|---------------------------------------------
> >day 1| period 1 |period 2 |period 3 |period 4| 
> >|----|----------|---------|---------|--------|
> >day 2| period 1 |period 2 |period 3 |period 4|
> >|---------------------------------------------

这是我的PHP代码:

$sql = file_get_contents("eboard.sql");
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "<table>";
        echo "<tr>";
        echo "<td>";
        echo " Course: ID ". $row["course_id"]." : ";     
        echo " Prof.:". $row["uName"]." : ";
        echo " Room :".$row["room_name"]." : ";
        echo " period".$row["slot_num"]."";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
    }
} else
    echo "0 results";
$conn->close();

我得到的输出:--- enter image description here

请帮忙!!

2 个答案:

答案 0 :(得分:0)

好的,所以这段代码应该这样做。您应该了解表的工作原理。 W3Schools有一个关于它的简单页面。我希望这个版本是你想要的。它不是一个完整的解决方案,但它向您展示了如何使用表格。

if ($result->num_rows > 0) {
    echo "<table>";
    echo "  <tr>";
    echo "    <th>Course</th>";
    echo "    <th>Prof.</th>";
    echo "    <th>Room</th>";
    echo "    <th>Period</th>";
    echo "  </tr>";
    while($row = $result->fetch_assoc()) {
        echo "  <tr>";
        echo "    <td>Course: ID ". $row["course_id"]."</td>";     
        echo "    <td>Prof.:". $row["uName"]."</td>";
        echo "    <td>Room :".$row["room_name"]."</td>";
        echo "    <td>period".$row["slot_num"]."</td>";
        echo "  </tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

答案 1 :(得分:0)

<table>移出循环:

if ($result->num_rows > 0) {
    echo "<table>";
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>";
        echo " Course: ID ". $row["course_id"]." : ";     
        echo " Prof.:". $row["uName"]." : ";
        echo " Room :".$row["room_name"]." : ";
        echo " period".$row["slot_num"]."";
        echo "</td>";
        echo "</tr>";

    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();