請問關於資料庫的清單問題

edited 十月 2013 in PHP新手區
各位前輩好!!
最近我位一個房屋仲介做了一個資料庫的案子,
目前已經用dreamweaver完成了.

不過最近業主要求我將
原資料篩選清單多加了一不限的條件:

(東區,南區,中西區,.....)→(東區,南區,中西區.....,不限地區)

(100以下,100~200,....)→(100以下,100~200,.....,不限金額)
 
  ......

我試了很久都失敗了!!

請問有經驗的前輩朋友,能否告訴我該怎麼改呢?


原篩選清單的選項如下

<table width="254" height="80" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="127" align="right"><span class="style1">地區:</span></td>
<td width="127"><select name="AreaID" id="AreaID">
<option value="東區" <?php if (!(strcmp("東區", $row_Recordset3))) {echo "SELECTED";} ?>>東區</option>
<option value="南區" <?php if (!(strcmp("南區", $row_Recordset3))) {echo "SELECTED";} ?>>南區</option>
<option value="中西區" <?php if (!(strcmp("中西區", $row_Recordset3))) {echo "SELECTED";} ?>>中西區</option>
<option value="北區" <?php if (!(strcmp("北區", $row_Recordset3))) {echo "SELECTED";} ?>>北區</option>
<option value="安平區" <?php if (!(strcmp("安平區", $row_Recordset3))) {echo "SELECTED";} ?>>安平區</option>
<option value="安南區" <?php if (!(strcmp("安南區", $row_Recordset3))) {echo "SELECTED";} ?>>安南區</option>
<option value="永康市" <?php if (!(strcmp("永康市", $row_Recordset3))) {echo "SELECTED";} ?>>永康市</option>
<option value="其他" <?php if (!(strcmp("其他", $row_Recordset3))) {echo "SELECTED";} ?>>其他</option>
</select></td>
</tr>
<tr>
<td align="right"><span class="style1">價格:</span></td>
<td><select name="PriceID" id="PriceID">
<option value="100以下" <?php if (!(strcmp("100以下", $row_Recordset3))) {echo "SELECTED";} ?>>100以下</option>
<option value="100~200" <?php if (!(strcmp("100~200", $row_Recordset3))) {echo "SELECTED";} ?>>100~200</option>
<option value="200~300" <?php if (!(strcmp("200~300", $row_Recordset3))) {echo "SELECTED";} ?>>200~300</option>
<option value="300~400" <?php if (!(strcmp("300~400", $row_Recordset3))) {echo "SELECTED";} ?>>300~400</option>
<option value="400~500" <?php if (!(strcmp("400~500", $row_Recordset3))) {echo "SELECTED";} ?>>400~500</option>
<option value="500~1000" <?php if (!(strcmp("500~1000", $row_Recordset3))) {echo "SELECTED";} ?>>500~1000</option>
<option value="1000以上" <?php if (!(strcmp("1000以上", $row_Recordset3))) {echo "SELECTED";} ?>>1000以上</option>
</select></td>
</tr>
<tr>
<td align="right"><span class="style1">用途:</span></td>
<td><select name="wayID" id="wayID">
<option value="公寓" <?php if (!(strcmp("公寓", $row_Recordset3))) {echo "SELECTED";} ?>>公寓</option>
<option value="華廈" <?php if (!(strcmp("華廈", $row_Recordset3))) {echo "SELECTED";} ?>>華廈</option>
<option value="大樓" <?php if (!(strcmp("大樓", $row_Recordset3))) {echo "SELECTED";} ?>>大樓</option>
<option value="透天住家" <?php if (!(strcmp("透天住家", $row_Recordset3))) {echo "SELECTED";} ?>>透天住家</option>
<option value="透天店面" <?php if (!(strcmp("透天店面", $row_Recordset3))) {echo "SELECTED";} ?>>透天店面</option>
<option value="店面" <?php if (!(strcmp("店面", $row_Recordset3))) {echo "SELECTED";} ?>>店面</option>
<option value="其他" <?php if (!(strcmp("其他", $row_Recordset3))) {echo "SELECTED";} ?>>其他</option>
</select>
<input type="submit" name="Submit" value="送出"></td>
</tr>
</table>


結果頁面的篩選條件如下:

//mysql_select_db($database_snehouse, $snehouse);
//$query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = '%s' And PriceID='%s' And wayID='%s'", $colname_Recordset5,$b_Recordset5,$c_Recordset5);
//$query_limit_Recordset5 = sprintf("%s LIMIT %d, %d", $query_Recordset5, $startRow_Recordset5, $maxRows_Recordset5);
//$Recordset5 = mysql_query($query_limit_Recordset5, $snehouse) or die(mysql_error());
//$row_Recordset5 = mysql_fetch_assoc($Recordset5);

謝謝您的幫忙!!

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=1357

評論

  • edited 三月 2006
    把 Where 條件拆開來,加入判斷式,如果選項不等於其他時才加入條件。
  • edited 三月 2006
    謝謝囉!!
    我再試試看!!
  • edited 三月 2006
    您好!!
    我照您的說明,使用條件判斷方式更改,
    可是卻只能作出第一層的篩選,
    能不能請您幫我看一下那裡弄錯了?

    原先的篩選:
    mysql_select_db($database_snehouse, $snehouse);
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = '%s' And PriceID='%s' And wayID='%s'", $colname_Recordset5,$b_Recordset5,$c_Recordset5);

    改成:

    mysql_select_db($database_snehouse, $snehouse);
    if ($b_Recordset = "10" and $c_Recordset = "10" and $d_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s", $colname_Recordset5);
    } elseif ($b_Recordset = "10" and $c_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And wayID=%s", $colname_Recordset5,$d_Recordset5);
    } elseif ($b_Recordset = "10" and $d_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And spaceID=%s", $colname_Recordset5,$c_Recordset5);
    } elseif ($b_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And spaceID=%s And wayID=%s", $colname_Recordset5,$c_Recordset5,$d_Recordset5);
    } elseif ($c_Recordset = "10" and $d_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And priceID=%s", $colname_Recordset5,$b_Recordset5);
    } elseif ($c_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And priceID=%s And wayID=%s", $colname_Recordset5,$b_Recordset5,$d_Recordset5);
    } elseif ($d_Recordset = "10") {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And priceID=%s And spaceID=%s", $colname_Recordset5,$b_Recordset5,$c_Recordset5);
    } else {
    $query_Recordset5 = sprintf("SELECT * FROM `object` WHERE AreaID = %s And priceID=%s And spaceID=%s And wayID=%s", $colname_Recordset5,$b_Recordset5,$c_Recordset5,$d_Recordset5);
    }
    $query_limit_Recordset5 = sprintf("%s LIMIT %d, %d", $query_Recordset5, $startRow_Recordset5, $maxRows_Recordset5);
    $Recordset5 = mysql_query($query_limit_Recordset5, $snehouse) or die(mysql_error());
    $row_Recordset5 = mysql_fetch_assoc($Recordset5);


    其中的不限地區或者不限價格,我的傳遞設定值為10

    感激不盡,謝謝!!
  • edited 三月 2006
    下面範例花些時間研究看看:
    $sql = 'SELECT * FROM `object` WHERE 1';
    if($b_Recordset != '10') {
      $sql .= ' AreaID LIKE ...';
    }
    if($c_Recordset != '10') {
      $sql .= ' PriceID LIKE ...';
    }
    
    ...
    
    $sql .= 'LIMIT ...';
    
    //start query ...
    
  • edited 三月 2006
    完成了!!
    這種方式蠻好用的
    剛開始看不懂,不過了解後發現可以運用在很多地方,
    謝謝!!
Sign In or Register to comment.