พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
Javascript development / รบกวนขอสอบถามวิธีการแปลงโค้ดdrop-down
รุต
รุต
Mon 13 Mar 2006 13:35:51

รบกวนขอสอบถามวิธีการแปลงโค้ดdrop-downด้านล่างนี้ ให้ดึงข้อมูลมาจากฐานข้อมูลmysql ทั้ง2ข้างเลยนะครับ

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
<script>
var a = new Array();
    a[0] = new Array(1, " - Select Mobile Serie - ");
    a[1] = new Array(
       101,"3210",
       102,"3310",
       103,"8250"
    );
    a[2] = new Array(
       201,"A30",
       202,"C45",
       203,"SL42",
       204,"M50"
    );
    a[3] = new Array(
       301,"T300",
       302,"T190",
       303,"V90",
       304,"V8088"
    );

function clearcombo(elem){
 var i;
 for (i = elem.options.length - 1; i >= 0; i--) elem.options[i] = null;
 elem.selectedIndex = -1;
}
function populatelist2(elem, index){
 for (var i = 0; i < a[index].length; i= i + 2){
  elem.options[elem.options.length] = new Option(a[index][i + 1], a[index][i]);
 }
}
function clicklist(elem){
 clearcombo(document.f1.list2)
 populatelist2(document.f1.list2, elem[elem.selectedIndex].value);
 return true;
}
</script>

</head>

<body>
<form name=f1>
<table>
<tr>
<td>Mobile
<select NAME="list1" ONCHANGE="return(clicklist(this));">
 <option VALUE="0"> - Select Mobile - </option>
 <option value="1">Nokia</option>
 <option value="2">Siemen</option>
 <option value="3">Motorola</option>
</select>
</td>
<td>Serie
<select NAME="list2">
 <option VALUE=""> - Select Mobile Serie - </option>
</select>
</td>
</tr>
</table>
</form>

</body>
รบกวนขอโค้ดตัวอย่างด้วยนะครับ
ขอบคุณมากครับ

Administrator
Mon 13 Mar 2006 14:37:57
ต้องทำการวนลูปเพื่อ select 2 รอบครับ รอบแรกเพื่อหาหมวดหมู่หลักก่อนครับ
รอบที่สองจึงหารายละเอียด

เช่นถ้าตารางเป็น
id int(6) auto_increment,
parent int(6)
name varchar(50)

<?
//establish connection
//query

//First loop
$res = mysql_query("SELECT id FROM table_name WHERE parent=0");
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
    echo "a[".$row["id"]."] = new Array(";
    //Second loop
    $reulst = mysql_query("SELECT id, name FROM table_name WHERE parent=".$row["id"]);
    while($rec = mysql_fetch_array($result, MYSQL_ASSOC)){
       echo $row["id"].sprintf("%02d", $rec["id"]).", ".$rec["name"].",";
    }
    //End second loop
    echo ");";
}
//End first loop

//close connection
?>

ประมาณนี้ครับ เอาเป็นไกด์นะครับ
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login