posted on 06 Dec 2009 20:18 by php-ajax-code in jquery
<!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 rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css"
type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#datepicker").datepicker({showOn: 'button', buttonImage: 'calendar.gif',
buttonImageOnly: true, dateFormat : 'yy-mm-dd'});
});
</script>
<style type="text/css">
body { font-family:Tahoma, Geneva, sans-serif; font-size:11px;}
</style>
</head>
<body>
<label>เลือกวันที่ : </label><input type="text" id="datepicker" name="datepicker">
</body>
</html>
สำหรับโค้ด PHP Class สร้าง Dropdown-list เลือกวันที่-เดือน-ปี แบบไทยๆ ตัวนี้ผมผมได้ทดลองใช้งานดูแล้วทำงานได้ปรกติไม่ต่างกับโค้ดที่ผมเขียนแบบธรรมดาแต่ว่า มันมีข้อดีก็คือถ้าเราต้องการสร้าง
Dropdown list มาใช้งานซึ่ง properties ของ dropdown ต้องตรงกับที่เขียนไว้ใน class ถ้าไม่ตรงก็ดัดแปลงได้ครับ
เราไม่จำเป็นต้องเขียนโค้ดใหม่ทั้งหมด แค่เรา สร้าง object ขึ้นมาแล้วเรียกฟังก์ชันส่ง parameter เข้าไป class ตัวนี้ก็
จะทำการสร้าง dropdown list ให้เราเองครับ
Demo:> http://downstream.freetzi.com/dropdown_list_date/list_thai_date.test.php แบบ ธรรมดา
Demo:> http://downstream.freetzi.com/dropdown_list_date/list_thai_date.test.php แบบ Class
ตัว Download ผมได้ใส่ไว้ทั้งสอง version ครับ:
Download: http://downstream.freetzi.com/dropdown_list_date/dropdown_list_date.rar
ถ้าผิดพลาดประการใดขอต้องขอ อภัย ณ ที่นี้ด้วยครับ
ถ้ามีปัญหาประการได้ก็ comment ไว้นะครับ
File: list_thai_date.class.php
<?php
class listSelThaiDate {
protected $selName;
protected $selID;
protected $selected;
protected $selectOp;
protected $start;
protected $stop;
protected $endl = "\n";
protected $month = array();
function setSelectDayAndYear($selName, $selID, $start, $stop, $selected) {
$this->selName = $selName;
$this->selID = $selID;
$this->selected = $selected;
$this->start = $start;
$this->stop = $stop;
}
function setSeletMonth($selName, $selID, $arrMonth, $selected) {
$this->selName = $selName;
$this->selID = $selID;
$this->selected = $selected;
$this->month = $arrMonth;
}
function getSelectDayAndYear() {
echo '<select name="'.$this->selName.'">'.$this->endl;
foreach (range ($this->start, $this->stop) as $val) {
$this->selectOp = ($this->selected == $val) ? 'selected="selected"' : '';
echo '<option value="'.$val.'" '.$this->selectOp.'>'.$val.'</option>'.$this->endl;
}
echo '</select>'.$this->endl;
}
function getSelectMonth() {
echo '<select name="'.$this->selName.'">'.$this->endl;
foreach ($this->month as $key=>$val) {
$this->selectOp = ($this->selected == $key) ? 'selected="selected"' : '';
echo '<option value="'.$key.'" '.$this->selectOp.'>'.$val.'</option>'.$this->endl;
}
echo '</select>'.$this->endl;
}
}
?>
File: list_thai_date.test.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>
</head>
<body>
<?php
require_once ('list_thai_date.class.php');
$d = date('d');
$m = date('m');
$y = 2533;
$month = array('01'=>'มกราคม', '02'=>'กุมภาพันธ์', '03'=>'มีนาคม', '04'=>'เมษายน',
'05'=>'พฤษภาคม', '06'=>'มิถุนายน', '07'=>'กรกฎาคม','08'=>'สิงหาคม',
'09'=>'กันยายน','10'=>'ตุลาคม','11'=>'พฤศจิกายน','12'=>'ธันวาคม');
$select = new listSelThaiDate();
$select->setSelectDayAndYear('sDay', 'sDay', 1, 31, $d);
$select->getSelectDayAndYear();
$select->setSeletMonth('sMonth', 'sMonth', $month, $m);
$select->getSelectMonth();
$select->setSelectDayAndYear('sYear', 'sYear', 2500, 2552, $y);
$select->getSelectDayAndYear();
?>
</body>
</html>
<!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>Dropdown-List-เลือกวันที่-เดือน-ปี-ไทย</title>
</head>
<body>
<?php
$d = date('d');
$m = date('m');
$y = 2533;
echo $date;
$month = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน",
"กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
?>
<select name="sDay">
<option>วัน</option>
<?php foreach (range (1, 31) as $val) { ?>
<option value="<?=$val?>" <?=($val == $d) ? 'selected="selected"' : '' ?>><?=$val?></option>
<?php } ?>
</select>
<select name="sMonth">
<option>เดือน</option>
<?php foreach ($month as $key=>$val) { ?>
<option value="<?=$key?>" <?=($key == $m-1) ? 'selected="selected"' : '' ?>><?=$val?></option>
<?php } ?>
</select>
<select name="sYear">
<option>ปี</option>
<?php foreach (range (2500, 2533) as $val) { ?>
<option value="<?=$val?>" <?=($val == $y) ? 'selected="selected"' : '' ?>><?=$val?></option>
<?php } ?>
</select>
</body>
</html>