Sum of column in Generated Table
I'm trying to get sum of data in one colum in Generated HTML table.
The table is getting yt_id from MySQL and use JSON to get data from YT Api
(the the table is autogenerated)
Here is my code:
<html>
<?php
include_once('db.php');
$result = mysql_query("select * from yt_data");
echo "<table id='countit' border='1'>
<tr>
<th>ID</th>
<th>Views</th>
<th>Likes</th>
</tr>";
while($r=mysql_fetch_array($result))
{
$yt_id=$r["yt_id"];
$video_ID = $yt_id;
$JSON =
file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
$likes = $JSON_Data->{'entry'}->{'yt$rating'}->{'numLikes'};
echo "<tr class='countit'>";
echo "<td>" . $yt_id . "</td>";
echo "<td class='count-me'>" . $views . "</td>";
echo "<td class='count-me'>" . $likes . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</html>
I have tried to get sum using this code but I'm getting just NaN:
<script language="javascript" type="text/javascript">
var tds = document.getElementById('countit').getElementsByTagName('td');
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds[i].className == 'count-me') {
sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
}
document.getElementById('countit').innerHTML += '<tr><td>' + sum +
'</td><td>total</td></tr>';
</script>
Any idea how to make this to work?
No comments:
Post a Comment