I am using Gdata API to get Youtube video and comments. The reply is in XML which contains an array inside it.
For video ID and comments the XML response are different. For example I am getting array ID as video ID array and for one ID one or many comments in array.
Array of both video ID and comments is as follow:
foreach ($array as $entry)
{
$videoid = basename($entry);
$video[] = $videoid;
$logger->info('Response From Youtube:videoid=>' . $videoid);
}
$this->view->videoid = $video;
$author = array();
$content = array();
$arraycnt = array();
foreach ($video as $id)
{
$comment = "http://gdata.youtube.com/feeds/api/videos/".$id."/comments";
$sxml1 = simplexml_load_file($comment);
$entries = $sxml1->entry;
foreach ($entries as $a)
{
$author[] = $a->author->name;
$content[] = $a->content;
}
}
And the particular view as follow:
<table>
<tr>
<td>
<?php
for($i=0;$i<$length;$i++)
{
?>
<embed
width="420" height="345"
src="http://www.youtube.com/v/<?php echo $videoid[$i];?>"
type="application/x-shockwave-flash">
</embed>
<?php
}
?>
</td>
<td>
<?php
foreach($content as $cont)
{
?>
<p>Comment:<?php echo $cont;?></p>
<?php
}
?>
</td>
<td>
<?php
foreach($author as $auth)
{
?>
<p>Commented By:<?php echo $auth;?></p>
<?php
}
?>
</td>
</tr>
</table>
How can I show the video and comments in the view like:
videoA1 commentA1 commentA2
videoB1 commentB1 commentB2 commentB3
You can keep the id of the video in $author
and $content
arrays.
foreach ($video as $id)
{
foreach ($entries as $a)
{
$author[$id][] = $a->author->name;
$content[$id][] = $a->content;
}
}
So you can get comment's authors from specific video id:
foreach ($video as $id)
{
echo $id;
foreach($author[$id] as $auth) {
echo ' ', $auth;
}
}
The same goes with comment's content.
Of course you can extend this solution if you want to have just one array for comment's authors and content, but the logic stays the same.