如何循环所有json数据并插入到我的HTML中?

时间:2015-07-11 04:42:19

标签: php jquery json

我想将Json数据插入到我的file.php中。因此,每组都插入到div组中。 (风格相同等)。

我有一种感觉,我使用的方式并不聪明......更好的解决方案非常受欢迎。谢谢你们。

    $.ajax({

    url: feedURL,
    jsonpCallback: 'jsonpCallback',
    contentType:"application/json",
    dataType: 'jsonp',
    success: function(json) {

        for (var i=0; i < 15; i++) {

        var date = new Date(json.posts.data[i].created_time);
        var months = ["/01/", "/02/", "/03/", "/04/", "/05/", "/06/", "/07/",
     "/08/", "/09/", "/10/", "/11/", "/12/"];



        $("#description").html(json.posts.data[i].message);
        $("#caption").html(json.posts.data[i].caption); 
        $("#expire_date").html(date.getDate() + months[date.getMonth()] + date.getFullYear()); 
        $("#fb_link").html(
            '<a href="' + json.posts.data[i].link + '">'
            + 'Total Like: ' + json.posts.data[i].likes.summary.total_count
            + ' Total Share: ' + json.posts.data[i].shares.count
            + ' View on Facebook' + '</a>'
        ); 
        $("#profile_img").html('<img src="' + json.picture.data.url + '">');
        $("#profile_name").html(json.name);
        $("#full_fb_photo").html('<img src="' + json.posts.data[i].full_picture + '">');


        }

    },
    error: function(e) {
        console.log(e.message);
    }
});

2 个答案:

答案 0 :(得分:0)

您可以流畅地编写代码。在循环中打印不可计数的日期。如果我知道你的数据格式,我会给你一个确切的方法。

试试这段代码。

$folders = glob("*", GLOB_ONLYDIR);
usort($folders, function ($a, $b) {
return filemtime($b) - filemtime($a);
});

$record_count  = 20;
$total_pages   = ceil(count($folders)/$record_count);
$page          = $_REQUEST['page']; ///make it dyanamic :: page num
$offset        = ($page-1)*$record_count;
$folders_filter  = array_slice($folders, $offset,$record_count);

foreach ($folders_filter as $folder) {
echo "$folder <br/>";
}

if($total_pages > 1){
   if($page != 1){
      echo '<a href="thispage.php?page='.($page-1).'">Prev</a>';
   }
   if($page != $total_pages){
      echo '<a href="thispage.php?page='.($page+1).'">Next</a>';
   }
}

答案 1 :(得分:0)

谢谢你们,我已经想出来了。

curl https://api.wheniwork.com/2/shifts/?location_id=1&start=2014-03-05 \
-H "W-Token: ilovemyboss"