|
|
|
แจกตัวอย่างโค๊ด ทำ listbox 3 select จาก database |
|
|
|
|
|
|
|
ตัวอย่างโค๊ด ทำ listbox 3 select จาก database
ต.ย. มี 3 ตารางดังนี้
ตารางที่1 = ชื่อ select1 มี 2 ฟิวด์ ( id_s1(pk) , name_s1 )
ตารางที่2 = ชื่อ select2 มี 3 ฟิวด์ ( id_s2(pk) , name_s2 , id_s1(fk) )
ตารางที่3 = ชื่อ select3 มี 3 ฟิวด์ ( id_s3(pk) , name_s3 , id_s2(fk) )
ต้องเลือก select1 ก่อน แล้วเลือก select2 แล้วเลือก select3
select1 --> select2 -->select3 ตามนี้
<script type="text/javascript">
var select1 = new Array();
var select2 = new Array();
var select3 = new Array();
</script>
<select id="id_select[1]" name="name1" onchange="return show_select_next(1,2,select2)" ><option value="select1">select1</option></select>
<select id="id_select[2]" name="name2" onchange="return show_select_next(2,3,select3)"><option value="select2">select2</option></select>
<select id="id_select[3]" name="name3"><option value="select3">select3</option></select>
<?
$select1 = mysql_query("SELECT * FROM select1");
for($s1=0;$s1<mysql_num_rows($select1);$s1++)
{
$arr_s1=mysql_fetch_array($select1);
// echo $arr_s1['name_s1'];
?>
<script language="javascript">
select1[<?=$s1?>] = '<?=$arr_s1['name_s1']?>';
select2['<?=$arr_s1['name_s1']?>'] = new Array();
</script>
<?
// echo "<br>";
$select2 = mysql_query("SELECT * FROM select2 where id_s1 = '".$arr_s1['id_s1']."'");
for($s2=0;$s2<mysql_num_rows($select2);$s2++)
{
$arr_s2=mysql_fetch_array($select2);
// echo " '-- ".$arr_s2['name_s2'];
?>
<script language="javascript">
select2['<?=$arr_s1['name_s1']?>'][<?=$s2?>] = '<?=$arr_s2['name_s2']?>';
select3['<?=$arr_s2['name_s2']?>'] = new Array();
</script>
<?
// echo "<br>";
$select3 = mysql_query("SELECT * FROM select3 where id_s2 = '".$arr_s2['id_s2']."'");
for($s3=0;$s3<mysql_num_rows($select3);$s3++)
{
$arr_s3=mysql_fetch_array($select3);
// for($nbsp=0;$nbsp<14;$nbsp++){if($nbsp==5 && $s2 < mysql_num_rows($select2) -1 ){echo "'";} echo " ";}
// echo "'-- ".$arr_s3['name_s3']." ";
?>
<script language="javascript">
select3['<?=$arr_s2['name_s2']?>'][<?=$s3?>] = '<?=$arr_s3['name_s3']?>';
</script>
<?
// echo "<br>";
}
}
// echo "<br>";
}
?>
<script type="text/javascript">
var options1 = (parseFloat(select1.length)+1);
document.getElementById('id_select[1]').length = options1;
for(var ss1 = 1; ss1 < options1; ss1++)
{
document.getElementById('id_select[1]').options[ss1].text = select1[ss1-1];
document.getElementById('id_select[1]').options[ss1].value = select1[ss1-1];
}
function show_select_next(change_select,select_next,array_next)
{
var id_next = select_next ;
while(document.getElementById('id_select['+id_next+']'))
{
document.getElementById('id_select['+id_next+']').length=1;
document.getElementById('id_select['+id_next+']').options[0].selected =true
id_next++;
}
var name_text ;
var num_options = (parseFloat(array_next[document.getElementById('id_select['+change_select+']').value].length)+1);
document.getElementById('id_select['+select_next+']').length = num_options;
for(var nn = 1; nn < num_options; nn++)
{
name_text = array_next[document.getElementById('id_select['+change_select+']').value][nn-1];
document.getElementById('id_select['+select_next+']').options[nn].text = name_text;
document.getElementById('id_select['+select_next+']').options[nn].value = name_text;
}
}
</script>
Tag : - - - -
|
|
|
|
|
|
Date :
8 ม.ค. 2551 16:49:32 |
By :
heng |
View :
6920 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอถามต่อ จะเอาค่า id_s2(pk) เก็บลงฐานข้อมูลได้อย่างไร
|
|
|
|
|
Date :
13 ก.พ. 2551 18:59:59 |
By :
pitsack |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณ heng ครับ ลองนำโค๊ทไปใช้แล้วแต่มันฟ้อง การ connection กับ database ครับ
|
|
|
|
|
Date :
15 ก.พ. 2551 16:06:10 |
By :
vv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณน่ะครับ........ผมประสบความสำเร็จแล้วครับ
|
|
|
|
|
Date :
10 ก.ค. 2551 16:40:46 |
By :
chongwut |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2009-07-13 15:44:28 |
By :
asr |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|