|
|
|
ช่วยดู code การวนลูป รับค่าจากฐานข้อมูลมาเก็บไว้ที่ตัวแปร array ใช้ในตารางสอนครับ |
|
|
|
|
|
|
|
หน้า index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.tree
{
font-family: Arial, Helvetica, sans-serif; font-size: 9;
border: 1px solid black;
}
.tree th
{
border: 1px solid #FFFFFF;
}
.tree td
{
border: 1px solid #FFFFFF;
}
.no{ width : 100px; text-align : center;}
.highlight{ background-color : #ffccaa; }
.style2 {
color: #FFFFFF;
font-size: 12px;
}
.style3 {color: #FFFF33}
.style5 {color: #FF99CC}
.style7 {color: #00FF33}
.style9 {color: #FF9933}
.style10 {color: #00FFCC}
.style11 {color: #FFFFCC}
.style13 {color: #999933}
-->
</style>
<style>
.no{ width : 100px; text-align : center;}
.highlight{ background-color : #99aaff; }
.title{ text-align : center;}
</style>
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tree">
<tr>
<td width="7%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="35" align="center" bgcolor="#333333"><span class="style11">วัน/เวลา</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style3">จันทร์</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style5">อังคาร</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style7">พุธ</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style9">พฤหัสบดี</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style10">ศุกร์</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style11">เสาร์</span></td>
</tr>
<tr>
<td height="42" align="center" bgcolor="#666666"><span class="style13">อาทิตย์</span></td>
</tr>
</table></td>
<td width="93%" align="left" valign="top">
<?php
$timeArr = array(
0 => array( "start" => "07.00", "stop" => "07.30"),
1 => array( "start" => "07.30", "stop" => "08.00"),
2 => array( "start" => "08.00", "stop" => "08.30"),
3 => array( "start" => "08.30", "stop" => "09.00"),
4 => array( "start" => "09.00", "stop" => "09.30"),
5 => array( "start" => "09.30", "stop" => "10.00"),
6 => array( "start" => "10.00", "stop" => "10.30"),
7 => array( "start" => "10.30", "stop" => "11.00"),
8 => array( "start" => "11.00", "stop" => "11.30"),
9 => array( "start" => "11.30", "stop" => "12.00"),
10 => array( "start" => "12.00", "stop" => "12.30"),
11 => array( "start" => "12.30", "stop" => "13.00"),
12 => array( "start" => "13.00", "stop" => "13.30"),
13 => array( "start" => "13.30", "stop" => "14.00"),
14 => array( "start" => "14.00", "stop" => "14.30"),
15 => array( "start" => "14.30", "stop" => "15.00"),
16 => array( "start" => "15.00", "stop" => "15.30"),
17 => array( "start" => "15.30", "stop" => "16.00"),
18 => array( "start" => "16.00", "stop" => "16.30"),
19 => array( "start" => "16.30", "stop" => "17.00"),
20 => array( "start" => "17.00", "stop" => "17.30"),
21 => array( "start" => "17.30", "stop" => "18.00"),
22 => array( "start" => "18.00", "stop" => "18.30"),
23 => array( "start" => "18.30", "stop" => "19.00"),
24 => array( "start" => "19.00", "stop" => "19.30"),
25 => array( "start" => "19.30", "stop" => "20.00")
);
?>
<?php
/* Key Positon */
function getCol($haystack, $keyNeedle)
{
$i = 0;
foreach($haystack as $arr)
{
if($arr['start'] == $keyNeedle)
{
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 = getCol($timeCol, $tm[1] );
$colspan = $end - $start;
$data[$tm[0]] = array('colspan' => $colspan, 'title' => $timeA['title']);
}
return $data;
}
?>
<?php
$timeTeach = array(
0 => array(
array('time' => '10.00-13.30', 'title' => '4312405 ICT'),
array('time' => '15.00-18.00', 'title' => '4312605 DB'),
),
1 => array(
array('time' => '09.30-12.00', 'title' => '4312502 AC'),
),
2 => array(
array('time' => '12.30-14.00', 'title' => '4312502 AC'),
),
3 => array(
array('time' => '07.00-12.00', 'title' => '4312502 AC'),
),
4 => array(
array('time' => '08.30-11.30', 'title' => '4312502 AC'),
),
5 => array(),
6 => array(),
);
?>
<?php
$list = "";
echo '<table border="0" width="100%" align="center" cellspacing="0">';
echo '<tr>';
echo '<td width="7.68%" height="34" colspan="2" align="center" bgcolor="#333333"><span class="style2">07.00 - 08.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">08.00 - 09.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">09.00 - 10.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">10.00 - 11.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">11.00 - 12.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">12.00 - 13.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">13.00 - 14.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">14.00 - 15.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">15.00 - 16.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">16.00 - 17.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">17.00 - 18.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">18.00 - 19.00</span></td>';
echo '<td width="7.68%" colspan="2" align="center" bgcolor="#333333"><span class="style2">19.00 - 20.00</span></td>';
echo '</tr>';
?>
<?php
foreach($timeTeach as $i=>$arr){
$timeT = $timeTeach[$i];
$arrRange = getTimeRange($timeT, $timeArr);
//echo '<pre>', print_r($arrRange,true) ,'</pre>';
$list = '<tr>';
$chkCol = 0;
$col = 0;
foreach( $timeArr as $timeA )
{
$highlight = "";
$colspan = "";
if($chkCol < ($col-1) && $col != 0){
$chkCol++;
continue;
}
$title = " ";
$col = 0;
$chkCol = 0;
if(!empty($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" height="39">' . $title . '</td>';
}
$list .= '</tr>';
echo $list;
}
echo '</table>';
?>
</td>
</tr>
</table>
</body>
</html>
การแสดงผลจะแสดงดังภาพนี้ครับ
อยากทราบว่าเราจะวนลูปรับค่ามาเก็บไว้ที่ $timeTeach ที่เป็น อาเรย์ยังไงครับ
อันนี้ เป็นฐานข้อมูลที่สร้างไว้
ลองศึกษา จากกระทู้ https://www.thaicreate.com/php/forum/094496.html#00337086
ไม่รู้จะทำยังไง ลองทำเองแต่มันก็เกิด error ตามนี้
การวนลูปข้อมูลจากฐานข้อมูล มาเก็บไว้ที่ $timeTeach
<?php
include "connect.php";
$sql="select * from time_booking";
$query=mysql_query($sql);
$timeTeach = array(
while($result=mysql_fetch_array($query))
{
$tid=$result["id_time"];
$tid => array(
array('time' => $result["start_time"].'-'.$result["end_time"], 'title' => $result["subject_id"]."".$result["subname"]),
),
}
);
?>
รบกวนท่านๆ ช่วยแนะนำการเขียนวนลูปรับค่ามาเก็บไว้ที่ตัวแปรอาเรย์ด้วยครับ
Tag : PHP
|
|
|
|
|
|
Date :
2013-06-27 08:11:42 |
By :
kantarasukan |
View :
1407 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$timeTeach[] = $result; ประมาณนี้ป่ะ
|
|
|
|
|
Date :
2013-06-27 09:26:38 |
By :
moomoohorn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|