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
}
?>
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.
<SELECT NAME=Model>
- hjpotter92 2012-04-05 19:31