Classic ASP Active Server Pages Examples, code, tutorials, scriptsParse RSS - XML Feed with PHP for Javascript Slideshow (Zenphoto)

CURL, PHP, RSS or XML, Javascript Slideshow example uses Zenphoto Gallery RSS feed. This can be adapted to almost any javascript slideshow. NO flash, flash free!

I really like Zenphoto! I wanted to use the RSS feed that is produced by zenphoto as the data backend for a slideshow using zenphoto images that could be shown elsewhere on the site or on another site entirely. I did NOT want to have to use flash/SWF at all. The slideshow script is in the white table below (scroll down).

Note about zenphoto: The scripts automatically update the database with the names of new folders. If you use FrontPage or ExpressionWeb, this means that eventually, you'll end up with albums that are titled "_vti_cnf." The immediate way to deal with these is to login to the admin area and UNpublish those albums so they will not be publicly linked. If you attempt to delete them, FrontPage will re-create them and zenphoto wll add them again, so it is better just to UNpublish them.

The slideshow script has code in it to filter out those folders, in case the remote site that hosts the RSS feed is modified with FrontPage. However, I have written a short little PHP script to dynamically make those folder names non-public/unpublished by updating the `show` columns of the undesired records in the database to 0. I don't delete those records because zenphoto would simply re-create them. This is not as elegant as it would be to write a zenphoto extension, but it is quick and easy. At the very bottom of the zenphoto index.php file, paste in:

<?php
$con = mysql_connect("DBHOST","USERID","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("zenphoto", $con);
mysql_query("UPDATE `zp_albums` SET `show` ='0' WHERE (INSTR( folder, '_vti_cnf' ) >0) AND `show`='1'");
$query = "SELECT `id` FROM `zp_albums` WHERE (INSTR( folder, '_vti_cnf' ) >0)";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$sRec = $row[0];
$rsImgUp="UPDATE `zp_images` SET `show` ='0' WHERE `albumid` = '".$sRec."' AND `show`='1'";
mysql_query($rsImgUp) or die ("Error in query: ".$rsImgUp);
//print "<br /><em>Updated!</em><br /><br />";
}
mysql_close($con);

?>

Another, perhaps more simple option is:

Go to the admin menu on the page right of the overview tab and click on the link "publish content" (schedule_content.php).

On the form that opens:

Publishing options
Publish albums by default UN-SELECT THIS

Make images visible by default UN-SELECT THIS

 


That option makes it so that new folders (and images) are not automatically, dynamically made public (published). This is a good option for FrontPage/ExpressionWeb users. WHY might you want to use both options? If someone else will be admininstrator of the photo gallery, they might change the settings. The PHP code is insurance for that.

SLIDE SHOW DEMO


 


<table><tr><!-->loader animation in background of cell-->
<td style="background-position: center; text-align: center; width: 300pxpx; background-image:url('ajax-loader.gif'); background-repeat:no-repeat">

<script language="javascript">

/*
Random image slideshow- By Tyler Clarke (tyler@ihatecoffee.com)
For this script and more, visit http://www.javascriptkit.com - http://www.javascriptkit.com/script/script2/randomslide.shtml
PHP RSS/XML parser from http://ditio.net/2008/06/19/using-php-curl-to-read-rss-feed-xml/
ajax-loader.gif from http://ajaxload.info/
This example using zenphoto rss at http://classicaspreference.com
*/

var delay=3000 //set delay in miliseconds
var curindex=0

var randomimages=new Array()


<?php

$ch = curl_init("http://YOURURL/zenphoto/index.php?rss");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
//curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);

if(isset($doc->channel))
{
parseRSS($doc);
}
if(isset($doc->entry))
{
parseAtom($doc);
}

function parseRSS($xml)
{

//echo "<strong>".$xml->channel->title."</strong>";
$cnt = count($xml->channel->item);
$n=-1;
for($i=0; $i<$cnt; $i++)
{
$url = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$desc = $xml->channel->item[$i]->description;

$pos = strpos($desc, '_vti_cnf'); //this is to filter out certain FrontPage/ExpressionWeb folders
if ($pos ===false) {
$n=$n+1;
//echo " N ".$n;
$myPic = explode("src=",$desc,2);
$myPix = explode(" alt=",$myPic[1]);

$input = trim( preg_replace( '/\s+/', '', "randomimages[". ($n) ."]=$myPix[0]" ) );
$picSet= "$input\n";
}
//echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'';

echo $picSet;

}
}

//this section for Atom formatted feeds
function parseAtom($xml)
{
echo "<strong>".$xml->author->name."</strong>";
$cnt = count($xml->entry);
for($i=0; $i<$cnt; $i++)
{
$urlAtt = $xml->entry->link[$i]->attributes();
$url = $urlAtt['href'];
$title = $xml->entry->title;
$desc = strip_tags($xml->entry->content);

echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'';


}
}
?>



var preload=new Array()

for (n=0;n<randomimages.length;n++)
{
preload[n]=new Image()
preload[n].src=randomimages[n]
}

//Choose display with or without hyperlink:
//document.write('<a href=http://YOURDOTCOM/zenphoto/><img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'"></a>')
document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'">')


function rotateimage()
{

if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
curindex=curindex==0? 1 : curindex-1
}
else
curindex=tempindex

document.images.defaultimage.src=randomimages[curindex]
}

setInterval("rotateimage()",delay)

</script>

</td>
</tr>
</table>

 

 


Back to Site Menu
My favorite scripts site is Codango.com