Joe W Joe W - 1 year ago 97
jQuery Question

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</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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download