PHP Filter (Multiple Dropdown list)

Go To StackoverFlow.com

1

I have look around the internet and even on here, but with my lack of php knowledge I am finding it hard to get it working. I feel I am close though (well I think).

I am trying to make a filter page, where people can filter the mobile phone brand, along with for example the minutes the package comes with (currently, just two features but once I have this working I plan to add more).

Thanks in advance!

Okay, enough of the details - here is my code

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

        }
?>
2012-04-05 19:26
by Bushell Consultancy
Because there are two occurrences of <SELECT NAME=Model> - hjpotter92 2012-04-05 19:31
It's always good to have a second set of eyes look at your work - thanks haha : - Bushell Consultancy 2012-04-05 19:45
That fixed part of the problem but the second box doesn't seem to be used when searching - Bushell Consultancy 2012-04-06 10:10


0

Use a different approach. First get the mobile brands in first select. And minutes in second select box. Then use a button which will fetch both select box values to php function where you can query the result. Thats all.

2012-04-06 12:15
by Muhammad Raheel
Ads