dongrouli2667 2016-09-21 23:24
浏览 14
已采纳

使用AJAX每秒显示一个新的DIV foreach数组

I'm trying to create a new div every second with a prefilled data from the array list.

fields.php:

<?php

$file = fopen('names.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
  //$line is an array of the csv elements
    $names = $line;
    echo $names[array_rand($names)];
}
fclose($file);

?>

JS:

<script>
function createName(){
    var feedback = $.ajax({
        type: "POST",
        url: "fields.php",
        async: false
    }).complete(function(){
        setTimeout(function(){get_fb_complete();}, 1000);
    }).responseText;

    $('.content').html(feedback);
}

$(function(){
    createName();
});
</script>

And the Div I want to be created with the data from the arrays:

    <div class="content">

    <div class="col-1">

    <div class="profile-bg">
    <div class="inside-profile">
    <img src="img/avatars/avatar1.jpg" class="img-circle" border="0"></img>
    </div>
    </div>

    </div>

    <div class="col-2">
        <div class="details">
    <p style="font-size: 18px;"> ==*I want to show an ARRAY here.== </p>
    <p class="details-desc"> ==*(TIMESTAMP, something like: 1m to show when this div was created)== </p>
    </div>
    </div>

    <div class="col-3">
    <img src="img/userImg/userImg1.jpg" class="viewed-img" border="0"></img>
    </div>
</div>
<div class="col-4">
<div class="seperator"></div>
</div>

</div>

How to transform each ARRAY created in fields.php into a .content DIV?

  • 写回答

2条回答 默认 最新

  • dongluo8439 2016-09-22 02:15
    关注

    First you have to print the value in a JSON format from the php side

    <?php
    
    $file = fopen('names.csv', 'r');
    $data = [];
    while (($line = fgetcsv($file)) !== FALSE) {
      //$line is an array of the csv elements
        $names = $line;
        $data[] = $names[array_rand($names)];
    }
    fclose($file);
    echo json_encode($data);
    
    ?>
    

    assuming values was a,b,c it will print ["a","b","c"]

    at the js we will parse this values and iterate over it

    <script>
    function createName(){
        var feedback = $.ajax({
            type: "POST",
            url: "fields.php",
            async: false,
            success: function(data)
            {
                // parse json 
                data = JSON.parse(data);
                // add them in divs
                for ( var i = 0 ; i < data.length ; i++ )
                {
                    // create the required format - you can do the same with createElement and assign class also
                    var item_code = "<div class='content'>"+data[i]+"</div>";
                    $("#container_p").append(item_code);
                }
            }
        }).complete(function(){
            setTimeout(function(){get_fb_complete();}, 1000);
        }).responseText;
    
        $('.content').html(feedback);
    }
    
    $(function(){
        createName();
    });
    </script>
    

    Last thing is the HTML part just add id for the container p

        <div class="content">
    
        <div class="col-1">
    
        <div class="profile-bg">
        <div class="inside-profile">
        <img src="img/avatars/avatar1.jpg" class="img-circle" border="0"></img>
        </div>
        </div>
    
        </div>
    
        <div class="col-2">
            <div class="details">
        <p style="font-size: 18px;" id="container_p"></p>
        <p class="details-desc"> ==*(TIMESTAMP, something like: 1m to show when this div was created)== </p>
        </div>
        </div>
    
        <div class="col-3">
        <img src="img/userImg/userImg1.jpg" class="viewed-img" border="0"></img>
        </div>
    </div>
    <div class="col-4">
    <div class="seperator"></div>
    </div>
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?