Flash Gallery with PHP and images from a database
The names of my pictures are stored in a mySQL database. How can I use these images instead of the generated XML file from Flash Album Generator 2?
Answer:
You could save your XML as a PHP file (For example, “MyGallery.xml” into “MyGallery_xml.php”. I have called mine Gallery_dyn_xml.php). Then create a connection to your database and retrieve the image names. Then make the attributes (id, filename and thumbnail) of the photo elements to be filled with the information from your database.
One example of the modified file may look like this:
<?php // **** the original xml file is renamed to Gallery_dyn_xml.php **** ?>
<?php require_once('Connections/cnn_TestDB.php'); ?>
<?php $filesPath = "Images/"; ?>
<?php
//Define function GetSQLValueString
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
//Define Function for removing the extension from the file name
if (!function_exists("RemoveExtension")) {
function RemoveExtension($strName)
{
$ext = strrchr($strName, '.');
if($ext !== false)
{
$strName = substr($strName, 0, -strlen($ext));
}
return $strName;
}
}
$colname_rsImages = "1";
if (isset($_GET['userId'])) {
$colname_rsImages = $_GET['userId'];
}
mysql_select_db($database_cnn_TestDB, $cnn_TestDB);
$query_rsImages = sprintf("SELECT * FROM images WHERE userId = %s", GetSQLValueString($colname_rsImages, "int"));
$rsImages = mysql_query($query_rsImages, $cnn_TestDB) or die(mysql_error());
$row_rsImages = mysql_fetch_assoc($rsImages);
$totalRows_rsImages = mysql_num_rows($rsImages);
?>
<?xml version="1.0" encoding="UTF-8"?>
<flashgallery version="1">
<gallery id="Gallery_dyn_xml">
<title>My Image Gallery</title>
<meta>
<entry name="backgroundColor">
0xFFFFFF
</entry>
<entry name="foregroundColor">0x000000</entry>
<entry name="layout">2</entry>
<entry name="photoScale">0.7</entry>
<entry name="thumbnailRows">2</entry>
<entry name="thumbnailColumns">5</entry>
<entry name="thumbnailPadding">10</entry>
<entry name="frameColor">0xFFFFFF</entry>
<entry name="activeFrameColor">0xFFFFFF</entry>
<entry name="frameWidth">1</entry>
<entry name="activeFrameWidth">6</entry>
<entry name="transition">mxfade?duration=1000</entry>
<entry name="noShadow">true</entry>
</meta>
<?php do { ?>
<?php
//Empty Filename check
if($row_rsImages['id'] == "") continue;
//Check if the image and the thumbnail exist on the server
if ((!file_exists($filesPath.$row_rsImages['image']))||
(!file_exists($filesPath."thumbs/".$row_rsImages['image']))) continue;
?>
<photo id="<?php echo $row_rsImages['id']; ?>" filename="<?php echo $filesPath.$row_rsImages['image']; ?>" thumbnail="<?php echo $filesPath."thumbs/".$row_rsImages['image']; ?>">
<title><?php echo RemoveExtension($row_rsImages['image']); ?></title>
</photo>
<?php } while ($row_rsImages = mysql_fetch_assoc($rsImages)); ?>
</gallery>
</flashgallery>
<?php
mysql_free_result($rsImages);
?>
My connection file is called cnn_TestDB.php. The table for the pictures is called images and I have three columns: id - the primary key of the table, image - which contains the image name, and userId. The userId column accepts integer values and I use it to select the images for a particular gallery.
With a do while loop I write the information for the photo element values.
For the id attribute of the photo element, I use the value into the primary key field. Check:
<?php echo $row_rsImages['id']; ?>
Because, only the image name in my image field is saved, I use the variable $filesPath to store the path to the images:
<?php $filesPath = "Images/"; ?>
To get the filename I write:
<?php echo $filesPath.$row_rsImages['image']; ?>
The thumbnails have the same name but are stored into thumbs sub folder. To achieve this, use the following code:
<?php echo $filesPath."thumbs/".$row_rsImages['image']; ?>
To display the image title without the extension I use the function RemoveExtension:
<title><?php echo RemoveExtension($row_rsImages['image']); ?></title>
I want to write photo information only for the records which contain valid information. For this, I perform the following two checks:
<?php
//Empty Filename check
if($row_rsImages['id'] == "") continue;
//Check if the image and the thumbnail exist on the server
if ((!file_exists($filesPath.$row_rsImages['image']))||(!file_exists($filesPath."thumbs/".$row_rsImages['image']))) continue;
?>
When the php file is used, it has to be passed the userId value. If the file, that contains the Flash Gallery, is php the userId my be dynamically generated.
For example:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="500" height="500" id="Gallery_dyn_xml" align="middle">
<param name="FlashVars" value="Gallery=Gallery_dyn_xml.php?userId=<?php echo $userID ?>" />
<param name="movie" value="dmxFlashGallery2.swf" />
<param name="quality" value="best" />
<param name="scale" value="noscale" />
<param name="bgcolor" value="#FFFFFF" />
<embed src="dmxFlashGallery2.swf" id="Gallery_dyn_xml" quality="best" flashvars="Gallery=Gallery_dyn_xml.php?userId=<?php echo $userID ?>" scale="noscale" bgcolor="#FFFFFF" type="application/x-shockwave-flash" width="500" height="500" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
For additional information you could also check the following FAQ, which shows how a similar file can be created for Active Slideshow Pro using ASP server-side technology:
How to create a Dynamic Slideshow
DISCLAIMER:
This is extra complimentary content which purpose is to show additional usage that is not part of the product, i.e. it suggests tips for extending the functionality of the product by the users themselves.
It is provided "as is", without warranty of any kind, express or
implied , including but not limited to the warranties of
merchantability, fitness for a particular purpose and nonfringement of
third party rights.
DMXzone does not take responsibility to
support the suggested content to be fully compatible and working as
intended and does not provide support for extended functionality which
is not included in the current release of the extension.
It is highly recommended that only more advanced developers use it.
Comments
Be the first to write a comment
You must me logged in to write a comment.