2012年11月22日 星期四

PHP行事曆(列印用)範例


這支源碼是先月前應上司要求而寫,主要是列印填寫。為方便iPad列印,所以直接寫為PHP上傳至Server,以利員工隨時打印。雖然最後不了了之OTL,但也將之細部微調,放流源碼供大家參考學習。

編寫上主要以PHP及jQuery為主,員工可依個人要求,自由增刪行數後列印出來填寫。當時為節省時間,使用網上流傳不知名的日曆源碼修改即製。因為效率至上,所以不使用HTML5及DIV。反正不需對外開放,更不想要Google搜索,直接用Table架構即可。如果有興趣的朋友可以「魔改」變成DIV,難度不大。




calendar.php


<?php
//起首檢查Get變數
$params = array();
if (isset($_GET['year']) && isset($_GET['month'])) {
    $params = array(
        'year' => $_GET['year'],
        'month' => $_GET['month'],
    );
}
$params['url']  = 'demo.php';
//調用Calendar Class
//特別聲明此組源碼乃調用自網上流傳的一款PHP日曆,經本人微調,但大體沒有分別
//原作者不可考,但亦要事先聲明
require_once 'calendar.class.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" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
/*
*星期六日字體為紅色
*此範例以「星期一」為每周起始日
*/
$(document).ready(function(){
$("tr[name=calendarD]").find("td:nth-child(6)").css("color","red");
$("tr[name=calendarD]").find("td:nth-child(7)").css("color","red");
/*
*JQuery源碼,新增空白列於最底
*/
$("#Add").click(function(){
var _len = $("#form tr").length;      
$("#form").append("<tr id="+_len+" name='len'>"
+"<td>&nbsp;</td>"
+"<td>&nbsp;</td>"
+"<td>&nbsp;</td>"
+"<td>&nbsp;</td>"
+"<td>&nbsp;</td>"
+"</tr>");
})
/*
*與前面相反,將最底一行刪除
*/
$("#Del").click(function(){
$("#form tr[name='len']:last-child").remove();
})
});
</script>
<title>Calendar</title>
<style type="text/css">
table.calendar {
border: 3px solid #050;
}
.calendar th, .calendar td {
width:30px;
text-align:center;
}
.calendar th {
color:#000;
font-weight:bold;
}
.calendarD td {
border: 1px solid #050;
}
.today{
color:#fff;
background-color:#050;
}
table.form tr td{
height:30px;
}
table.form tr.form_main td{
height:5px;
font-weight: bold;
}
</style>
</head>
<body>
<div style="align:center">
<?php
//調用日曆插件
//相關HTML源碼在calendar.class.php中
$cal = new Calendar($params);
$cal->display();
?>
<table width="950" border="0" cellspacing="1" cellpadding="1" align="center">
  <tr>
    <td>
    <table width="100%" border="0" cellspacing="1" cellpadding="1">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><table id="form" class="form" width="100%" border="1" cellspacing="1" cellpadding="1">
      <tr class="form_main">
        <td width="22%"><div align="center">Name</div></td>
        <td width="17%"><div align="center">Date</div></td>
        <td width="12%"><div align="center">Time</div></td>
        <td width="15%"><div align="center">Amount</div></td>
        <td width="24%"><div align="center">Contact</div></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr><td align="right"><input type="button" value="Add" id="Add"/>&nbsp;<input type="button" value="Del" id="Del"/></td></tr>
</table>
</div>
</body>
</html>


calendar.class.php


<?php
class Calendar
{
    private $year;
    private $month;
//星期一作每周起屹日
//原本源碼起始為「日」,今改修為「一」,需要將下面部份運算源碼改寫
    private $weeks  = array('MON','TUE','WED','THU','FRI','SAT','SUN');
   
    function __construct($options = array()) {
        $this->year = date('Y');
        $this->month = date('m');
       
        $vars = get_class_vars(get_class($this));
        foreach ($options as $key=>$value) {
            if (array_key_exists($key, $vars)) {
                $this->$key = $value;
            }
        }
    }
    function display()
    {
        //設定日曆闊度
echo '<table class="calendar" width="950" align="center">';
        $this->showChangeDate();
        $this->showWeeks();
        $this->showDays($this->year,$this->month);
        echo '</table>';
    }
   
    private function showWeeks()
    {
        echo '<tr>';
        foreach($this->weeks as $title)
        {
            echo '<th>'.$title.'</th>';
        }
        echo '</tr>';
    }
   
    private function showDays($year, $month)
    {
        //求當月總天數,每周起屹星期
$firstDay = mktime(0, 0, 0, $month, 1, $year);
        $firstDay2 = mktime(0, 0, 0, $month, 0, $year);
        $starDay = date('w', $firstDay2);
        $days = date('t', $firstDay);
        echo '<tr name="calendarD" class="calendarD">';
        for ($i=0; $i<$starDay; $i++) {
            echo '<td>&nbsp;</td>';
        }
       
        for ($j=1; $j<=$days; $j++) {
            $i++;
            //if ($j == date('d')) {
                //echo '<td class="today">'.$j.'</td>';
            //} else {
                echo '<td>'.$j.'</td>';
            //}
            if ($i % 7 == 0) {
                echo '</tr><tr name="calendarD" class="calendarD">';
            }
        }
       
        echo '</tr>';
    }
   
    private function showChangeDate()
    {
       
        $url = basename($_SERVER['PHP_SELF']);
//前台調用月曆變更月份功能
if ($this->month == '1') {
$thismo = 'January';
} elseif ($this->month == '2') {
$thismo = 'February';
} elseif ($this->month == '3') {
$thismo = 'March';
} elseif ($this->month == '4') {
$thismo = 'April';
} elseif ($this->month == '5') {
$thismo = 'May';
} elseif ($this->month == '6') {
$thismo = 'June';
} elseif ($this->month == '7') {
$thismo = 'July';
} elseif ($this->month == '8') {
$thismo = 'August';
} elseif ($this->month == '9') {
$thismo = 'September ';
} elseif ($this->month == '10') {
$thismo = 'October';
} elseif ($this->month == '11') {
$thismo = 'November';
} elseif ($this->month == '12') {
$thismo = 'December';
}
    //月曆HTML生成
echo '<tr><td colspan="7"><table width="100%" border="0" align="center"><tr><td><div align="left"><h2>'.$thismo.'<br />'.$this->year.'</h2></div></td><td><div align="center"><b>Country:</b>______________________</div></td><td><div align="right"><p>Sales Teams:____________________________________(1)</p><p>____________________________________(2)</p></div></td></tr></table></td></tr>';
       
        echo '<tr>';
echo '<td><a href="?'.$this->preYearUrl($this->year,$this->month).'">'.'<<'.'</a></td>';
echo '<td><a href="?'.$this->preMonthUrl($this->year,$this->month).'">'.'<'.'</a></td>';
        echo '<td colspan="3"><form>';
       
        echo '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
        for($ye=1970; $ye<=2038; $ye++) {
            $selected = ($ye == $this->year) ? 'selected' : '';
            echo '<option '.$selected.' value="'.$ye.'">'.$ye.'</option>';
        }
        echo '</select>';
        echo '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value+\'\'">';
       
       
        for($mo=1; $mo<=12; $mo++) {
            $selected = ($mo == $this->month) ? 'selected' : '';
            echo '<option '.$selected.' value="'.$mo.'">'.$mo.'</option>';
        }
        echo '</select>';
        echo '</form></td>';
echo '<td><a href="?'.$this->nextMonthUrl($this->year,$this->month).'">'.'>'.'</a></td>';
echo '<td><a href="?'.$this->nextYearUrl($this->year,$this->month).'">'.'>>'.'</a></td>';
        echo '</tr>';
    }
   
    private function preYearUrl($year,$month)
    {
        //起屹年份,如修改請一併變更function preMonthUrl
$year = ($this->year <= 1970) ? 1970 : $year - 1 ;
       
        return 'year='.$year.'&month='.$month;
    }
   
    private function nextYearUrl($year,$month)
    {
        //結束年份,如修改請一併變更function nextMonthUrl
$year = ($year >= 2038)? 2038 : $year + 1;
       
        return 'year='.$year.'&month='.$month;
    }
   
    private function preMonthUrl($year,$month)
    {
        if ($month == 1) {
            $month = 12;
            $year = ($year <= 1970) ? 1970 : $year - 1 ;
        } else {
            $month--;
        }      
       
        return 'year='.$year.'&month='.$month;
    }
   
    private function nextMonthUrl($year,$month)
    {
        if ($month == 12) {
            $month = 1;
            $year = ($year >= 2038) ? 2038 : $year + 1;
        }else{
            $month++;
        }
        return 'year='.$year.'&month='.$month;
    }
   
}


沒有留言:

張貼留言