php HTML时间表

时间:2015-10-18 18:10:54

标签: php html

我关于SO的第一篇文章。我正在研究基于php的时间表模块。似乎90%的代码都运行良好。只是无法正确显示循环以便以时间表格式显示我的数据。

我希望我的时间表就像这张表:

Required Timetable

我的代码:

//Array for days starting from 1 for monday
$days = array('1','2','3','4','5','6','7');

//Selecting all the hours from lectures
$hours = select id, start_time from lectures;

$timetable = select timetable.id, timetable.day, timetable.lecture_id, timetable.subject_id from timetable;

        echo "<table>";
        echo "<tr>";
            //echo "<td>";
            //  echo "Day";
            //echo "</td>";
    foreach($hours as $hh)
                        {
                        echo "<td>";
                        {echo $hh->start_time;}
                        echo "</td>";   
                        }
    echo "</tr>";                   
    foreach($hours as $hour)
        {
            foreach($days as $day)
                {
                    echo "<tr>";
                        foreach($timetable as $tt)
                        {
                            echo "<td>";
                            if ($tt->day==$day AND $tt->lecture_id==$hour->id)
                            {echo $tt->subject_id;}
                            echo "</td>";
                        }
                    echo "</tr>";   
                }
        }
    echo "</table>";

我的输出如下。这没关系,除非我无法将Days放在行的开头。任何人都可以帮我解决这个小问题。我想我搞乱了HTML

Current Timetable

3 个答案:

答案 0 :(得分:0)

您可能需要在第一行显示开始时间的空单元格,然后对于包含主题的每一行,添加一个显示星期几的单元格。

另外,如果您在SQL中使用ORDER BY子句,我觉得您可能会将三重for循环减少到至少两个for循环。也许按天排序,然后按开始时间排序(对于开始时间,您需要与讲座表进行连接)。似乎外循环应该遍历几天,内循环 - 通过开始时间。

编辑:你甚至可以只有一个循环,如果你要交叉加入不同的日期和开始时间,按天计算,然后按开始时间排序,然后按照你的实际时间表进行左外连接。您将最终在每一行中都有NULL,这些NULL对应于没有预定讲座的日期和时间。您还会知道您的查询每天都会提供相同的行数。有了它,你可以每N行创建一个新的<tr />,其中N是一天中的开始时间。

EDIT2:关于循环嵌套,我的意思是这样的: `

foreach($days as $day)
    {
        echo "<tr>"; // Every day is a table row
        echo "<td>" . getDayNameByNumber($day) . "</td>" // use one of the methods being suggested here, i.e. indexing into an array of names, a built-in function if PHP has it (would be better, because it would likely follow the system's locale and translate it for you...)
        foreach($hours as $hour)
            {
                    // Every hour in a day is a cell in the row
                    // optimize this loop...
                    foreach($timetable as $tt)
                    {
                        echo "<td>";
                        if ($tt->day==$day AND $tt->lecture_id==$hour->id)
                        {echo $tt->subject_id;}
                        echo "</td>";
                    }
                echo "</tr>";   
            }
    }

答案 1 :(得分:0)

首先,确保您正在调用正确的MySQL表字段。

如果是,那么,将$ days数组更改为带有工作日名称的双向数组,在循环中添加day cell,在顶部添加一个空单元格。 (见代码评论)

<?php

// days of week array
$days = array( 
1 => 'Monday', 
2 => 'Tuesday', 
3 => 'Wednesday', 
4 => 'Thursday', 
5 => 'Friday', 
6 => 'Saturday', 
7 => 'Sunday' );

//Selecting all the hours from lectures
// $hours = select id, start_time from lectures;

// $timetable = select timetable.id, timetable.day, timetable.lecture_id, timetable.subject_id from timetable;



echo "<table>";
echo "<tr>";

echo '<td></td>'; // empty cell

foreach( $hours as $hh ) {

    echo "<td>";    
    echo $hh->start_time;
    echo "</td>";

}

echo "</tr>";

foreach( $hours as $hour ) {

    foreach( $days as $day => $day_name ) {

        echo "<tr>";
        echo '<td>', $day_name, '</td>'; // day of the week

        foreach( $timetable as $tt ) {

            echo "<td>";

            if( (int)$tt->day == $day and $tt->lecture_id == $hour->id ) {
                echo $tt->subject_id;
            }

            echo "</td>";
        }

        echo "</tr>";
    }
}
echo "</table>";

?>

如果您不想创建双向数组,请使用PHP的函数jddayofweek()。这是一个例子:

<?php


//Array for days starting from 1 for monday
$days = array( 1, 2, 3, 4, 5, 6, 7 );

foreach ( $days as $day ) {

    echo jddayofweek( $day - 1, 1 ), '<br>';

}

?>

但是,出于性能原因,我建议您使用双向数组。

答案 2 :(得分:0)

经过一些谷歌搜索

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'spec_helper'
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

Rails.logger.level = 4

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  config.include FactoryGirl::Syntax::Methods
  config.include Sorcery::TestHelpers::Rails
  config.include Macros::Controller::Security
end
FactoryGirl.reload
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end