Source code PHP

PHP Knowledge เป็น บอร์ดรวามความรู้ php เน้นบทความ แนวทางการเขียนโปรแกรม บันทึกกันลืม เพื่อให้สมาชิกได้เขียนความรู้ที่ตัวเองมีให้สมาชิกท่านอื่นๆ ได้ เข้ามาอ่าน และ ไว้อ่านเองกันลืมด้วย

Moderator: mindphp, ผู้ดูแลกระดาน

buay
PHP Super Member
PHP Super Member
โพสต์: 250
ลงทะเบียนเมื่อ: 02/06/2014 9:55 am

Source code PHP

โพสต์ที่ยังไม่ได้อ่าน โดย buay »

สร้างตารางเรียน ตารางสอน
01.png
01.png (54.72 KiB) Viewed 1898 times

โค้ด: เลือกทั้งหมด

<style>
.no{ width : 100px; text-align : center;}
.highlight{ background-color : #cccccc; }
.title{ text-align : center;}
</style>
<?php

$timeArr = array( 
            0 => array( "start" => "08:30", "stop" => "09:20"), 
            1 => array( "start" => "09:20", "stop" => "10:10"), 
            2 => array( "start" => "10:15", "stop" => "11:05"), 
            3 => array( "start" => "11:05", "stop" => "11:55"), 
            4 => array( "start" => "11:55", "stop" => "12:45"), 
            5 => array( "start" => "12:45", "stop" => "13:35"), 
            6 => array( "start" => "13:35", "stop" => "14:30"), 
            7 => array( "start" => "14:30", "stop" => "15:20"), 
            8 => array( "start" => "15:20", "stop" => "16:10"), 
            9 => array( "start" => "16:10", "stop" => "17:00"), 
            10 => array( "start" => "17:00", "stop" => "17:50")
        );

//DATABASE to Array
//วนลูปฐานข้อมูล มาเก็บในรูปแบบ Array
$timeTeach = array(
    0 => array( 
            array('time' => '08:30-11:55', 'title' => '4312405 เทคโนโลยีสารสนเทศ และการสื่อสาร'), 
            array('time' => '13:35-15:20', 'title' => '4312605 ระบบฐานข้อมูล')
            ),
    1 => array(
            array('time' => '12:45-16:10', 'title' => '4312502 หัวข้อพิเศษเกี่ยวกับวิทยาการคอมพิวเตอร์')
            ),
    2 => array(),
    3 => array(),
    4 => array(),
    5 => array(),
    6 => array(),
    7 => array()
);
//End การจัดรูปแบบข้อมูล

/* Head Column */
function createCol($arr){
    $row = "";
    foreach( $arr as $data )
    {
        $row .= '<td>' . $data['start'] . '-' . $data['stop'] . '</td>';
    }
    return $row;
}

/* 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;
}

$list = "";
echo '<table border="1" width="90%" align="center" cellspacing="0">';
echo '<tr><td>&nbsp;</td><td>&nbsp;</td>'. createCol( $timeArr ) .'</tr>';
foreach($timeTeach as $i=>$arr){

    //ค้นหาข้อมูลในตารางลงทะเบียน
    //นับช่วงเวลา start_time กับ stop_time ว่ามีกี่ช่อง
    $timeT = $timeTeach[$i];
    
    $arrRange = getTimeRange($timeT, $timeArr);
    
    //echo '<pre>', print_r($arrRange,true) ,'</pre>';
    
    $no = $i + 1;

    $list = '<tr>';
    $list.= '<td rowspan="2" class="no">'.$no.'</td>';
    $list.= '<td>ลายเซ็น</td>';
    $chkCol = 0;
    $col = 0;
    foreach( $timeArr as $timeA )
    {    
        $highlight = "";
        $colspan = "";
        if($chkCol < ($col-1) && $col != 0){
            $chkCol++;
            continue;
        }
        $col = 0;
        $chkCol = 0;
        if(!empty($arrRange[trim($timeA['start'])])){
            $col = $arrRange[trim($timeA['start'])]['colspan'];
            $highlight = "highlight";
            $colspan = 'colspan="'.$col.'"';
        }
        $list.= '<td '.$colspan.' class="'. $highlight .'">&nbsp;</td>';
    }
    $list.= '</tr>';
    
    $list.= '<tr>';
    $list.= '<td>เอก/รุ่น/ห้อง</td>';
    foreach( $timeArr as $timeA )
    {    
        $highlight = "";
        $colspan = "";
        if($chkCol < ($col-1) && $col != 0){
            $chkCol++;
            continue;
        }
        $title = "&nbsp;";
        $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">' . $title . '</td>';
    }
    $list .= '</tr>';
    echo $list;
    
}
echo '</table>';

?>
buay
PHP Super Member
PHP Super Member
โพสต์: 250
ลงทะเบียนเมื่อ: 02/06/2014 9:55 am

Re: Source code PHP

โพสต์ที่ยังไม่ได้อ่าน โดย buay »

แสดงเวลา
02.png
02.png (7.64 KiB) Viewed 1897 times

โค้ด: เลือกทั้งหมด

<?php

header("Pragma: no-cache");

/* Grab the current server time. */
$gDate = time();
/* Are the seconds shown by default? When changing this, also change the
   JavaScript client code's definition of clockShowsSeconds below to match. */
$gClockShowsSeconds = false;

function getServerDateItems($inDate) {
    return date('Y,n,j,G,',$inDate).intval(date('i',$inDate)).','.intval(date('s',$inDate));
    // year (4-digit),month,day,hours (0-23),minutes,seconds
    // use intval to strip leading zero from minutes and seconds
    //   so JavaScript won't try to interpret them in octal
    //   (use intval instead of ltrim, which translates '00' to '')
}

function clockDateString($inDate) {
    return date('l, F j, Y',$inDate);    // eg "Monday, January 1, 2002"
}

function clockTimeString($inDate, $showSeconds) {
    return date($showSeconds ? 'g:i:s' : 'g:i',$inDate).' ';
}
/*** Clock -- end of server-side support code ***/
?>
<html>
<head>
<title>Clock</title>
<meta name="template" content="none">
<script language="JavaScript" type="text/javascript">
<!--
/* set up variables used to init clock in BODY's onLoad handler;
   should be done as early as possible */
var clockLocalStartTime = new Date();
var clockServerStartTime = new Date(<?php echo(getServerDateItems($gDate))?>);

/* stub functions for older browsers;
   will be overridden by next JavaScript1.2 block */
function clockInit() {
}
//-->
</script>
<script language="JavaScript1.2" type="text/javascript">
<!--

function simpleFindObj(name, inLayer) {
    return document[name] || (document.all && document.all[name])
        || (document.getElementById && document.getElementById(name))
        || (document.layers && inLayer && document.layers[inLayer].document[name]);
}


var clockIncrementMillis = 60000;
var localTime;
var clockOffset;
var clockExpirationLocal;
var clockShowsSeconds = false;
var clockTimerID = null;

function clockInit(localDateObject, serverDateObject)
{
    var origRemoteClock = parseInt(clockGetCookieData("remoteClock"));
    var origLocalClock = parseInt(clockGetCookieData("localClock"));
    var newRemoteClock = serverDateObject.getTime();
    // May be stale (WinIE); will check against cookie later
    // Can't use the millisec. ctor here because of client inconsistencies.
    var newLocalClock = localDateObject.getTime();
    var maxClockAge = 60 * 60 * 1000;   // get new time from server every 1hr

    if (newRemoteClock != origRemoteClock) {
        // new clocks are up-to-date (newer than any cookies)
        document.cookie = "remoteClock=" + newRemoteClock;
        document.cookie = "localClock=" + newLocalClock;
        clockOffset = newRemoteClock - newLocalClock;
        clockExpirationLocal = newLocalClock + maxClockAge;
        localTime = newLocalClock;  // to keep clockUpdate() happy
    }
    else if (origLocalClock != origLocalClock) {
        // error; localClock cookie is invalid (parsed as NaN)
        clockOffset = null;
        clockExpirationLocal = null;
    }
    else {
        // fall back to clocks in cookies
        clockOffset = origRemoteClock - origLocalClock;
        clockExpirationLocal = origLocalClock + maxClockAge;
        localTime = origLocalClock;
        // so clockUpdate() will reload if newLocalClock
        // is earlier (clock was reset)
    }
    /* Reload page at server midnight to display the new date,
       by expiring the clock then */
    var nextDayLocal = (new Date(serverDateObject.getFullYear(),
            serverDateObject.getMonth(),
            serverDateObject.getDate() + 1)).getTime() - clockOffset;
    if (nextDayLocal < clockExpirationLocal) {
        clockExpirationLocal = nextDayLocal;
    }
}

function clockOnLoad()
{
    clockUpdate();
}

function clockOnUnload() {
    clockClearTimeout();
}

function clockClearTimeout() {
    if (clockTimerID) {
        clearTimeout(clockTimerID);
        clockTimerID = null;
    }
}

function clockToggleSeconds()
{
    clockClearTimeout();
    if (clockShowsSeconds) {
        clockShowsSeconds = false;
        clockIncrementMillis = 60000;
    }
    else {
        clockShowsSeconds = true;
        clockIncrementMillis = 1000;
    }
    clockUpdate();
}

function clockTimeString(inHours, inMinutes, inSeconds) {
    return inHours == null ? "-:--" : ((inHours == 0
                   ? "12" : (inHours <= 12 ? inHours : inHours - 12))
                + (inMinutes < 10 ? ":0" : ":") + inMinutes
                + (clockShowsSeconds
                   ? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "")
                + (inHours < 12 ? " AM" : " PM"));
}

function clockDisplayTime(inHours, inMinutes, inSeconds) {
    
    clockWriteToDiv("ClockTime", clockTimeString(inHours, inMinutes, inSeconds));
}

function clockWriteToDiv(divName, newValue) // APS 6/29/00
{
    var divObject = simpleFindObj(divName);
    newValue = '<p>' + newValue + '<' + '/p>';
    if (divObject && divObject.innerHTML) {
        divObject.innerHTML = newValue;
    }
    else if (divObject && divObject.document) {
        divObject.document.writeln(newValue);
        divObject.document.close();
    }
    // else divObject wasn't found; it's only a clock, so don't bother complaining
}

function clockGetCookieData(label) {
    /* find the value of the specified cookie in the document's
    semicolon-delimited collection. For IE Win98 compatibility, search
    from the end of the string (to find most specific host/path) and
    don't require "=" between cookie name & empty cookie values. Returns
    null if cookie not found. One remaining problem: Under IE 5 [Win98],
    setting a cookie with no equals sign creates a cookie with no name,
    just data, which is indistinguishable from a cookie with that name
    but no data but can't be overwritten by any cookie with an equals
    sign. */
    var c = document.cookie;
    if (c) {
        var labelLen = label.length, cEnd = c.length;
        while (cEnd > 0) {
            var cStart = c.lastIndexOf(';',cEnd-1) + 1;
            /* bug fix to Danny Goodman's code: calculate cEnd, to
            prevent walking the string char-by-char & finding cookie
            labels that contained the desired label as suffixes */
            // skip leading spaces
            while (cStart < cEnd && c.charAt(cStart)==" ") cStart++;
            if (cStart + labelLen <= cEnd && c.substr(cStart,labelLen) == label) {
                if (cStart + labelLen == cEnd) {                
                    return ""; // empty cookie value, no "="
                }
                else if (c.charAt(cStart+labelLen) == "=") {
                    // has "=" after label
                    return unescape(c.substring(cStart + labelLen + 1,cEnd));
                }
            }
            cEnd = cStart - 1;  // skip semicolon
        }
    }
    return null;
}

/* Called regularly to update the clock display as well as onLoad (user
   may have clicked the Back button to arrive here, so the clock would need
   an immediate update) */
function clockUpdate()
{
    var lastLocalTime = localTime;
    localTime = (new Date()).getTime();
    
    /* Sanity-check the diff. in local time between successive calls;
       reload if user has reset system clock */
    if (clockOffset == null) {
        clockDisplayTime(null, null, null);
    }
    else if (localTime < lastLocalTime || clockExpirationLocal < localTime) {
        /* Clock expired, or time appeared to go backward (user reset
           the clock). Reset cookies to prevent infinite reload loop if
           server doesn't give a new time. */
        document.cookie = 'remoteClock=-';
        document.cookie = 'localClock=-';
        location.reload();      // will refresh time values in cookies
    }
    else {
        // Compute what time would be on server 
        var serverTime = new Date(localTime + clockOffset);
        clockDisplayTime(serverTime.getHours(), serverTime.getMinutes(),
            serverTime.getSeconds());
        
        // Reschedule this func to run on next even clockIncrementMillis boundary
        clockTimerID = setTimeout("clockUpdate()",
            clockIncrementMillis - (serverTime.getTime() % clockIncrementMillis));
    }
}

/*** End of Clock ***/
//-->
</script>
</head>

<body bgcolor="#FFFFFF"
    onload="clockInit(clockLocalStartTime, clockServerStartTime);clockOnLoad();"
    onunload="clockOnUnload()">
<div id="ClockTime" style="position: absolute; left: 406px; top: 28px;
    width: 200px; height: 20px; z-index: 11; cursor: pointer"
    onclick="clockToggleSeconds()">
  <p><?php echo(clockTimeString($gDate,$gClockShowsSeconds));?></p>
</div>
<div id="ClockBkgnd" style="position: absolute; left: 406px; top: 28px;
    width: 200px; z-index: 10">
  <p> <br>
  <?php echo(clockDateString($gDate));?></p>
</div>

</body>
</html>
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 75