PHP过滤器(多个下拉列表)

时间:2012-04-05 19:26:09

标签: php mysql search filter

我环顾互联网,甚至在这里,但由于我缺乏PHP知识,我发现很难让它工作。我觉得我很接近(我认为)。

我正在尝试制作一个过滤页面,人们可以过滤手机品牌,例如包装附带的分钟数(目前只有两个功能,但一旦我开始工作,我计划添加更多功能)。

提前致谢!

好的,足够的细节 - 这是我的代码

filter.php:

    <?php
        include('db.php');  // include your code to connect to DB.
        $tbl_name="mobile";     //your table name

        $sql="SELECT DISTINCT model FROM $tbl_name ORDER BY model ASC"; 
        $result=mysql_query($sql); 
            $sql1="SELECT DISTINCT minutes FROM $tbl_name ORDER BY model ASC"; 
    $result1=mysql_query($sql1); 

    $model_o=""; 

    while ($row=mysql_fetch_array($result)) { 

        $model=$row["model"]; 
        $model_o.="<OPTION VALUE=\"$model\">".$model; 

    } 

    $minutes_o=""; 

    while ($row=mysql_fetch_array($result1)) { 

         $minutes=$row["minutes"]; 
        $minutes_o.="<OPTION VALUE=\"$minutes\">".$minutes;
    } 

    ?>
    <form action="result.php" method="post">
    <SELECT NAME=Model> 
    <OPTION VALUE=0>Choose 
    <?=$model_o?> 
    </SELECT> 
    <SELECT NAME=Model> 
    <OPTION VALUE=0>Choose 
    <?=$minutes_o?> 
    </SELECT> 
    <input type="submit" value="search phones" />
    </form>

result.php

<?php
    include('db.php');  // include your code to connect to DB.
    $tbl_name="mobile";     //your table name

$whereClauses = array(); 
if (! empty($_POST['Model'])) $whereClauses[] ="model='".mysql_real_escape_string($_POST['Model'])."'"; 
if (! empty($_POST['minutes'])) $whereClauses[] ="minutes='".mysql_real_escape_string($_POST['minutes'])."'"; 
$where = ''; 
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } 
$sql = mysql_query("SELECT * FROM $tbl_name".$where); 

        while($row = mysql_fetch_array( $sql )) 
        {
            echo "<tr>
                  <td><img src='".$row['image_url']."' alt='some_text'/></br>".$row['model']."</td>
                  <td>".$row['tariff']."</td>
                  <td>".$row['minimumcontractterm']."</td>
                  <td>".$row['minutes']."</td>
                  <td>".$row['texts']."</td>
                  <td>".$row['linerental']."</td>
                  <td>£".$row['dealcost']."</td>
                  <td>".$row['free_gift']."</td>
                  <td><button type='submit' class='green' onClick=parent.location='test/deal.php?id='><span>View</span></button></td>
    </tr>";


        // Your while loop here

        }
?>

1 个答案:

答案 0 :(得分:0)

使用不同的方法。首先选择移动品牌。第二个选择框中的分钟数。 然后使用一个按钮,它将两个选择框值都提取到php函数,您可以在其中查询结果。多数民众赞成。