ค้นหาข้อมูลจาก list box

คือว่าผมทำการค้นหาข้อมูลจากฐานข้อมูลที่เป็น list box น่ะคับ แต่ทีนี้ เผอิญว่า list box ของผมนั้นมี ให้เลือก 16 รายการ แล้วที่นี้ผมติดตรงที่ค้นหาตามเงื่อนไขไม่ได้คับ บางทีก้เลือกแค่อันเดียวแต่บางทีเลือกหลายรายการ ผมก้เลยมาปรึกษาผู้รู้ครับ ช่วยผมทีน่ะครับ ผมงมตั้งนานแล้วครับยังไมใด้เลย
25 เม.ย. 2552 8 4,378

พิมผิดครับ จากlist  box เป็น check box น่ะคับ น่ะคับ โดยแต่ละ check box มี value=1 หมดคับ เพราะว่าในฐานข้อมูลของผมได้กำหนดค่าที่เก็บให้เป็นเลข 1 ครับ
#1

ลองดูเรื่อง input แบบ array บนเว็บ

ใช้ if แยกเอา แล้วเชื่อมแต่ละรายการด้วย OR
#2

 #######################################form############################
<table width="230" border="0" align="center" cellpadding="0" cellspacing="0" class="menu"  background="block_right/back_saduag.gif">
          <form name="frm1" action="show_saduag.php" method="post"><tr>
            <td height="30" colspan="4" align="center"  valign="top" class="menulink">&nbsp;</td>
          </tr>
         
       
          <tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="jod_rod">               </td>
            <td class="corner">ที่จอดรถ</td>
           
            <td  valign="top">  <input type="checkbox"   value="1"  name="furniture">               </td>
            <td>เฟอร์นิเจอร์</td>
          </tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="tel" >               </td>
            <td class="corner">โทรศัพท์</td>
           
            <td  valign="top">  <input type="checkbox"   value="1" name="air">               </td>
            <td>แอร์</td>
            </tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="pad_lom">               </td>
            <td class="corner">พัดลม</td>
           
            <td  valign="top">  <input type="checkbox"   value="1" name="ubc">               </td>
            <td>ยูบีซี</td>
            </tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="internet">               </td>
            <td class="corner">Internet</td>
           
            <td  valign="top">  <input type="checkbox"   value="1" name="swim">               </td>
            <td>สระว่ายน้ำ</td>
            </tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="police">               </td>
            <td class="corner">รปภ</td>
           
            <td  valign="top">  <input type="checkbox"   value="1" name="key_card">               </td>
            <td>คีย์การ์ด</td>
            </tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="tv">               </td>
            <td class="corner">โทรทัศน์</td>
           
            <td  valign="top">  <input type="checkbox"  value="1" name="yod_ren">               </td>
            <td>ตู้น้ำหยอดเหรียญ</td>
            </tr>
            <td  valign="top">  <input type="checkbox"   value="1" name="sak_pa">               </td>
            <td class="corner">เครื่องซักผ้าหยอดเหรียญ</td>
            <td  valign="top">  <input type="checkbox"  value="1" name="vong_jorn_pid">               </td>
            <td>กล้องวงจรปิด</td>
            </tr>
            <tr>
              <td  valign="top"><input type="checkbox"   value="1" name="fitnet" /></td>
              <td class="corner">ฟิตเนส</td>
              <td  valign="top"><input type="checkbox"  value="1" name="minimart" /></td>
              <td>มินิมาร์ท</td>
            <tr>
              <td colspan="4" align="center"  valign="top"><input type="submit" name="Submit3" value="ค้นหาข้อมูล" /></td>
            <tr>
              <td height="17" colspan="4" align="center"  valign="top"></td>
          </table>




######################################################################

###########################ตัวรับคับ######################################

ข้อมูลที่เลือกมีน่ะ แต่ว่า ไปเช็คเงื่อนไขนี่สิ ยากจริง ๆ ยังไงช่วยแนะนำผมด้วยเถอะครับ ในฐานข้อมูลจะเก็บค่าที่มีเป็น 1 อยู่แล้วคับ
ถ้าเลือกอันเดียวจะได้ตามเงื่อไขน่ะคับ แต่ถ้าเลือก 2 อันขึ้นไป จบเห่เลยคับ แสดงทั้งหมด ไม่ตรงตามที่เขาได้เลือก

#####################################################################
$jod_rod=$_REQUEST[jod_rod];
      $furniture=$_REQUEST[furniture];  
     $tel=$_REQUEST[tel];
     $air=$_REQUEST[air];
     $pad_lom=$_REQUEST[pad_lom];
     $ubc=$_REQUEST[ubc];
     $internet=$_REQUEST[internet];
     $swim=$_REQUEST[swim];
     $police=$_REQUEST[police];
     $key_card=$_REQUEST[key_card];
     $tv=$_REQUEST[tv];
     $yod_ren=$_REQUEST[yod_ren];
     $sak_pa=$_REQUEST[sak_pa];
     $vong_jorn_pid=$_REQUEST[vong_jorn_pid];
     $fitnet=$_REQUEST[fitnet];
     $minimart=$_REQUEST[minimart];  
 
 if($jod_rod !="")
                 {
                print "jod_rod &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
 if($furniture !="")
                 {
                print "furniture &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($tel !="")
                 {
                print "tel &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($air !="")
                 {
                print "air &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($pad_lom !="")
                 {
                print "pad_lom &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($ubc !="")
                 {
                print "ubc &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($internet !="")
                 {
                print "internet &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($swim !="")
                 {
                print "swim &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($police !="")
                 {
                print "police &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($key_card !="")
                 {
                print "key_card &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }                                                
if($tv !="")
                 {
                print "tv &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($yod_ren !="")
                 {
                print "yod_ren &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($sak_pa !="")
                 {
                print "sak_pa &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($vong_jorn_pid !="")
                 {
                print "vong_jorn_pid &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }
if($fitnet !="")
                 {
                print "fitnet &nbsp;";
                }
                else
                {
                print  "null &nbsp; &nbsp;";
                }
if($minimart!="")
                 {
                print "minimart &nbsp;";
                }
                else
                {
                print  "null &nbsp;";
                }                   
/* ปัญหาอยู่ตรงนี้คัรบแก้ไม่ตกเลย---->*/ $condition=" WHERE (jod_rod LIKE '$jod_rod') or (furniture LIKE '$furniture') or (tel LIKE '$tel') or (air LIKE '$air') or (pad_lom LIKE '$pad_lom') or (ubc LIKE '$ubc') or (internet LIKE '$internet') or (swim LIKE '$swim') or (police LIKE '$police') or (key_card LIKE '$key_card') or (tv LIKE '$tv') or (yod_ren LIKE '$yod_ren') or (sak_pa LIKE '$sak_pa') or (vong_jorn_pid LIKE '$vong_jorn_pid') or (fitnet LIKE '$fitnet') or (minimart LIKE '$minimart')";

$sql = "SELECT * FROM ads". $condition;
#3

ช่วยดุโค๊ดผมหน่อยเถอะครับช่วยผมด้วย
#4

if($jod_rod ==1){$condition1=" jod_rod LIKE '$jod_rod'";}else{$condition1="";}
if($furniture ==1){$condition2=" and furniture LIKE '$furniture'";}else{$condition2="";}
if($tel ==1){$condition3=" and tel LIKE '$tel'";}else{$condition3="";}
if($air ==1){$condition4=" and air LIKE '$air'";}else{$condition4="";}
if($pad_lom ==1){$condition5=" and pad_lom LIKE '$pad_lom'";}else{$condition5="";}
if($ubc ==1){$condition6=" and ubc LIKE '$ubc'";}else{$condition6="";}
if($internet ==1){$condition7=" and internet LIKE '$internet'";}else{$condition7="";}
if($swim ==1){$condition8=" and swim LIKE '$swim'";}else{$condition8="";}
if($swim ==1){$condition8=" and swim LIKE '$swim'";}else{$condition8="";}
if($police ==1){$condition9=" and police LIKE '$police'";}else{$condition9="";}
if($key_card ==1){$condition10=" and key_card LIKE '$key_card'";}else{$condition10="";}
if($tv ==1){$condition11=" and tv LIKE '$tv'";}else{$condition11="";}
if($yod_ren ==1){$condition12=" and yod_ren LIKE '$yod_ren'";}else{$condition12="";}
if($sak_pa ==1){$condition13=" and sak_pa LIKE '$sak_pa'";}else{$condition13="";}
if($vong_jorn_pid ==1){$condition14=" and vong_jorn_pid LIKE '$vong_jorn_pid'";}else{$condition14="";}
if($fitnet ==1){$condition15=" and fitnet LIKE '$fitnet'";}else{$condition15="";}
if($minimart ==1){$condition16=" and minimart LIKE '$minimart'";}else{$condition16="";}

   
$condition="$condition1". "$condition2". "$condition3". "$condition4". "$condition5". "$condition6". "$condition7". "$condition8". "$condition9". "$condition10". "$condition11". "$condition12". "$condition13". "$condition14". "$condition15". "$condition16";
           
                                   
print $sql = "SELECT * FROM ads where ". $condition;  


ผมติดปัญหาอีกแล้วครับ เมื่อผมสร้างเงื่อนไขแบบนี้น่ะครับ ถ้าได้เลือก conditon1 ด้วยแล้วการแสดงข้อมูลจะแสดงได้ตามเงื่อนไขเลยครับ แต่ถ้าเิกิดว่า ไม่ได้เลือก condition1 น่ะครับ sql ที่ออกมาจะเป็นประมาณว่า-----> SELECT * FROM ads where and air LIKE '1' ทำให้ error น่ะครับ ผมควรจะใช้เงื่อนไข and ไว้ตรงไหนดีคับ ถึงจะไม่ error ครับ
#5

ได้แล้วครับ if($jod_rod ==1){$condition1=" jod_rod LIKE '$jod_rod'";}else{$condition1="jod_rod";}ใสค่าลงไปก้สามารถหาผลลัพธ์ได้แล้วครับ sql ก้จะได้ว่า----> SELECT * FROM ads where jod_rod and air LIKE '1'



บางคนอาจรู้แล้ว....ก้ขออภัญมาณะที่นี้ด้วยน่ะครับ...ทำได้ก้เลยดีใจมากไปหน่อย
#6

ยินดีด้วย ถึงแม้ว่ามันจะไม่ค่อยถูกต้องนัก แต่ในเมื่อเข้าใจหลักการแล้ว ผมจะอธิบายวิธีที่ดีกว่าเพิ่มเติมละกัน

<?php
if($jod_rod == 1){
  $sqls[]="jod_rod LIKE '$jod_rod' ";
}
if($furniture == 1){
  $sqls[]="furniture LIKE '$furniture' ";
}
$sql = "SELECT * FROM ads WHERE ".implode(' OR ', $sqls);
?>

โค้ดด้านบนจะดูเข้าใจง่ายกว่า อยากรู้ว่าผลลัพท์เป็นยังไง ก็ echo $sql ออกมาดูเอาละกัน
#7

  ครับขอบคุณมากครับ  
#8
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^