简体   繁体   English

如何通过ajax获取多个查询的结果

[英]how to get results of multiple queries through ajax

I have a input field which filter the data based upon the value of input field value. 我有一个输入字段,可根据输入字段值的值过滤数据。 I am using ajax call to filter the data at run time. 我正在使用ajax调用在运行时过滤数据。 But the problem is I am using multiple queries and foreach loop to fetch my desired results like I am getting data of a person: name, email, phone, departments. 但是问题是我正在使用多个查询和foreach循环来获取所需的结果,就像我正在获取一个人的数据一样:姓名,电子邮件,电话,部门。 A person is working on multiple departments so I am using foreach loop to fetch all his departments along with his name, email and phone. 一个人在多个部门工作,因此我正在使用foreach循环来获取其所有部门以及他的姓名,电子邮件和电话。

When I output my data,the detail of first person coming correct but in the 2nd person means in 2nd row department column also show the departments of first person and 3rd showing the 2nd person departments and so on. 当我输出数据时,第一人称详细信息正确但在第二人称中意味着第二行部门列中还显示了第一人称部门,而第三人称则显示了第二人称部门等。 how can i show the right detail of a person? 如何显示一个人的正确细节?

here is my code: 这是我的代码:

<script>
 $(document).ready(function() {

    $("#cmp_name").keyup(function(){
        $.ajax({
             'url':"filter.php",
            'data':{name:$("#cmp_name").val()},
            'method':'GET',
            'success':function(name){
                //alert(data);
                 $("#datas").html(name);            
            }
        })

  });
 });
</script>
<input type="text" placeholder="Name"  name="cmp_name"  id="cmp_name">

and here is my filter.php file: 这是我的filter.php文件:

<?php
if(isset($_GET['name'])){
 $name = $_GET['name'];
 $dpt = "";
 $ret = "";
 $query = mysql_query("select id,name,phone,country_id,departments from persons where name like '%$name%'");

 while($data = mysql_fetch_assoc($query)){
     $name = $data['name'];
     $phone = $data['phone'];
     $country_id = $data['country_id'];
     $queryr= mysql_query("select country from countries where country_id = $country_id'");
                        $$country = mysql_fetch_array($queryr);
                        $country_name = $country['country'];





     $depart = $data['departments'];
     $department=explode(",",$depart);
        foreach($department as $departments){

              $depart_fetch = mysql_query("select department_name from departments where id='$departments'");
                                        $fetch1 = mysql_fetch_array($depart_fetch);
                                        $depart_list = $fetch1['description'];
                                    $dpt=$dpt."<div> ".$depart_list." </div>";

                                }
                $ret=$ret."<tr><td> ".$name." </td><td> ".$phone." </td><td> ".$country_name." </td><td> ".$dpt."</td></tr>";
                }
    echo $ret;
}
?> 

This line seems to be wrong: $dpt=$dpt."<div> ".$cns." </div>"; 这行似乎是错误的: $dpt=$dpt."<div> ".$cns." </div>"; . I don't see where $cns comes from. 我看不到$cns来源。 I believe it should be: $dpt="<div> ".$depart_list." </div>"; 我相信应该是: $dpt="<div> ".$depart_list." </div>"; This removes the duplication of departments in $dpt . 这消除了$dpt中部门的重复。

You need to write $dpt = ""; 您需要写$dpt = ""; just before foreach($department as $departments){ . 就在foreach($department as $departments){ Otherwise it just keeps on adding to the list on every loop, instead of making a new list for each person. 否则,它只会在每个循环中不断添加到列表中,而不是为每个人创建新列表。

This will fix your immediate issue, however you should also strongly note the comments I made above regarding your out-of-date data access methods and your broken database design and aim to fix those in the near future, before putting your code into production use. 这将解决您的直接问题,但是,您还应该强烈注意我上面关于过时的数据访问方法和损坏的数据库设计所发表的意见,并打算在将代码投入生产使用之前在不久的将来修复这些问题。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM