简体   繁体   English

如何在php中循环数据时分割数据并传递给具有不同id的ajax html

[英]How to split data while loop data in php and pass to ajax html with different id's

I have db table of doctors nearly more than 100 doctors list in it, Here is my PHP code for doctors dc.php : 我有db表的医生近100多名医生列表,这是我的医生dc.php的 PHP代码

    $conn= mysqli_connect('localhost', 'root', '', 'test');
    $query = mysqli_query($conn, "select * from doctors");
    while($result=mysqli_fetch_array($query)){
    $dc_name = $result['name'];
    $dc_img = $result['image'];
    $dc_email = $result['email'];


    echo $dc_name."||".$dc_img."||".$dc_email;
    }

    I want echo all doctors another main page index which already fixed large layout with bootstrap,html,css by using ajax calling on click

    Here is my code for ajax

    $(function(){
    $(".divscrolla").on("click", function(){
    $.ajax({
     url:'db.php',
    type:'POST',
    data:{"data":$(this).val()},
    success:function(data){
    var splitted = data.split("||");
    $("#result_name").html.splitted[0];
    $("#result_img").html.splitted[1];
    $("#result_email").html.splitted[2];
    }
    });
    });

Here I want display all 100 records by AJAX 在这里,我希望通过AJAX显示所有100条记录

But here I am getting only first element only, but I have more 100 records in database. 但在这里我只获得第一个元素,但我在数据库中有更多的100条记录。

How can I solve by simplifying this? 如何通过简化来解决这个问题?

Did you try 你试过了吗

$resultArray = $result->fetch_all(MYSQLI_ASSOC);
$jsonstring = json_encode($resultArray);
echo $jsonstring;

And then in the AJAX code don't split, let the json format help you. 然后在AJAX代码中不拆分,让json格式帮助你。 By setting the data type to json dataType: "json", 通过将数据类型设置为json dataType: "json",

$(function(){
    $(".divscrolla").on("click", function(){
        $.ajax({
            dataType : "json",
            url:'db.php',
            type:'POST',
            data:{"data":$(this).val()},
            success:function(data){
                //consume the data in a assoc array manner
                //data[0]['name'] for first entry's name
            }
        });
    });
 }

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

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