Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php, xml and gallery (dynamic). 1

Status
Not open for further replies.

rahulpatel

Programmer
Jan 20, 2007
33
AU
Not sure if this is a PHP question or a XML question, so I'll ask it here first.

I have a database in mysql and I am making a gallery from it. I found a 'flash' gallery that works from a xml file. I don't want to keep amending my files as and when I add an image, so I'm trying to make it dynamic. I have had success up to a point.

This is what the original file looks like
Code:
<?xml version="1.0" encoding="UTF-8"?>
<veppaPhotoAlbum maxImageWidth="400" maxImageHeight="400" imageFit="normal" textColor="0x009999" frameColor="0x000000" frameWidth="5" stagePadding="10" thumbnailColumns="2" thumbnailRows="4" navPosition="right" imagePath="images/" thumbPath="images/thumbs/" autoPlay="false" slideInterval="5" imageDefaultLink="self">
<image>
	<filename>....

This is what I have got to, to try and make the gallery.xml dynamic...
Code:
<?php
if(!$dbconnect = mysql_connect('localhost', 'root', '')) {
   echo "Connection failed to the host 'localhost'.";
   exit;
} // if
if (!mysql_select_db('gallery')) {
   echo "Cannot connect to database 'gallery'";
   exit;
} // if

$table_id = 'image';
$query = "SELECT filename, caption FROM image";
$dbresult = mysql_query($query, $dbconnect);

// create a new XML document
$doc = new DomDocument('1.0');
$doc->encoding="UTF-8";
// create root node
$root = $doc->createElement('veppaPhotoAlbum');

$root = $doc->appendChild($root);

// process one row at a time
while($row = mysql_fetch_assoc($dbresult)) {

// add node for each row
  $occ = $doc->createElement($table_id);
  $occ = $root->appendChild($occ);
  
// add a child node for each field
  foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
    $child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);
    } // foreach
} // while

// get completed xml document
$xml_string = $doc->saveXML();
echo $xml_string;

print $doc->save("gallery.xml");

?>

My code works well apart from the code that tells the flash gallery how many columns there are, borders etc. I cannot find out how to get it in. Any spaces in the createElement line throws an error. I need to add these instructions to the root element
maxImageWidth="400" maxImageHeight="400" imageFit="normal" textColor="0x009999" frameColor="0x000000" frameWidth="5" stagePadding="10" thumbnailColumns="2" thumbnailRows="4" navPosition="right" imagePath="images/" thumbPath="images/thumbs/" autoPlay="false" slideInterval="5" imageDefaultLink="self"

Does anyone know how to solve this issue. The root element is not dynamic but I can't hard code it either. Any help is always gratefully received.
 
Once $root is established, you can set those attribute like this.
[tt]
$root = $doc->appendChild($root);

$root->setAttribute("maxImageWidth","400");
$root->setAttribute("maxImageHeight","400");
$root->setAttribute("imageFit","normal");
//etc etc...

// process one row at a time
while($row = mysql_fetch_assoc($dbresult)) {
//etc etc...
}
//etc etc...
[/tt]
Is that what you mean and dynamic enough?
 
tsuji,

Too easy wasn't it. I've been staring at this problem for the best part of this evening. I managed to get where I got to, and then hit the wall. It sometimes takes a fresh pair of eyes. That is exactly what I wanted.

However, I've spent another hour still trying to get it to work. This is a warning to others as well. Double check upper and lower case. I was about to give up and realised the XML file is case sensitive. As soon as I sorted that out, it all worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top