drfals1307 2015-02-25 18:23
浏览 80
已采纳

PHP MySQL数据库多重过滤

So I have a table with a whole load of properties and have displayed them on a webpage. As we have a wide range I developed a filtration system. However when used you can only select one tag, but we want to devise it in a way so that you can select one tag and then another. My current code is as follows.

<h3>Advanced Search:</h3>

    <table>
<tr>
    <td colspan="2" style="vertical-align: top; width:50%; !important;">
          <h3>Location</h3>
          <div>
            <a href="webpropertyloc.php?loc=Alsancak" data-ajax="false" class="ui-btn">Alsancak</a>
            <a href="webpropertyloc.php?loc=Karsiyaka" data-ajax="false" class="ui-btn">Karsiyaka</a>
            <a href="webpropertyloc.php?loc=Lapta" data-ajax="false" class="ui-btn">Lapta</a>
            <a href="webpropertyloc.php?loc=Kayalar" data-ajax="false" class="ui-btn">Kayalar</a>
            <a href="webpropertyloc.php?loc=Sadrazamkoy" data-ajax="false" class="ui-btn">Sadrazamkoy</a>
            <a href="webpropertyloc.php?loc=Camlibel" data-ajax="false" class="ui-btn">Cambeli</a>
            <a href="webpropertyloc.php?loc=Baspinar" data-ajax="false" class="ui-btn">Baspinar</a>
            <!--<a href="property.php?loc=" data-ajax="false" class="ui-btn"></a>
            <a href="property.php?loc=" data-ajax="false" class="ui-btn"></a>
            <a href="property.php?loc=" data-ajax="false" class="ui-btn"></a>-->
          </div>
          </td>


    <td style="vertical-align: top; width:25%;">      
          <h3>Number of Bedrooms</h3>
          <div>
            <a href="webpropertybed.php?bed=1" data-ajax="false" class="ui-btn">1</a>
            <a href="webpropertybed.php?bed=2" data-ajax="false" class="ui-btn">2</a>
            <a href="webpropertybed.php?bed=3" data-ajax="false" class="ui-btn">3</a>
            <a href="webpropertybed.php?bed=4" data-ajax="false" class="ui-btn">4</a>
          </div>
    </td>

And it goes on with another few options the code then on the page is as follows:

    <h1>Properties</h1>

    <?php
    $loc = $_GET["loc"];
    $con = mysql_connect("localhost","Username","Password");
    mysql_selectdb("db",$con);
    $sql = "SELECT * FROM Properties WHERE Location='$loc' ORDER BY  `Properties`.`Price` ASC";
    $mydata = mysql_query($sql,$con);

    echo "<h4>Location:" . $loc . "</h4><br>";

    while($record = mysql_fetch_array($mydata)){
        echo  "<div id=\"property\">" . "<img src=\"images/" . $record['MainPic'] . "\" id=\"propimg\" align=\"left\">" . "<div id=\"text\" style=\"text-align: center; margin-right:20px;\"><h2>" . $record['Title'] . "</h2>" . $record['Ref'] . "<br/>" . "&pound;" . $record['Price'] . "<br/>" . $record['Location'] . "<br/>" . "<a href=\"property_view.php?id=" . $record['ID'] . "\" data-ajax=\"false\" class=\"ui-btn\" id=\"button\">Details</a></div>" . "<img src=\"images/bottom-line.png\" id=\"bottomline\"></div>" . "</div>" ;

    }

    mysql_close($con);
    ?>

Any help would be greatly received. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongnaopa6200 2015-02-25 19:28
    关注

    I would suggest using the same page for displaying the results, whether the filter applies to the location or the number of bedrooms. You can always use WHERE (1 = 1) in your SQL so that you can append AND (Location='$loc') or AND (Beds='$bed'), though it would be preferable to use parameters.

    Once you have that set up, you can modify your links. Let's say your destination page is webproperty.php. Store that URL in a variable $urlDest.

    When you generate your links for filtering by location, add any variables in $_GET that aren't loc to the end of $urlDest, getting something like webproperty.php?bed=2. As you iterate through your locations, append the loc variable to $urlDest, like "<a href=\"$urlDest&amp;loc=$loc\">".

    Similarly, when you generate your links for filtering by bedroom count, add any variables in $_GET that aren't bed to the end of $urlDest, getting something like webproperty.php?loc=Alsancak. As you iterate through your bedroom counts, append the bed variable to $urlDest, like "<a href=\"$urlDest&amp;bed=$bed\">".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?