Reputation: 41
I want to put the value of $album into an XML tag called category. But everything I try is wrong. The closest I get is the variable name $album but not the data inside. I'm new to writing XML with php. I have tried all sorts of concatenation & have seen CDATA mentioned here but don't know if I should be using it or how.
<?php
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "password";
$dbname = "db";
@mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$sql = "SELECT * FROM responsivegallery_rgal";
$q = mysql_query($sql) or die(mysql_error());
$album = $q['album_rgal'];
$category_tag = '"<category caption = ". $album .">"';
$xml = "<gallery>";
while($r = mysql_fetch_array($q)){
$xml .= $category_tag ."</category>";
$xml .= "<item>";
$xml .= "<image>"."gallery_files/slides/".$r['image_rgal']."</image>";
$xml .= "<thumb>"."gallery_files/slides/".$r['thumb_rgal']."</thumb>";
$xml .= "<caption>".$r['title_rgal']."</caption>";
$xml .= "</item>";
}
$xml .= "</gallery>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("test.xml");
//$sxe->asXML("../gallery_files/gallery.xml");
?>
Upvotes: 0
Views: 346
Reputation: 23892
You constructing your loop incorrectly, the $q
wasn't fetched so ['album_rgal']
won't exist. Try this
<?php
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "password";
$dbname = "db";
@mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$sql = "SELECT * FROM responsivegallery_rgal";
$q = mysql_query($sql) or die(mysql_error());
$xml = "<gallery>";
while($r = mysql_fetch_array($q)){
$xml .= '<category caption="'. $r['album_rgal'] .'"></category>';
$xml .= "<item>";
$xml .= "<image>"."gallery_files/slides/".$r['image_rgal']."</image>";
$xml .= "<thumb>"."gallery_files/slides/".$r['thumb_rgal']."</thumb>";
$xml .= "<caption>".$r['title_rgal']."</caption>";
$xml .= "</item>";
}
$xml .= "</gallery>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("test.xml");
//$sxe->asXML("../gallery_files/gallery.xml");
?>
Also your concatenation was off.
$category_tag = '"<category caption = ". $album .">"';
would have been
$category_tag = '"<category caption = "'. $album .'">"';
Upvotes: 1