View Full Version : update xml
g5604
2nd September 06, 07:19 PM
Hi,
i have a gallery which is built using xml. I would like a way to be able to upate the xml using a user friendly interface. I have an idea that i might need to have a mysql database that outputs the xml using php, but i am not sure where to start. Does anyone have any tips, hints or tutorials? or is there an easy way to do this?
many thanks,
G
Game Makker
3rd September 06, 09:41 AM
You can update and change xml using php. If I am not correct :S but I dion't currently know how but I am reading about it soon so if I find out I will PM you.
g5604
3rd September 06, 10:05 AM
Hi,
thanks that would be great. I have had some limited success using a mysql database + php to generate xml.
I am trying to generate this xml schema:
<MENU>
<FOLDER NAME="Web" link="web.swf">
<FILE NAME="Printbook" LINK="printbook.jpg"/>
<FILE NAME="site" LINK="site.jpg"/>
</FOLDER>
<FOLDER NAME="Print link="print.swf"">
<FILE NAME="Submenu Link 1" LINK="sublink31.html" />
</FOLDER>
</MENU>
heres my php so far:
<?php
$db_name = "portfolio";
$link = mysql_connect("localhost", "root", "****") or die("Could not connect to server!");
$table_name = 'folder';
$select_db = mysql_select_db($db_name, $link);
$query = "SELECT * FROM " . $table_name;
$result = mysql_query($query, $link) or die("Could not complete database query");
$num = mysql_num_rows($result);
if ($num != 0) {
$file= fopen("results.xml" , "w");
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<menu>\r\n";
while ($row = mysql_fetch_array($result)) {
$_xml .=" <folder name='" . $row[name] . "' link= '" . $row[link] . "'>\r\n";
$_xml .=" </folder>\r\n";
}
$_xml .="</menu>";
fwrite($file, $_xml);
fclose($file);
echo $_xml;
} else {
echo "No Records found";
}
?>
This generates this:
<?xml version="1.0" encoding="UTF-8" ?>
<menu>
<folder name='web' link= 'web.swf'>
</folder>
<folder name='print' link= ''print.swf'>
</folder>
</menu>
but i cant work out how to add in the files to each folder.
thatguy
28th September 06, 01:51 PM
I've been using this script which i got from the kirupa forums a while back to update xml files. Its quite straightforward to modify, and works quite nicely.
index.php<div id="container">
<div id="uploadTag"><IFRAME id=newsiframe name=newsiframe marginWidth=0 marginHeight=0 src="images/image_upload.php" frameBorder=0 width=420 scrolling=yes height=200>Your browser doesn't support iframes. Please click <a href="images/image_upload.php">here</a> to see the content.</IFRAME>
</div>
<div id="xmlTag">
<h3>Step 2 - Update the Gallery</H3>
<form action="processForm.php" method="post">
<fieldset>
<label for="name">Description:</label> <input type="text" id="name" name="name"/><br />
<label for="source">Filename:</label><input type="text" id="source" name="source" /><br />
<label for="thumb">Thumbnail:</label> <input type="text" id="thumb" name="thumb" /> <br />
<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" />
</fieldset>
</form>
<p>Current entries:</p>
<?php
function start_tag($parser, $name, $attrs){
global $table_string;
$table_string .= "<tr><td>$attrs[title]</td><td>$attrs[source]</td><td>$attrs[thumb]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
<tr><th>DESCRIPTION</th><th>FILENAME</th><th>THUMBNAIL NAME</th></tr>";
xml_parse($parser, file_get_contents("gallery.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;
?>
and
processForm.php
<?
$songs = Array();
function start_element($parser, $name, $attrs){
global $songs;
if($name == "image"){
array_push($songs, $attrs);
}
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("gallery.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
array_push($songs, Array(
"title" => $_POST['name'],
"source" => $_POST['source'],
"thumb" => $_POST['thumb']));
$songs_final = $songs;
}else if($_POST['action'] == "del"){
$songs_final = Array();
foreach($songs as $song){
if($song['title'] != $_POST['name']){
array_push($songs_final, $song);
}
}
}
$write_string = '<portfolio cols="12">';
foreach($songs_final as $song){
$write_string .= "\n<image title=\"$song[title]\" source=\"$song[source]\" thumb=\"$song[thumb]\" />\n";
}
$write_string .= '</portfolio>';
$fp = fopen("gallery.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Image inserted or deleted successfully</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>
It was originally for an mp3 playlist but i've used i a couple of times to work with image galleries.
Hope this is what ypu're looking for.
Dave
Vox Pelli
30th September 06, 04:42 PM
How about doing it like this:
Have a table in mysql which supplies you with the filenames.
You fetch all of them before you fetch the maps and you order them after the name of the folder. Something like this:
SELECT folder, name, link FROM files ORDER BY folder
Then you run them through and assigns them to an array like this, if a name only appears once in each folder:
while($row = mysql_fetch_array($answer)) {
$array[$row['folder']][$row['name']] = $row['link'];
}
Then when you are making your xml file yu are doing like this:
while ($row = mysql_fetch_array($result)) {
$_xml .=" <folder name='" . $row[name] . "' link= '" . $row[link] . "'>\r\n";
foreach($array[$row['name']] as $key => $value) {
$xml .= '<file name="'.$key.'" link="'.$value.'"/>'
}
$_xml .=" </folder>\r\n";
}
If these folders and files are actual folders and files you can also use php-functions to read the data from your filesystem and construct the xml-file from that.
g5604
1st October 06, 01:46 PM
many thanks guys! lots to look at.
i would be very intresting in using php to generate xml from my file system. Do you have any more info on this?
cheers,
G
Vox Pelli
1st October 06, 02:00 PM
Sorry - I haven't got any more info about it. Haven't been doing it myself, but I know that it can be done with some php-functions.
vBulletin® v3.8.4, Copyright ©2000-2013, Jelsoft Enterprises Ltd.