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

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.

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

报告相同问题?

悬赏问题

  • ¥20 阿里云python代码求解
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路