如何防止重复相同的数据在php&阿贾克斯

时间:2015-11-19 14:59:01

标签: php jquery mysql ajax

我已经构建了一个带下拉列表的搜索过滤器,用于过滤数据并选择下拉列表。但它反复显示相同的结果数据,并且不会使用匹配的查询正确显示数据。请查看演示http://speedymarket.byethost5.com/test_filter/atest.php

atest.php

<?php
    include_once 'dbcon.php';
?>
<!DOCTYPE html>
<html>
    <head>

        <title></title>
      <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
        <script src="js/global.js" type="text/javascript"></script>
    </head>
    <body>

         <?php
    $res = $MySQLiconn->query("SELECT title FROM store order by title");
    $count = $res->num_rows;
 ?>

           Title:   <select  name="title" id="title">
        <option value="">Select one</option>
                <?php

                    if($count > 0)
    {
        while($row=$res->fetch_array())
        {
                ?>

                <option  value="<?php echo $row['title']; ?>"> <?php echo $row['title']; ?> </option>
                <?php        }} ?>
                </select>

            <select  name="date" id="datee">

                <option value="cdate"> Current Date </option>
                <option value="pdate"> Past Deals </option>

                </select>
        <div id="title-data"></div>
      <div id="title-data2"></div>
    </body>
</html>

title.php

<?php
 $con = new mysqli("localhost","root","","filterdatabase");
 if (!$con)
 {
 die('Could not connect: ' . $con->connect_error);
 }
 if(isset($_POST['title']))
 {
  $title = mysql_real_escape_string($_POST['title']);
  $arrChartData[] = array();
 $sql = "SELECT * FROM store
 WHERE title LIKE '%".$title."%' group by storeid";
 $res = $con->query($sql) or trigger_error($con->error."[$sql]");
 while($row = $res->fetch_assoc())
 {
     $arrChartData[] = $row;
 echo $row['title'] . " " . $row['startdate'];
 echo "<br />";
 }
 }
 if(isset($_POST['date']) )
 {
     //$title =(isset(mysql_real_escape_string($_POST['title'])) ? $_POST['title'] : null);
     $title = mysql_real_escape_string($_POST['title']);
     $date= mysql_real_escape_string($_POST['date']);
     if($date=="cdate")
         {
              $arrChartData[] = array();
                 $sql="select DISTINCT  * from 
             store where  startdate > CURRENT_DATE() and title LIKE '%".$title."%' group by storeid";


             $res = $con->query($sql) or trigger_error($con->error."[$sql]");
             while($row = $res->fetch_assoc())
                 {
                     $arrChartData[] = $row;
                 echo $row['title'] . " " . $row['startdate'];
                 echo "<br />";
                 }
         }//
         elseif($date=="pdate")
         {

             $arrChartData[] = array();
                $sql="select DISTINCT  * from 
             store where  startdate < CURRENT_DATE() AND title LIKE '%".$title."%' group by storeid ";


             $res = $con->query($sql) or trigger_error($con->error."[$sql]");
         while($row = $res->fetch_assoc())
             {
                 $arrChartData[] = $row;
             echo $row['title'] . " " . $row['startdate'];
             echo "<br />";
             }
         }//

 }

 ?>

global.js

$(document).ready(function () {
    $('#title').on('change', function () {
        var title = $('#title').val();
        if ($.trim(title) != '') {

            $.ajax({
            url: "title.php",
            type: 'POST',
            dataType: 'json',
            data: {'title': title},
            dataType: 'json',
            cache: false,
            success: function(data){
                 $('#title-data').html(data);
            },
            error: function(jqxhr) {
               $('#title-data').html(jqxhr.responseText); 
            }
         }); 
        }
    })//


    $('#datee').on('change', function () {

        var date = $('#datee').val();
        var title = $('#title').val();
        if ($.trim(date) != '') {
         $.ajax({
            url: "title.php",
            type: 'POST',
            dataType: 'json',
            data: {'date': date, 'title': title},
            dataType: 'json',
            cache: false,
            success: function(data){
                 $('#title-data').html(data);
            },
            error: function(jqxhr) {
               $('#title-data').html(jqxhr.responseText); 
            }
         }); 
        }
    })//
})

请建议我解决问题。提前致谢编码器

0 个答案:

没有答案