help with usort cmparison
hi everyone!
it's getting cold here & can't seem wrap frozen brain cells around problem.
below code parse youtube feed works fine.
i need sort entries $pubdate , can't make work.
<?php
//empty variable end
$html = ' ';
//url of playlist feed
$feed = "http://www.youtube.com/feeds/videos.xml?playlist_id=plc02cfde5690e4010";
//load feed xml
$xml = simplexml_load_file($feed);
//display 6 entries
for($i = 0; $i < 6; $i++) {
//node namespace variables
$published = $xml->entry[$i]->published;
//optional, shorten date
$shortdate = date("m/d/y", strtotime($published));
$title = $xml->entry[$i]->title;
$id = $xml->entry[$i]->id;
//strip unwanted characters id
$id = str_replace ("yt:video:", "", $id);
$author = $xml->entry[$i]->author->name;
$uri = $xml->entry[$i]->author->uri;
//put node variables html tags & embedded youtube player.
$html .= "<div class='col-sm-6 col-md-4'>
<h4><a href='$uri'>$title</a></h4>
<iframe src='http://www.youtube.com/embed/$id' allowfullscreen>
</iframe><br>
<small>published: $shortdate by: $author</small>
</div><hr>";
}
//output html code
echo $html;
?>
parsed feed results: http://alt-web.com/demos/my_feed.php
any & suggestions appreciated.
thanks,
nancy o.
not quite sure you're trying do. xml feed has videos in descending date order. want them in ascending order? if so, use splheap class this:
<?php $feed = simplexml_load_file('https://www.youtube.com/feeds/videos.xml?playlist_id=plc02cfde5690e4010'); class sortvideos extends splheap { protected function compare($val1, $val2) { $val1 = (string) $val1->published; $val2 = (string) $val2->published; if ($val1 == $val2) { return 0; } elseif ($val1 > $val2) { return -1; } else { return 1; } } } $videos = new sortvideos(); foreach ($feed->entry $video) { $videos->insert($video); } foreach ($videos $video) { echo $video->published . '<br>'; }
inside second foreach loop, can access each entry element $video.
More discussions in Coding Corner
adobe
Comments
Post a Comment