Jump to content

[SOLVED] XML Replication problem


dmschenk

Recommended Posts

I'm trying to replicate an XML file using PHP and can't figure out what I'm doing wrong. 

The pgCategoryID name should be grouping the images but instead is listed before each item.

I've included code blocks for the php code, a mysql db, what I'm currently getting and what I would like to achieve. 

I would really appreciate if someone could help me figure out what I'm doing wrong here

 

 

PHP Code:

//DB Connection
mysql_select_db($database_dbFV, $dbFV);
$query_rsFVPhotoGallery = "SELECT photogallery.* FROM photogallery ORDER BY pgCategoryID";
$rsFVPhotoGallery = mysql_query($query_rsFVPhotoGallery, $dbFV) or die(mysql_error());
//$row_rsFVPhotoGallery = mysql_fetch_assoc($rsFVPhotoGallery);
//$totalRows_rsFVPhotoGallery = mysql_num_rows($rsFVPhotoGallery);

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<imagegallery>\n";
echo "<settings basePath=\"gallery/\" />\n";
echo ("<directory name=\"Image Gallery\" >\n");

while($row_rsFVPhotoGallery = mysql_fetch_assoc($rsFVPhotoGallery)) {
if (!(strcmp($row_rsFVPhotoGallery["pgCategoryID"],$row_rsFVPhotoGallery["pgCategoryID"]))) {
echo ("<directory name=" . "\"" . $row_rsFVPhotoGallery['pgCategoryID'] . "\" thumbUrl=\"" . $row_rsFVPhotoGallery['pgThmURL'] . "\">\n");
echo ("<image name=" . "\"" . $row_rsFVPhotoGallery['pgTitle'] . "\" imageUrl=\"" . $row_rsFVPhotoGallery['pgImageURL'] . "\" thumbUrl=\"" . $row_rsFVPhotoGallery['pgThmURL'] . "\"/>\n");
} else {
	echo ("</directory>\n");
} 
}
echo ("</directory>\n");
echo "</imagegallery>\n";
?>
<?php
mysql_free_result($rsFVPhotoGallery);
?>

 

 

 

This is the current XML Output:

<?xml version="1.0" encoding="UTF-8"?>
<imagegallery>
<settings basePath="gallery/" />
<directory name="Image Gallery" >
<directory name="People" thumbUrl="people/thmpeople_01.jpg">
<image name="People01" imageUrl="people/people_01.jpg" thumbUrl="people/thmpeople_01.jpg"/>
<directory name="People" thumbUrl="people/thmpeople_02.jpg">
<image name="People02" imageUrl="people/people_02.jpg" thumbUrl="people/thmpeople_02.jpg"/>
<directory name="People" thumbUrl="people/thmpeople_03.jpg">
<image name="People03" imageUrl="people/people_03.jpg" thumbUrl="people/thmpeople_03.jpg"/>
<directory name="Animals" thumbUrl="animals/thmanimal_01.jpg">
<image name="Animals01" imageUrl="animals/animal_01.jpg" thumbUrl="animals/thmanimal_01.jpg"/>
<directory name="Animals" thumbUrl="animals/thmanimal_02.jpg">
<image name="Animals02" imageUrl="animals/animal_02.jpg" thumbUrl="animals/thmanimal_02.jpg"/>
<directory name="Animals" thumbUrl="animals/thmanimal_03.jpg">
<image name="Animals03" imageUrl="animals/animal_03.jpg" thumbUrl="animals/thmanimal_03.jpg"/>
</directory>
</imagegallery>

 

 

 

 

This is theXML I would like to achieve:

<?xml version="1.0" encoding="utf-8"?>
<imagegallery>
  <settings basePath="gallery/" />
  <directory name="Image Gallery" thumbUrl="animals/thmanimal_01.jpg">
    <directory name="People" thumbUrl="people/thmpeople_01.jpg">
      <image name="People 1" imageUrl="people/people_01.jpg" thumbUrl="people/thmpeople_01.jpg" />
      <image name="People 2" imageUrl="people/people_02.jpg" thumbUrl="people/thmpeople_02.jpg" />
      <image name="People 3" imageUrl="people/people_03.jpg" thumbUrl="people/thmpeople_03.jpg" />
    </directory>
    <directory name="Animals" thumbUrl="animals/thmanimal_01.jpg">
      <image name="Animal 1" imageUrl="animals/animal_01.jpg" thumbUrl="animals/thmanimal_01.jpg" />
      <image name="Animal 2" imageUrl="animals/animal_02.jpg" thumbUrl="animals/thmanimal_02.jpg" />
      <image name="Animal 3" imageUrl="animals/animal_03.jpg" thumbUrl="animals/thmanimal_03.jpg" />
    </directory>
  </directory>
</imagegallery>

 

 

 

 

Here is the Mysql database info:

-- phpMyAdmin SQL Dump
-- version 2.10.0.2
-- Server version: 5.0.27
-- PHP Version: 5.2.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE `photogallery` (
  `pgID` int(10) unsigned NOT NULL auto_increment,
  `pgTitle` varchar(50) NOT NULL,
  `pgImageURL` varchar(100) NOT NULL,
  `pgThmURL` varchar(100) NOT NULL,
  `pgCategoryID` varchar(50) NOT NULL,
  PRIMARY KEY  (`pgID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `photogallery` (`pgID`, `pgTitle`, `pgImageURL`, `pgThmURL`, `pgCategoryID`) VALUES 
(1, 'People01', 'people/people_01.jpg', 'people/thmpeople_01.jpg', 'People'),
(2, 'People02', 'people/people_02.jpg', 'people/thmpeople_02.jpg', 'People'),
(3, 'People03', 'people/people_03.jpg', 'people/thmpeople_03.jpg', 'People'),
(4, 'Animals01', 'animals/animal_01.jpg', 'animals/thmanimal_01.jpg', 'Animals'),
(5, 'Animals02', 'animals/animal_02.jpg', 'animals/thmanimal_02.jpg', 'Animals'),
(6, 'Animals03', 'animals/animal_03.jpg', 'animals/thmanimal_03.jpg', 'Animals');

 

Thanks

Dave

Link to comment
Share on other sites

This is what the solution ended up to be thanks to "Lostboy" on http://weberforums.com

Dave

 

$oldGallery = '';   //default old gallery name 
  
while($row_rsFVPhotoGallery = mysql_fetch_assoc($rsFVPhotoGallery)) 
{ 
// compare the name of the rs gallery to the old gallery 
  if ($oldGallery != $row_rsFVPhotoGallery["pgCategoryID"]) 
  { 

//   check to see if the $oldGallery is not empty to avoid closing the directory in the first iteration 
     if ($oldGallery != ''){ 
       echo ("</directory>\n"); 
     } // end if empty gallery check 

//   echo the new directory xml node 
     echo ("<directory name=" . "\"" . $row_rsFVPhotoGallery['pgCategoryID'] . "\" thumbUrl=\"" . $row_rsFVPhotoGallery['pgThmURL'] . "\">\n"); 

//   set the $oldGallery variable to the new name 
     $oldGallery = $row_rsFVPhotoGallery['pgCategoryID']; 
   } // end if gallery name check 
    
// echo out the image xml node 
    echo ("<image name=" . "\"" . $row_rsFVPhotoGallery['pgTitle'] . "\" imageUrl=\"" . $row_rsFVPhotoGallery['pgImageURL'] . "\" thumbUrl=\"" . $row_rsFVPhotoGallery['pgThmURL'] . "\"/>\n"); 
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.