开发者

How to fill a table with PHP & JSON?

In CodeIgniter, I'm trying to create a table which fills with data from the Facebook Graph API.

The JSON loads a controller which passes data to a view, and it is this view which is added to a pre-existing table.

My PHP view looks like this:

if (array_key_exists('is_community_page', $json)==FALSE){
    echo '<tr>';
    echo '<td><a href="http://graph.facebook.com/' . $json['id'] . '">ID</a></td>';
    echo '<td><a href="' . $json['link'] . '">'. $json['name'] .'</td>';

    if (!empty($json['website'])) {
        if (!preg_match("~^(?:f|ht)tps?://~i", $json['website'])) {
           $json['website'] = "http://" . $json['website'];
        }
        echo '<td><a href="' . $json['website'] . '">' . $json['website'] . '</a></td>';
        }
    else {
        echo '<td>N/A&开发者_StackOverflowlt;/td>';
        }

    if (!empty($json['likes'])) {
        echo '<td class="num">' . number_format($json['likes']) . '</td>';
        }
    if (!empty($json['checkins'])) {
        echo '<td class="num">' . number_format($json['checkins']) . '</td>';
        }
    echo '</tr>';
    }

And the jQuery/JSON looks like this:

$.ajax({
    url: "<?php echo site_url('controller/function'); ?>",
    type: 'POST',
    data: form_data,
    success: function(data) {
        $('#results_table').html(data);
    }
});

But when the data is returned, it just inserts the <a> elements between the <table> tags, and none of the <td> or <tr>.

Can anyone see why it might be ignoring the table row and table data tags, yet still keeping anchor tags and all the desired content?


Have you tried print_r($json) before the if to see if you're getting any output whatsoever from the Facebook data?


As per the comments, adding <tbody> to the table seemed to fix this adequately.


Perhaps try appending markup to a string and return the final value?

$row_data = '';
if (array_key_exists('is_community_page', $json)==FALSE){

    $row_data .= '<tr>';
    $row_data .= '<td><a href="http://graph.facebook.com/' . $json['id'] . '">ID</a></td>';
    $row_data .= '<td><a href="' . $json['link'] . '">'. $json['name'] .'</td>';

    if (!empty($json['website'])) {

        if (!preg_match("~^(?:f|ht)tps?://~i", $json['website'])) {
           $json['website'] = "http://" . $json['website'];
        }

        $row_data .= '<td><a href="' . $json['website'] . '">' . $json['website'] . '</a></td>';

        } else {
            $row_data .= '<td>N/A</td>';
        }

    if (!empty($json['likes'])) {
        $row_data .= '<td class="num">' . number_format($json['likes']) . '</td>';
    }

    if (!empty($json['checkins'])) {
        $row_data .= '<td class="num">' . number_format($json['checkins']) . '</td>';
    }

    $row_data .= '</tr>';

    return $row_data;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜