|
 |
|
ช่วยรบกวนแก้ไขโค๊ดโปรแกรม เกี่ยวกับการเลือกรายการในช่อง DropDown List ให้ด้วยครับ |
|
 |
|
|
 |
 |
|
ผมจะเขียตัวอย่างการ WHERE นะครับ
$strSQL = "SELECT * FROM table WHERE 1 ";
if(trim($_POST["Status"]) != "")
{
$strSQL .= " AND STATUS = '".$_POST["Status"]."' ";
}
|
 |
 |
 |
 |
Date :
2010-04-21 22:54:23 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ้าว พี่วินครับ แล้วผมจะเพิ่มหรือแก้ไขตรงไหนละครับ
รบกวนพี่วิน ด้วยนะ ขอขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2010-04-22 09:43:16 |
By :
riderman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอาโค๊ดนี้ไปใช้แทนดีกว่านะครับ
คงจะตรงกับความต้องการของคุณ
เลือกทั้งหมดก็ได้ด้วย เหมือน ๆ กันเลยครับ
-----------------------------------------------------
<?php
$connDB = mysql_connect("localhost","user","user") or die ("Cannot connect to Database");
mysql_query("SET NAMES utf-8",$connDB);
mysql_query("USE db_report");
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
<script language="javascript1.2" type="text/javascript">
function chSelect(){
document.location.href='testReportCom1.php?desCom='+(document.formReport.desCom.value);
}
</script>
</head>
<body>
<form name="formReport" method="GET" action="">
<?php
$_REQUEST['desCom'];
// loop DrowdownList > desCom
$qDC = "SELECT * FROM tb_comreport GROUP BY idCom";
$reDesc = mysql_query($qDC) or die ("$qDC");
?>
เลือกประเภท :
<select name="desCom" onChange="chSelect()">
<option value="0" selected="selected">-- ทุกประเภท --</option>
<?php
while($roDesc = mysql_fetch_array($reDesc)){
?>
<option value="<?php echo $roDesc['idCom']; ?>" <?php if($roDesc['idCom'] == $_REQUEST['desCom']){echo "selected";} ?>><?php echo $roDesc['desCom']; ?></option>
<?php } ?>
</select>
<table width="31%" border="1" cellpadding="0" cellspacing="3" class="border">
<tr>
<td width="10%" height="20" align="center">ลำดับ</td>
<td width="25%" height="20" align="center">ประเภท</td>
<td width="24%" height="20" align="center">ปีงบประมาณ</td>
<td width="41%" align="center">วันเวลาที่ซื้อ</td>
</tr>
<?php
if($_REQUEST['desCom']=="0" || $_REQUEST['desCom']==""){ // ไม่เลือกอะไรเลย ให้แสดงทั้งหมด
$sql = "SELECT * FROM tb_comreport ORDER BY idCom";
}else{ // เมื่อมีการเลือกเงื่อนไข
$sql = "SELECT * FROM tb_comreport WHERE ";
// เลือกประเภทที่ต้องการ และเลือกปีงบประมาณที่ต้องการ
if($_REQUEST['desCom']!="0"){
$sql .= "idCom = '".$_REQUEST['desCom']."'";
}
}
$res = mysql_query($sql) or die ("$sql");
while($row = mysql_fetch_array($res)){ // loop data
?>
<tr>
<td align="center"><?=$row['idCom'];?></td>
<td><?=$row['desCom'];?></td>
<td align="center"><?=$row['yearCom'];?></td>
<td align="center"><?=$row['dtCom'];?></td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
---------------------------------------------------------
|
 |
 |
 |
 |
Date :
2010-05-04 11:28:27 |
By :
pukmtec |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
THANK YOU
ผมเป็น webmaster ให้กับเว็บขายชุดเครื่องนอนแห่งหนึ่งครับ (เว็บภรรยาผมเอง)
ก่อนหน้านี้พัฒนาด้วย ASP แต่ตอนนี้ผ่านไป 5 ปีก็ยกเครื่องใหม่เปลี่ยนเป็น PHP ทั้งหมดครับ
ผมได้ทดลองนำ code ของคุณ pUkMtEc ไปทดสอบแล้ว สามารถใช้งานได้ดีมากๆครับ
จากกรณีตัวอย่างที่ให้มานี้ default ของ Dropdown List คือ --- ทุกประเภท ----
แล้วหลังจากนั้นถึงจะไปวน loop ด้วย while เพื่อเรียกเอาข้อมูลในฐานข้อมูลมาแสดงใน Listbox
ผมอยากสอบถามเพิ่มเติมใน code ดังกล่าวนี้สักนิดนึงครับ
ถ้าหากว่าเราต้องการเพิ่มคำว่า --- กรุณาเลือกหมวดหมู่--- ให้อยู่ก่อน --- ทุกประเภท ----
แล้วหลังจากนั้นก็ค่อย while เหมือนเดิม
ผมเจอปัญหาว่าขณะแสดงผลจริง มันจะแสดง Default อยู่ที่ --- ทุกประเภท -- ซึ่งจริงๆแล้วผมต้องการให้ Default อยู่ที่ --- กรุณาเลือกหมวดหมู่---
พยายามหลายวิธีก็ไม่หายครับ รบกวนแนะนำวิธีแก้ปัญหานี้ด้วยครับ
สิ่งที่ผมเปลี่ยนจาก Code เดิม ดูได้จากด้านล่างนี้ครับที่ ตัวหนา สีน้ำเงิน
<?php
$_REQUEST['desCom'];
// loop DrowdownList > desCom
$qDC = "SELECT * FROM tb_comreport GROUP BY idCom";
$reDesc = mysql_query($qDC) or die ("$qDC");
?>
เลือกประเภท :
<select name="desCom" onChange="chSelect()">
<option value="" selected="selected">-- กรุณาเลือกหมวดหมู่ --</option>
<option value="0" selected="selected">-- ทุกประเภท --</option>
<?php
while($roDesc = mysql_fetch_array($reDesc)){
?>
<option value="<?php echo $roDesc['idCom']; ?>" <?php if($roDesc['idCom'] == $_REQUEST['desCom']){echo "selected";} ?>><?php echo $roDesc['desCom']; ?></option>
<?php } ?>
</select>
ทดลองทั้งใส่ selected และไม่ใส่ selected ก็ไม่หายครับ

|
 |
 |
 |
 |
Date :
2010-05-26 10:49:50 |
By :
verawat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมแก้ปัญหาได้แล้วครับ ขอบคุณสำหรับคำแนะนำดีทุกครั้งที่มีให้ผมครับ
ตามด้านล่างนี้เลย ผมเพิ่มที่ตัวหนาสีดำด้านล่าแบบนี้ครับ
<?php
$_REQUEST['desCom'];
// loop DrowdownList > desCom
$qDC = "SELECT * FROM tb_comreport GROUP BY idCom";
$reDesc = mysql_query($qDC) or die ("$qDC");
?>
เลือกประเภท :
<select name="desCom" onChange="chSelect()">
<option value="" selected="selected">-- กรุณาเลือกหมวดหมู่ --</option>
<option value="0" <?php if(0 == $_REQUEST['desCom']){echo "selected";} ?>>-- ทุกประเภท --</option>
<?php
while($roDesc = mysql_fetch_array($reDesc)){
?>
<option value="<?php echo $roDesc['idCom']; ?>" <?php if($roDesc['idCom'] == $_REQUEST['desCom']){echo "selected";} ?>><?php echo $roDesc['desCom']; ?></option>
<?php } ?>
</select>
|
 |
 |
 |
 |
Date :
2010-05-26 22:11:43 |
By :
verawat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|