【问题标题】:How to split data while loop data in php and pass to ajax html with different id's如何在php中循环数据时拆分数据并传递给具有不同ID的ajax html
【发布时间】:2017-01-31 16:44:40
【问题描述】:

我有医生的数据库表,其中列出了近 100 多个医生,这是我的医生 PHP 代码 dc.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 条记录。

如何通过简化来解决?

【问题讨论】:

    标签: php jquery ajax performance


    【解决方案1】:

    你试过了吗

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

    然后在 AJAX 代码中不要拆分,让 json 格式帮助你。通过设置数据类型为jsondataType: "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
                }
            });
        });
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 2017-11-14
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      相关资源
      最近更新 更多