我已经浏览了互联网,甚至在这里,但是由于我缺乏 php 知识,我发现很难让它工作。我觉得我很接近(我认为)。
我正在尝试制作一个过滤页面,人们可以在其中过滤手机品牌,例如包装随附的分钟数(目前,只有两个功能,但一旦我有了这个工作,我计划添加更多)。
提前致谢!
好的,足够的细节 - 这是我的代码
过滤器.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>
结果.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
}
?>