dongyuyi5680 2015-05-12 16:21
浏览 44
已采纳

PHP查询学校日程安排

Good day everybody, I'm here to ask for advices concerning schedule I've been working on. The schedule should register each subject in each hour of each day from monday to friday. The problem I encouter is in the number of queries, because it needs to be different query for each hour of each day and that would give me 45 different queries(the difference will be in the variable for $day and $hour).
Of course I could make 45 different queries but that seems like insanity to me so I wonder, there has to be a way how to make the process simplier. I'm pretty sure, that the sollution is hidden in the for or while loops, but than again, I would need to change the variables in the process.
Now my table and most of my php looks like this (yes I'm missing the the part, where I extract the results to each row, that will be added later):

.table{
    border: 1px solid red;
    margin-left: auto;
    margin-right:auto;
}
.table tr{
    border: 1px solid blue;
}
.table td{
    border: 1px solid green;
}
<?php

//authentication check
if (isset($_SESSION['authenticated'])) {
    echo " User id: " . $ID;
    require_once ('./functions/database.php');

    $mon = 'monday';
    $tue = 'tuesday';
    $wen = 'wensday';
    $thu = 'thursday';
    $fri = 'friday';

    $hourOne = '1.hour';
    $hourTwo = '2.hour';
    $hourThree = '3.hour';
    $hourFour = '4.hour';
    $hourFive = '5.hour';
    $hourSix = '6.hour';
    $hourSeven = '7.hour';
    $hourEight = '8.houra';
    $hournine = '9.hour';

    $query1 = "select subject from student_subjects natural join subjects
               where id_student = '$ID' AND day = '$mon' AND hour ='$hourOne';";
    $result = mysqli_query($conn, $query);



    echo "<section>";
    echo "<table class='table'>
        <tr>
            <td>Den</td>
            <td>1.hour</td>
            <td>2.hour</td>
            <td>3.hour</td>
            <td>4.hour</td>
            <td>5.hour</td>
            <td>6.hour</td>
            <td>7.hour</td>
            <td>8.hour</td>
            <td>9.hour</td>
        </tr>
        <tr>
            <td>monday</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
        </tr>
        <tr>
            <td>tuesday</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
        </tr>
        <tr>
            <td>wensday</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
        </tr>
        <tr>
            <td>thursday</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
        </tr>
        <tr>
            <td>friday</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
            <td>/*hodina*/</td>
        </tr>
    </table>";
    echo "</section>";
} else {
    echo " To see your schedule, you need to login first";
}
?>

(hodina = hour)
Thank you in advance for any advice.

</div>
  • 写回答

1条回答 默认 最新

  • douya6229 2015-05-12 21:04
    关注

    Well here is a quick and dirty way to do it.

    $days = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday');
    $hours = array('1.hour', '2.hour', '3.hour', '4.hour', '5.hour', '6.hour', '7.hour', '8.hour', '9.hour');
    
    echo "<section>";
    echo "<table class='table'>";
    
    foreach ($days as $day) {
        echo "<tr>";
        foreach ($hours as $hour) {
            echo "<td>";
            $query = "SELECT subject FROM student_subjects NATURAL JOIN subjects WHERE id_student='$ID' AND day='$day' AND hour = '$hour'";
            $result = mysql_query($conn, $query);
            $row = mysql_fetch_assoc($result);
            if ($row['subject']) {
                echo $row['subject'];
            }
            echo "</td>";
        }
        echo "</tr>";
    }
    
    echo "</table>";
    echo "</section>";
    

    You can store the days and hours in a pair of arrays and loop through both of them for each hour of each day. However this is very inefficient because of the number of queries that you need to make to the database. It is much better if you could use a single query to get all the schedules and then find a way to convert the data into columns instead of rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)