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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵