简体   繁体   中英

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. 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. 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.

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:

<?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>"; . I don't see where $cns comes from. I believe it should be: $dpt="<div> ".$depart_list." </div>"; This removes the duplication of departments in $dpt .

You need to write $dpt = ""; just before 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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