แค่เว็บๆหนึ่ง ! ที่อยากเก็บประสบการณ์
Home
 

Kohana กับ dropdown จังหวัด อำเภอ ตำบล

พอดีใช้ kohana3.2 ทำงาน 1 ชิ้น ผู้ใช้งานขี้เกียจพิมพ์ อยากคลิกๆๆๆๆๆ ง่ายๆ เลยหาบันทึก จังหวัด อำเภอ ตำบล ผ่าน dropdrown อ่านเจอในเว็ปไซต์ http://tappanom.blogspot.com/2010/09/kohana-ajax-jquery-dropdown-2.html เป็น kohana 2 ก็ลองเอามาเป็นตัวอย่าง อย่าเสียเวลาเลย มาเริ่มกันดี่กว่าครับ
1. download ไฟล์ จังหวัด จากนี้ครับ Download
2. สร้าง Model ขึ้นมา 3 Model ได้แก่ province.php ,amphur.php , district.php ส่วนรายละเอียดใน Model ไม่ขอพูดถึงนะครับ
3. สร้างไฟล์มา aa.php มา สมมติ ผมสร้างใน /view/person/aa.php
<div>
<form name=”frm1″ method=”post”>
<label>จังหวัด</label>
<?=form::select(“prv”, Model_Province::getprv(),@$prvid,array(‘onchange’ => ‘selectamp()’))?>
<label>อำเภอ</label>
<?=form::select(“ampid”,@$amp,@$ampid,array(‘onchange’ => ‘selectamp()’))?>
<label>ตำบล</label>
<?=form::select(“tum”,@$disct,””)?>
</form>
</div>
<script type=”text/javascript”>
function selectamp(){
var prvid = $(‘#prv’).val();
var ampid = $(‘#ampid’).val();
frm1.action = “<?=url::base()?>province/selectamp/”+prvid;
frm1.action = “<?=url::base()?>province/selectamp/”+prvid+”/”+ampid;
frm1.submit();
}
</script>
//————————————– End
4. สร้าง Model province เพื่อใช้งาน ดังนี้
<?php

 

Class Controller_Province extends Controller_Template{
public function action_index(){
$content = new View(“person/aa”);
$this->template->content = $content;
}
public function action_selectamp(){//id ที่รับมาจาก ajax
$prvid = $this->request->post(‘prv’);
$ampid = $this->request->post(‘ampid’);
$content = new View(‘person/aa’);
// อำเภอ
$arr = array();
$amps = ORM::factory(“amphur”)
->select(“id”,”amphur_name”)
->where(“province_id”, “=”,$prvid)
->find_all();
array_unshift($arr, ‘– อำเภอ –‘ );
foreach ($amps as $r){
$arr[$r->id] = $r->amphur_name;
}
$content->amp = $arr;
$content->prvid=$prvid;
$content->ampid=$ampid;
if ($ampid != NULL){
// ตำบล
$arr1 = array();
$discs = ORM::factory(“district”)
->select(“id”,”district_name”)
->where(“amphur_id”, “=”,$ampid)
->where(“province_id”, “=”,$prvid)
->find_all();
foreach ($discs as $r1){
$arr1[$r1->id] = $r1->district_name;
}
$content->disct = $arr1;
}
// echo Debug::vars($discs);
$this->template->content = $content;
}
}
?>
แค่นี้ก็เรียกใช้ได้แล้วครับ…
Off 

April 10, 2012 This post was written by Categories: เรื่องเล่ารายวัน No comments yet


Top