簡體   English   中英

如何在php中循環數據時分割數據並傳遞給具有不同id的ajax html

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

我有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];
    }
    });
    });

在這里,我希望通過AJAX顯示所有100條記錄

但在這里我只獲得第一個元素,但我在數據庫中有更多的100條記錄。

如何通過簡化來解決這個問題?

你試過了嗎

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

然后在AJAX代碼中不拆分,讓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