 |
รบกวนเรื่องการ search ด้วย text และ checkbox ครับ หากต้องการ search ด้วย text พร้อมกับ checkbox |
|
 |
|
|
 |
 |
|
รบกวนด้วยครับ T_T
|
 |
 |
 |
 |
Date :
2010-05-27 15:27:52 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$strsql = "SELECT * FROM test WHERE " ; บรรทัดนี้ ทำไมเขียนไม่ครบอะครับค่าที่มาจาก where อะครับ
|
 |
 |
 |
 |
Date :
2010-05-27 15:31:09 |
By :
SOUL |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ครบยังไงอะครับ คือตอนนี้ผมติดที่ยังไม่สามารถเอาค่าของการใช้ checkbox มา search ได้อะครับ สามารถใช้ text ในการ search ได้อย่างเดียว
|
 |
 |
 |
 |
Date :
2010-05-27 19:15:03 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่างแรกผมแนะนำให้ ใช้ methd="post" ดีกว่าครับ และถ้า php อยู่หน้าเดียวกับ html ก้อไม่ต้องระบุ action ก็ได้ครับ ตามนี้เลย ส่วนตอนเก็บ ค่าของ checkbox อยากเก็บแบบไหนครับ จะได้บอกถูก
Code (PHP)
<?php
extract($_POST);
?>
<form name="frmSearch" method="post">
<table width="703" border="0" align="center">
<tr>
<th height="10" colspan="5" bgcolor="#EDEDED">test</th>
</tr>
<tr>
<th height="10" colspan="5" bgcolor="#FFFFFF">ทดสอบ
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$txtKeyword?>" /></th>
</tr>
<tr>
<th width="154" height="22" bgcolor="#FFFFFF">ก</th>
<th width="111" align="left" bgcolor="#FFFFFF"><input name="test[]" type="checkbox" id="test[]" value="A" <?=(in_array('A',$test)?'checked':'')?>/>
A</th>
<th width="110" align="left" bgcolor="#FFFFFF"><input name="test[]" type="checkbox" id="test[]" value="B" <?=(in_array('B',$test)?'checked':'')?>/>
B</th>
<th width="110" align="left" bgcolor="#FFFFFF"><input name="test[]" type="checkbox" id="test[]" value="C" <?=(in_array('C',$test)?'checked':'')?>/>
C</th>
<th width="141" align="left" bgcolor="#FFFFFF"><input name="test[]" type="checkbox" id="test[]" value="D" <?=(in_array('D',$test)?'checked':'')?>/>
D</th>
</tr>
<tr>
<th height="22" bgcolor="#FFFFFF">ข</th>
<th width="111" align="left" bgcolor="#FFFFFF"><input name="num[]" type="checkbox" id="num[]" value="1" <?=(in_array('1',$num)?'checked':'')?>
/>
1 </th>
<th width="110" align="left" bgcolor="#FFFFFF"><input name="num[]" type="checkbox" id="num[]" value="2" <?=(in_array('2',$num)?'checked':'')?>
/>
2</th>
<th width="110" align="left" bgcolor="#FFFFFF"><input name="num[]" type="checkbox" id="num[]" value="3" <?=(in_array('3',$num)?'checked':'')?>/>
3</th>
<th width="141" align="left" bgcolor="#FFFFFF"><input name="num[]" type="checkbox" id="num[]" value="4" <?=(in_array('4',$num)?'checked':'')?>/>
4</th>
</tr>
<tr>
<th colspan="5" bgcolor="#EDEDED"><input type="submit" name="button" id="button" value="Search" /></th>
</tr>
</table>
</form>
|
 |
 |
 |
 |
Date :
2010-05-27 19:32:12 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามหน่อยครับ
Code (PHP)
<?php
extract($_POST);
?>
คือคำสั่งใช้ทำอะไรครับ ส่วนฟังชั่น in_array ตอนผมเขียนแล้วลองพรีวิวในเครื่องใช้ได้ปกตินะครับ แต่พอไปอัพเว็ปจริงๆแล้วมันขึ้น error function อะครับ ไม่รู้ว่าสาเหตุเป็นที่อะไร
ส่วนเชกบอกเก็บเป็นอเรย์ครับผม
ขอบคุณพี่ๆมากครับ อยู่ที่นี่ได้ความรู้มากมายเลยครับ
|
 |
 |
 |
 |
Date :
2010-05-27 19:39:55 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
extract() fuction นี้ จะทำหน้าแตก array ออกมาเป็นตัวแปรครับ เช่น
Code (PHP)
$arr = array('t1'=>1, 't2' => 2);
extract($arr);
เท่ากับเรามีตัวแปร
Code (PHP)
$t1 = 1;
$t2 = 2;
function นี้ ใช้เพื่อความสะดวกในการเรียกใช้ตัวแปรเวลาเรา get หรือ post มา คือ ไม่ต้องมานั่งเรียก
Code (PHP)
$_POST['ตัวแปร']
แบบนี้อ่ะครับ
แต่ถ้าบางหน้าเรามีทั้ง get และ post เราอาจใช้
Code (PHP)
extract($_REQUEST);
เลยก้อได้ครับ เพราะ $_REQUEST จะรวม array $_POST และ $_GET อยู่ด้วยกัน
ส่วนค่าใน check box อาจจะเก็บเป็น list ก้อได้ครับ โดยใช้คำสั่ง
Code (PHP)
$testList = explode(',', $test); // output : 1,2,3,4
|
 |
 |
 |
 |
Date :
2010-05-27 19:50:48 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ๆมากครับ คือตอนนี้ลองแก้แล้ว แต่ยังไม่เข้าวิธี search จากการ ใช้ checkbox อะครับ เช่น สมมติเราไม่ใส่ text ในการ search จะติกเฉพาะ checkbox อย่างเดียวอะครับ เป็นต้น
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-05-27 20:11:46 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าแบบนั้นเราต้อง check ก่อนว่า มีการใส่ keyword มามั้ย ถ้าไม่มี ก้อไม่ต้องใส่ เงื่อนไข sql นั้น ประมาณนี้
Code (PHP)
$strinput = trim($txtKeyword);
if($strinput != ''){
$arrWord = split(' ',$strinput);
for($i=0; $i<count($arrWord);$i++){
$strsql .= " name LIKE '%".$arrWord[$i]."%' OR" ;
}
$strsql=substr($strsql,0,strlen($strsql)-3) ;
}
|
 |
 |
 |
 |
Date :
2010-05-27 20:22:58 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สมมติว่าผมจะให้ txtKeyword มา And กับ test ซึ่งบางครั้งถ้าเราไม่ใส่ txtKeyword มาเราต้องไม่ใส่ And ด้านหลังคำสั่ง sql อะครับ หรือถ้าผมไม่ใส่ txtKeyword มาเลย แต่ถ้าผม ติก Checkbox ในช่อง test กับ num มา ผมต้องเขียนอย่างไรครับ คือตอนนี้ผมงง กับการใส่ And เนี่ยละครับ
|
 |
 |
 |
 |
Date :
2010-05-27 20:27:24 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้วิธีนี้ครับ
Code (PHP)
<?
$strsql = "SELECT * FROM test WHERE 1=1 " ;
$strinput = trim($txtKeyword);
if($strinput != ''){
$arrWord = split(' ',$strinput);
for($i=0; $i<count($arrWord);$i++){
$strsql .= "AND name LIKE '%".$arrWord[$i]."%' OR" ;
}
$strsql=substr($strsql,0,strlen($strsql)-3) ;
}
if(is_array($test)){
$strsql .= "AND test = ..............
}
?>
เราจะกำหนด WHERE 1=1 ไปเลย เพื่อที่เราสมารถเพิ่มเงื่อนไขโดยใส่ And ได้เลย
|
 |
 |
 |
 |
Date :
2010-05-27 20:49:25 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ครับอธิบาย where 1=1 ทีนะครับ ไม่เข้าใจจริงๆครับ T_T
|
 |
 |
 |
 |
Date :
2010-05-27 21:07:44 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
where 1=1 ก้อคือ ใส่เพื่อให้ มันเป็นจิง เท่านั้น อาจเป็น 'test' = 'test' , 2 = 2 ก้อได้ ใส่เพื่อที่เราสามารถ จะเติมเงื่อนไขใดๆ ต่อท้าย ได้อย่างง่ายๆ โดย เติม And แล้วตามด้วยเงื่อนไขใดๆ มันจะใช้ในกรณี ที่เรามีเงื่อนไข หลายๆอย่าง ซึ่งอาจจะมีหรือไม่มีเงื่อนไขนั้นๆก้อได้ อย่างเช่น case ที่ คุณ cc1 กำลังเจอ
|
 |
 |
 |
 |
Date :
2010-05-27 21:24:53 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ครับเดวผมขอลองก่อนนะครับ ติดยังตรงไหนจะมาถามอีกทีครับ ขอบคุณพี่มากๆครับผม
|
 |
 |
 |
 |
Date :
2010-05-27 21:29:02 |
By :
cc1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอคะแนนบ้างนะคับ 
|
 |
 |
 |
 |
Date :
2010-05-27 21:58:41 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|