I'm trying basically to convert an XML file to a PHP script that outputs the

christsealed

New member
identical code.? The original XML file looks something like this.

<?xml version="1.0"?>
<galleries>
<gallery title="architecture" intro="These are the photos of various buildings in the cities, towns and villages I visited during my voyages.">
<image>Dallas.</image>
<image>Tresnjevka. I love the atmosphere on this one.</image>
<image>Motovun.</image>
<image>New York.</image>
<image>Paris.</image>
</gallery>
<gallery title="essays" intro="A collection of various photos which either do not fit any other category - experiments and such.">
<image>Color mayhem!</image>
<image>The sleeping monster.</image>
<image>Let me out!</image>
<image>A barrel on the side of the trail.</image>
<image>Subterranean passage.</image>
<image>A train in snow.</image>
</gallery>
<galleries>

Here's my php

<?PHP

$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';

$gallPath = '/gallery/';

$link = mysql_connect ("localhost", "root", "");
mysql_select_db ("test");

$query = 'SELECT * FROM galleries';
$results = mysql_query($query);

echo "$xml\n";
echo "<galleries>\n";

while ($line = mysql_fetch_assoc($results)) {
echo "<gallery title=\"{$line["galleryTitle"]}\" intro=\"{$line["galleryIntro"]}\">\n";
echo "<img src=\"$gallPath{$line["galleryTitle"]}/{$line["imgName"]}\" description=\"{$line["imgDesc"]}\" />\n";
echo "</gallery>\n";
}

echo "</galleries>";

mysql_close($link);

?>

But the output isn't the same. I've changed a few things, obviously, but I would just like that based on the table field galleryTitle, the php would echo each item with that title and then close the node, instead of closing the node after each item.

Does that make sense?
 
Um ... I'm having trouble understanding what you want to do here. It seems like what you want to do is replicate the structure of the top XML document into a new XML document constructed from a MySQL query.

<?php
if(!$link = mysql_connect("server", "user", "pass")) {
die("Cannot connect to db server");
}
if(!mysql_select_db("dbname")) {
die("Cannot select database");
}

if(!$rs = mysql_query("SELECT * FROM galleries")) {
die("Cannot parse query");
}

if(mysql_num_rows($rs) == 0) {
die("No matching records");
}

$doc = new XMLWriter();
$doc->startDocument( "1.0", "utf-8", "yes");
$doc->startElement( "galleries" );

while($row = mysql_fetch_array($rs)) {
$doc->startElement( "gallery" );
$doc->writeAttribute( "title", $row['galleryTitle'] );
$doc->writeAttribute( "intro", $row['galleryIntro'] );
$doc->writeElement( "image", $row['imgDesc'] );
$doc->endElement();
}

$doc->endElement();
$doc->flush();
?>

It appears from what you have here that you either misunderstand the proper structuring of the data or have contrived a way to place the images and gallery information in the same table, although the images and galleries should be in separate tables and keyed to one another.

UPDATE:

To properly structure your data, you have two tables: galleries and images.

galleries contains a primary key (int) column named gallery_id.

images contains a int column named gallery_id as well. It contains the number of the gallery to which the image belongs. For example, if image 4 belongs in gallery 1, then its gallery_id column contains a 1.

Assuming you do that, you change the code above thus to recreate the XML you want:

<?php
if(!$link = mysql_connect("server", "user", "pass")) {
die("Cannot connect to db server");
}
if(!mysql_select_db("dbname")) {
die("Cannot select database");
}

if(!$gal = mysql_query("SELECT * FROM galleries")) {
die("Cannot parse query");
}

if(mysql_num_rows($gal) == 0) {
die("No matching records");
}

$doc = new XMLWriter();
$doc->startDocument( "1.0", "utf-8", "yes" );
$doc->startElement( "galleries" );

while($galrow = mysql_fetch_array($gal)) {
$doc->startElement( "gallery" );
$doc->writeAttribute( "title", $galrow['galleryTitle'] );
$doc->writeAttribute( "intro", $galrow['galleryIntro'] );
if(!$img = mysql_query("SELECT * FROM images WHERE gallery_id = $galrow[gallery_id]")) {
$doc->writeElement( "image", "could not parse image query" );
}
elseif(mysql_num_rows($img) == 0) {
$doc->writeElement( "image", "no images in this gallery" );
}
else {
while($imgrow = mysql_fetch_array($img)) {
$doc->writeElement( "image", $imgrow['imgDesc'] );
}
}
$doc->endElement();
}

$doc->endElement();
$doc->flush();
?>
 
Back
Top