|
|
|
ช่วยดู code การทำตารางเรียนให้สามารถซ้อนกันได้หน่อยครับ |
|
|
|
|
|
|
|
ต้องการให้วิชาที่เรียนเวลาเดียวกันหรือคาบเกี่ยวสามารถทับซ้อนกันได้ค่ะ ตอนนี้สามารถทำได้แต่ลักษณะที่มีไม่มีการทับซ้อนกัน เมื่อเวลาทับซ้อนกันมันจะแสดงแต่วิชาที่ select ออกมาหลังสุด เท่านั้น
เป็น code ที่เขียนขึ้นโดย select ข้อมูลจาก database เพื่อมาแสดงเป็นวันต่างๆ แต่ตอนนี้มีปัญหาคือ เมื่อวันนั้นเวลา 10.30 มีการเรียน 2 วิชา เกิดขึ้น อยากให้แสดงได้ว่ามีการทับซ้อนกันโดยแสดงออกมาในรูปแบบนี้ครับ
แต่ตอนนี้สามารถทำได้ในรูปแบบนี้ค่ะ เลยอยากให้ช่วยดูให้หน่อยค่ะว่าจะทำให้เป็นแบบที่ต้องการจะต้องแก้ไขยังไงครับ
เนื่องจากวันพฤหัสบดีมีเรียน 2 วิชาซึ่งซ้อนทับกันอยู่คือเวลา 10.30 -12.00 ครับ
Code
Code (PHP)
$timeArr = array(
0 => array( "start" => "08.00", "stop" => "08.30"),
1 => array( "start" => "08.30", "stop" => "09.00"),
2 => array( "start" => "09.00", "stop" => "09.30"),
3 => array( "start" => "09.30", "stop" => "10.00"),
4 => array( "start" => "10.00", "stop" => "10.30"),
5 => array( "start" => "10.30", "stop" => "11.00"),
6 => array( "start" => "11.00", "stop" => "11.30"),
7 => array( "start" => "11.30", "stop" => "12.00"),
8 => array( "start" => "12.00", "stop" => "12.30"),
9 => array( "start" => "12.30", "stop" => "13.00"),
10 => array( "start" => "13.00", "stop" => "13.30"),
11 => array( "start" => "13.30", "stop" => "14.00"),
12 => array( "start" => "14.00", "stop" => "14.30"),
13 => array( "start" => "14.30", "stop" => "15.00"),
14 => array( "start" => "15.00", "stop" => "15.30"),
15 => array( "start" => "15.30", "stop" => "16.00"),
16 => array( "start" => "16.00", "stop" => "16.30"),
17 => array( "start" => "16.30", "stop" => "17.00"),
18 => array( "start" => "17.00", "stop" => "17.30"),
19 => array( "start" => "17.30", "stop" => "18.00"),
20 => array( "start" => "18.00", "stop" => "18.30"),
21 => array( "start" => "18.30", "stop" => "19.00"),
22 => array( "start" => "19.00", "stop" => "19.30"),
23 => array( "start" => "19.30", "stop" => "20.00")
);
// loop create table major
?>
<div class="widget-content" id="divprint">
<?php
for($m=1;$m<=$_GET["num_major"];$m++){
if(isset($_GET["major".$m.""])){
unset($timeTeach);
//DATABASE to Array
///จันทร์
connect_db();
$select = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='จันทร์' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query = mysql_query($select) or die('query did not work');
$num=mysql_num_rows($query);
if($num == null){
$timeTeach[1][] = array('time' =>'');
}
else{
while($result=mysql_fetch_array($query)){
$tid=en2_day($result["Day"]);
$timeTeach[$tid][] = array('time' => $result["Starttime"].'-'.$result["Endtime"], 'title' => "<b>".$result["Subject_id"]."</b>"."<br>"."(".$result["Section_no"]."),".room($result["Section_type"],$result["Room_id"]));
}
}
// อังคาร
connect_db();
$select1 = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='อังคาร' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query1 = mysql_query($select1) or die('query did not work');
$num1=mysql_num_rows($query1);
if($num1 == null){
$timeTeach[2][] = array('time' =>'');
}
else{
while($result1=mysql_fetch_array($query1)){
$tid=en2_day($result1["Day"]);
$timeTeach[$tid][] = array('time' => $result1["Starttime"].'-'.$result1["Endtime"], 'title' => "<b>".$result1["Subject_id"]."</b>"."<br>"."(".$result1["Section_no"]."),".room($result1["Section_type"],$result1["Room_id"]));
}
}
// พุธ
connect_db();
$select2 = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='พุธ' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query2 = mysql_query($select2) or die('query did not work');
$num2=mysql_num_rows($query2);
if($num2 == null){
$timeTeach[3][] = array('time' =>'');
}
else{
while($result2=mysql_fetch_array($query2)){
$tid=en2_day($result2["Day"]);
$timeTeach[$tid][] = array('time' => $result2["Starttime"].'-'.$result2["Endtime"], 'title' => "<b>".$result2["Subject_id"]."</b>"."<br>"."(".$result2["Section_no"]."),".room($result2["Section_type"],$result2["Room_id"]));
}
}
// พฤหัสบดี
connect_db();
$select3 = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='พฤหัสบดี' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query3 = mysql_query($select3) or die('query did not work');
$num3=mysql_num_rows($query3);
if($num3 == null){
$timeTeach[4][] = array('time' =>'');
}
else{
while($result3=mysql_fetch_array($query3)){
$tid=en2_day($result3["Day"]);
$timeTeach[$tid][] = array('time' => $result3["Starttime"].'-'.$result3["Endtime"], 'title' => "<b>".$result3["Subject_id"]."</b>"."<br>"."(".$result3["Section_no"]."),".room($result3["Section_type"],$result3["Room_id"]));
}
}
// ศุกร์
connect_db();
$select4 = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='ศุกร์' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query4 = mysql_query($select4) or die('query did not work');
$num4=mysql_num_rows($query4);
if($num4 == null){
$timeTeach[5][] = array('time' =>'');
}
else{
while($result4=mysql_fetch_array($query4))
{
$tid=en2_day($result4["Day"]);
$timeTeach[$tid][] = array('time' => $result4["Starttime"].'-'.$result4["Endtime"], 'title' => "<b>".$result4["Subject_id"]."</b>"."<br>"."(".$result4["Section_no"]."),".room($result4["Section_type"],$result4["Room_id"]));
}
}
// เสาร์
connect_db();
$select5 = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='เสาร์' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query5 = mysql_query($select5) or die('query did not work');
$num5=mysql_num_rows($query5);
if($num5 == null){
$timeTeach[6][] = array('time' =>'');
}
else{
while($result5=mysql_fetch_array($query5)){
$tid=en2_day($result5["Day"]);
$timeTeach[$tid][] = array('time' => $result5["Starttime"].'-'.$result5["Endtime"], 'title' => "<b>".$result5["Subject_id"]."</b>"."<br>"."(".$result5["Section_no"]."),".room($result5["Section_type"],$result5["Room_id"]));
}
}
// อาทิตย์
connect_db();
$select6 = "SELECT * FROM ((section INNER JOIN timetable ON section.Section_id = timetable.Section_id) INNER JOIN subject ON section.Subject_id = subject.Subject_id) WHERE timetable.Major_id='gis3' and section.Sem_id='1' and timetable.Day ='อาทิตย์' group by section.Subject_id,section.Section_no,section.Section_type,timetable.Day,timetable.Starttime,timetable.Endtime order by timetable.starttime";
$query6 = mysql_query($select6) or die('query did not work');
$num6=mysql_num_rows($query6);
if($num6 == null){
$timeTeach[7][] = array('time' =>'');
}
else{
while($result6=mysql_fetch_array($query6)){
$tid=en2_day($result6["Day"]);
$timeTeach[$tid][] = array('time' => $result6["Starttime"].'-'.$result6["Endtime"], 'title' => "<b>".$result6["Subject_id"]."</b>"."<br>"."(".$result6["Section_no"]."),".room($result6["Section_type"],$result6["Room_id"]));
}
}
$list = "";
echo '<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed;">';
echo '<tr><th height="35" align="center" bgcolor="#515151"><span class="style3">วัน/เวลา</span></th>';
echo '<td width="7.68%" height="34" colspan="2" align="center" bgcolor="#515151"><span class="style2">08.00 - 09.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">09.00 - 10.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">10.00 - 11.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">11.00 - 12.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">12.00 - 13.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">13.00 - 14.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">14.00 - 15.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">15.00 - 16.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">16.00 - 17.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">17.00 - 18.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">18.00 - 19.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#515151"><span class="style2">19.00 - 20.00</span></td>';
echo '</tr>';
foreach($timeTeach as $i=>$arr){
//ค้นหาข้อมูลในตารางลงทะเบียน
//นับช่วงเวลา start_time กับ stop_time ว่ามีกี่ช่อง
$timeT = $timeTeach[$i];
//echo '<p>'.print_r($timeT).'</p>';
$arrRange = getTimeRange($timeT, $timeArr);
//echo '<pre>', print_r($arrRange,true) ,'</pre>';
$no = $i + 1;
if($no==2){
$day = "จันทร์";
}else if($no==3){
$day = "อังคาร";
}else if($no==4){
$day = "พุธ";
}else if($no==5){
$day = "พฤหัส";
}else if($no==6){
$day = "ศุกร์";
}else if($no==7){
$day = "เสาร์";
}else if($no==8){
$day = "อาทิตย์";
}
$list = '<tr >';
$list.= '<td height="50" align="center" bgcolor="#A0A0A0"><span class="style3">'.$day.'</span></td>';
$chkCol = 0;
$col = 0;
foreach( $timeArr as $timeA )
{
$highlight = "";
$colspan = "";
if($chkCol < ($col-1) && $col != 0){
$chkCol++;
continue;
}
$title = " ";
$col = 0;
$chkCol = 0;
if(isset($arrRange[trim($timeA['start'])])){
$col = $arrRange[trim($timeA['start'])]['colspan'];
$title = $arrRange[trim($timeA['start'])]['title'];
$highlight = "highlight";
$colspan = 'colspan="'.$col.'"';
}
$list .= '<td '.$colspan.' class="'. $highlight .' title" style="font-size:13px">' . $title . '</td>';
}
$list .= '</tr>';
echo $list;
}
echo '</table>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>';
}
}
?>
</div>
<?php }?>
<?php
function room($type,$room_id){
if($room_id == null){return $type;}
else{return $room_id;}
}
function en2_day($day){
if($day == 'จันทร์'){return 1;}
elseif($day == 'อังคาร'){return 2;}
elseif($day == 'พุธ'){return 3;}
elseif($day == 'พฤหัสบดี'){return 4;}
elseif($day == 'ศุกร์'){return 5;}
elseif($day == 'เสาร์'){return 6;}
elseif($day == 'อาทิตย์'){return 7;}
}
/* Key Positon */
function getCol($haystack, $keyNeedle){
$i = 0;
foreach($haystack as $arr){
if($arr['start'] == $keyNeedle){
return $i;
}
$i++;
}
}
function getCol2($haystack, $keyNeedle){
$i = 1;
//echo '<pre>', print_r($haystack,true) ,'</pre>';
foreach($haystack as $arr){
if($arr['stop'] == $keyNeedle){
//echo '<pre>', print_r($arr,true) ,'</pre>';
return $i;
}
$i++;
}
}
/* Time Range */
function getTimeRange($timeT, $timeCol){
$data = array();
foreach($timeT as $timeA){
$time = $timeA['time'];
if(!$time) continue;
$tm = explode("-", $time);
//echo '<pre>', print_r($tm,true) ,'</pre>';
$start = getCol($timeCol, $tm[0]);
$end = getCol2($timeCol, $tm[1] );
//echo '<pre>', $start,' '.$tm[0].' '.$end.' '.$tm[1] ,'</pre>';
$colspan = $end - $start;
$data[$tm[0]] = array('colspan' => $colspan, 'title' => $timeA['title']);
}
return $data;
}
Tag : Mobile, MySQL, HTML/CSS
|
|
|
|
|
|
Date :
2016-05-03 00:21:50 |
By :
pinthip |
View :
2403 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|