โดย ammyamm » 18/05/2009 12:50 pm
พอดีจะทำฟอร์ม ค้นหาข้อมูลโดยเมื่อเลือกข้อมูลโรงงานใน listbox1 แล้วใน listbox2 ให้แสดงของแผนกในโรงงานที่เราเลือกในlistbox1 อ่ะค่ะ แต่พอเลือกโรงงานแล้ว ข้อมูลแผนกกลับไม่ดึงมาแสดงในlistbox2 ให้อ่ะค่ะ ต้องแก้อย่างไงค่ะ
ตัวอย่างform ที่ทำค่ะ
<form name="form1" action="<?echo $PHP_SELF?>" method="post" enctype="application/x-www-form-urlencoded" >
<table width="512" height="99" border="1" cellpadding="0" cellspacing="0" >
<tr><th class=xl1716864 colspan=6><font size="4"><?echo $REPNAME?></font></th></tr>
<tr><th colspan=6></th></tr>
</span>
</caption>
<tr class=xl1726864>
<th width="117" ><span class="style2">รหัสโรงงาน<?echo $paramname ?></span></th>
<th width="172" colspan=3>
<select name="mc_factory" size="1" onChange="return submitForm('mc_factory')">
<?$master->getListofData( "mc_factory", "", "ASC" )?>
</select>
</th>
</tr>
<tr class=xl1726864>
<th width="117" ><span class="style2">เริ่มจากรหัส<?echo $paramname ?></span></th>
<th width="172"><span class="style2">
<select name="start_code" size="1" ><?$master->getListofDataWithCondition( "machine", "", "ASC", " mc_factory = '$mc_factory' " )?></select>
</span></th>
<th width="60"><span class="style2">ถึง</span></th>
<th width="172"><select name="end_code" size="1"><?$master->getListofDataWithCondition( "machine", "", "ASC", " mc_factory = '$mc_factory' " )?></select></th>
</tr>
<tr class=xl1726864>
<th width="117"><span class="style2">ตั้งแต่วันที่</span></th>
<th width="172">
<span class="style2">
<select name="s_dd" size="1"><? $mydate->showOptionDate( $d1, $m1, $y1 )?></select>
<select name="s_mm" size="1"> <? $mydate->showOptionMonth( $d1, $m1, $y1 )?></select>
<select name="s_yyyy" size="1"><? $mydate->showOptionYear( $d1, $m1, $y1 )?></select>
</span></th>
<th width="60"><span class="style2">ถึง</span></th>
<th width="172">
<select name="e_dd" size="1"><? $mydate->showOptionDate( $d1, $m1, $y1 )?></select>
<select name="e_mm" size="1"><? $mydate->showOptionMonth( $d1, $m1, $y1 )?></select>
<select name="e_yyyy" size="1"><? $mydate->showOptionYear( $d1, $m1, $y1 )?></select>
</th>
</tr>
<tr>
<th colspan="3" align="right" style="border-right:none"><input name="ตกลง" type="submit" value="ตกลง"></th>
<th align="right" ><input name="ปิดหน้านี้" type="button" value="ปิดหน้านี้" onClick="window.close();window.opener.focus();"></th>
</tr>
</table>
<input name="action" value="P" type="hidden" id="action">
</form>
โดยใน list box1 จะเลือกฟังก์ชั่น getListofData จากอีกไฟล์มาอ่ะค่ะ
ตัวอย่าง ฟังก์ชั่น getListofData ค่ะ
function getListofData( $tabname, $value, $order )
{
$isselected = false;
$sql = "SELECT * FROM $tabname WHERE status = 'A' order by code $order";
//if ( strlen( $value ) > 0 ) $sql .= " AND code Like $value";
//echo $sql;
$result = mysql_query( $sql );
$errmsg =mysql_error();
//echo "$errmsg";
$isselected = false;
if ( $result && mysql_affected_rows() > 0 )
{
for ( $i=0; $i < mysql_affected_rows(); $i++ )
{
$row = mysql_fetch_object($result);
//while ( $row = mysql_fetch_object($result) )
//{
$th = $row->nameth;
$en = $row->nameen;
$code = $row->code;
if ( strlen( $th ) > 0 )
{
$name = $th;
}else $name = $en;
if ( strlen( $value ) <= 0 )
{
echo "<option value=\"$code\">$code : $name</option>";
}else {
if ( $code == $value )
{
$isselected = true;
echo "<option value=\"$code\" selected>$code : $name</option>";
}else{
echo "<option value=\"$code\">$code : $name</option>";
}
}//end if =?
}//end while/
mysql_free_result( $result );
}
if ( !$isselected )
{
echo "<option value=\"\" selected> </option>";
}// end if result?
}
พอดีจะทำฟอร์ม ค้นหาข้อมูลโดยเมื่อเลือกข้อมูลโรงงานใน listbox1 แล้วใน listbox2 ให้แสดงของแผนกในโรงงานที่เราเลือกในlistbox1 อ่ะค่ะ แต่พอเลือกโรงงานแล้ว ข้อมูลแผนกกลับไม่ดึงมาแสดงในlistbox2 ให้อ่ะค่ะ ต้องแก้อย่างไงค่ะ
ตัวอย่างform ที่ทำค่ะ
<form name="form1" action="<?echo $PHP_SELF?>" method="post" enctype="application/x-www-form-urlencoded" >
<table width="512" height="99" border="1" cellpadding="0" cellspacing="0" >
<tr><th class=xl1716864 colspan=6><font size="4"><?echo $REPNAME?></font></th></tr>
<tr><th colspan=6></th></tr>
</span>
</caption>
<tr class=xl1726864>
<th width="117" ><span class="style2">รหัสโรงงาน<?echo $paramname ?></span></th>
<th width="172" colspan=3>
<select name="mc_factory" size="1" onChange="return submitForm('mc_factory')">
<?$master->getListofData( "mc_factory", "", "ASC" )?>
</select>
</th>
</tr>
<tr class=xl1726864>
<th width="117" ><span class="style2">เริ่มจากรหัส<?echo $paramname ?></span></th>
<th width="172"><span class="style2">
<select name="start_code" size="1" ><?$master->getListofDataWithCondition( "machine", "", "ASC", " mc_factory = '$mc_factory' " )?></select>
</span></th>
<th width="60"><span class="style2">ถึง</span></th>
<th width="172"><select name="end_code" size="1"><?$master->getListofDataWithCondition( "machine", "", "ASC", " mc_factory = '$mc_factory' " )?></select></th>
</tr>
<tr class=xl1726864>
<th width="117"><span class="style2">ตั้งแต่วันที่</span></th>
<th width="172">
<span class="style2">
<select name="s_dd" size="1"><? $mydate->showOptionDate( $d1, $m1, $y1 )?></select>
<select name="s_mm" size="1"> <? $mydate->showOptionMonth( $d1, $m1, $y1 )?></select>
<select name="s_yyyy" size="1"><? $mydate->showOptionYear( $d1, $m1, $y1 )?></select>
</span></th>
<th width="60"><span class="style2">ถึง</span></th>
<th width="172">
<select name="e_dd" size="1"><? $mydate->showOptionDate( $d1, $m1, $y1 )?></select>
<select name="e_mm" size="1"><? $mydate->showOptionMonth( $d1, $m1, $y1 )?></select>
<select name="e_yyyy" size="1"><? $mydate->showOptionYear( $d1, $m1, $y1 )?></select>
</th>
</tr>
<tr>
<th colspan="3" align="right" style="border-right:none"><input name="ตกลง" type="submit" value="ตกลง"></th>
<th align="right" ><input name="ปิดหน้านี้" type="button" value="ปิดหน้านี้" onClick="window.close();window.opener.focus();"></th>
</tr>
</table>
<input name="action" value="P" type="hidden" id="action">
</form>
โดยใน list box1 จะเลือกฟังก์ชั่น getListofData จากอีกไฟล์มาอ่ะค่ะ
ตัวอย่าง ฟังก์ชั่น getListofData ค่ะ
function getListofData( $tabname, $value, $order )
{
$isselected = false;
$sql = "SELECT * FROM $tabname WHERE status = 'A' order by code $order";
//if ( strlen( $value ) > 0 ) $sql .= " AND code Like $value";
//echo $sql;
$result = mysql_query( $sql );
$errmsg =mysql_error();
//echo "$errmsg";
$isselected = false;
if ( $result && mysql_affected_rows() > 0 )
{
for ( $i=0; $i < mysql_affected_rows(); $i++ )
{
$row = mysql_fetch_object($result);
//while ( $row = mysql_fetch_object($result) )
//{
$th = $row->nameth;
$en = $row->nameen;
$code = $row->code;
if ( strlen( $th ) > 0 )
{
$name = $th;
}else $name = $en;
if ( strlen( $value ) <= 0 )
{
echo "<option value=\"$code\">$code : $name</option>";
}else {
if ( $code == $value )
{
$isselected = true;
echo "<option value=\"$code\" selected>$code : $name</option>";
}else{
echo "<option value=\"$code\">$code : $name</option>";
}
}//end if =?
}//end while/
mysql_free_result( $result );
}
if ( !$isselected )
{
echo "<option value=\"\" selected> </option>";
}// end if result?
}