|
|
|
รบกวนผู้รู้ช่วยเรื่องการทำ listmenu 2 ระดับ หน่อยค่ะ |
|
|
|
|
|
|
|
ตัวอย่างที่คุณว่าก็นำมาประยุกต์ได้ง่ายนะครับ https://www.thaicreate.com/community/dependant-listmenu-dropdownlist.html
ผมลองช่วยดูให้นะไม่รู้ใช้ได้ไหม
Code (PHP)
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("thailand");
@mysql_query("SET NAMES UTF8");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ThaiCreate.Com ListMenu</title>
<script language = "JavaScript">
//**** ListMajor (Start) ***//
function ListMajor(SelectValue)
{
frmMain.Faculty.length = 0
frmMain.Major.length = 0
//*** Insert null Default Value ***//
var myOption = new Option('','')
frmMain.Major.options[frmMain.Major.length]= myOption
<?
$intRows = 0;
$strSQL = "SELECT * FROM Major ORDER BY Major_ID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
?>
x = <?=$intRows;?>;
mySubList = new Array();
strGroup = <?=$objResult["Faculty_ID"];?>;
strValue = "<?=$objResult["Major_ID"];?>";
strItem = "<?=$objResult["Major_ENName"];?>";
mySubList[x,0] = strItem;
mySubList[x,1] = strGroup;
mySubList[x,2] = strValue;
if (mySubList[x,1] == SelectValue){
var myOption = new Option(mySubList[x,0], mySubList[x,2])
frmMain.Major.options[frmMain.Major.length]= myOption
}
<?
}
?>
}
//**** List Major (End) ***//
</script>
</head>
<form name="frmMain" action="" method="post">
คณะ
<select id="Faculty" name="Faculty" onChange = "ListMajor(this.value)">
<option selected value=""></option>
<?
$strSQL = "SELECT * FROM Faculty ORDER BY Faculty_ID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["Faculty_ID"];?>"><?=$objResult["Faculty_ENName"];?></option>
<?
}
?>
</select>
สาขา
<select id="Major" name="Major" style="width:120px" </select>
</form>
</body>
</html>
<?
mysql_close($objConnect);
?>
|
ประวัติการแก้ไข 2012-01-05 23:33:12 2012-01-05 23:35:14
|
|
|
|
Date :
2012-01-05 23:29:37 |
By :
000su000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณ Ek_zab นะคะ faculty ออกแล้ว แต่ major ยังไม่ออกเลย
|
|
|
|
|
Date :
2012-01-05 23:46:49 |
By :
zeenanz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูชื่อ table กับ ชื่อ ฟิว ดูนะครับ ผมทำให้คร่าวๆ ถ้ายังไม่ได้ก็บอกนะครับ
|
|
|
|
|
Date :
2012-01-06 00:01:22 |
By :
000su000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เปลี่ยนแล้วค่ะ ยังไม่ออกเลย
|
|
|
|
|
Date :
2012-01-08 19:39:06 |
By :
zeenanz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าเปลี่ยนรูปแบบไปเลยจะไหวมั้ยครับ
index.php
<?php
require_once('PhpConsole/PhpConsole.php');
PhpConsole::start();
$conn = mysql_connect('localhost', 'root', '1234');
mysql_select_db("demo");
mysql_query("SET NAMES UTF8");
$sql = "SELECT * FROM faculty";
$res = mysql_query($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 0.20" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script type="text/javascript">
$(function() {
$("#faculty").change(function(){
$("#major").html('<option value="">เลือก</option>');
var facultyId = $("#faculty").val();
$.getJSON('ajaxAction.php?faculty=' + facultyId, function(data) {
$.each(data, function(key, obj) {
$("#major").append('<option value="' + obj.major_id + '">' + obj.major_thname + '</option>');
});
});
});
});
</script>
</head>
<body>
<select name="faculty" id="faculty">
<option value="">เลือก</option>
<?php
while($row = mysql_fetch_assoc($res)) {
debug($row['faculty_thname']);
echo '<option value="', $row['faculty_id'],'">', $row['faculty_thname'] ,'</option>'."\n";
}
?>
</select>
<select name="major" id="major">
<option value="">เลือก</option>
</select>
</body>
</html>
ajaxAction.php
<?php
require_once('PhpConsole/PhpConsole.php');
PhpConsole::start();
$conn = mysql_connect('localhost', 'root', '1234');
mysql_select_db("demo");
mysql_query("SET NAMES UTF8");
$sql = "SELECT * FROM major";
$res = mysql_query($sql);
$faculty = isset($_GET['faculty']) ? $_GET['faculty'] : '';
$sql = "SELECT * FROM major WHERE faculty_id = '{$faculty}'";
$res = mysql_query($sql);
debug($sql);
$data = array();
$i = 0;
while($row = mysql_fetch_assoc($res)) {
$data[$i]['major_id'] = $row['major_id'];
$data[$i]['major_thname'] = $row['major_thname'];
$i++;
}
echo json_encode($data);
|
|
|
|
|
Date :
2012-01-09 19:05:50 |
By :
nautilus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|