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 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿