View Full Version : PHP Quick Question: Saving form data into .xml with PHP
benghee
September 8th, 2008, 02:46 AM
Hi all experts,
I want to make a multiple selection form that allow user to upload picture, enter picture name,caption and url. I know how to make the form and i'm working on the uploading part. Just i need to know its how to generate all the data into xml once the user click submit button. Thanks for everyone help. :beam: Here is the xml output i need to generate:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>http://www.test.com/img/25.jpg</image>
<caption>webcaption>
<url>http://www.test.com</url>
</pic>
<pic>
<image>http://www.test.com/img/26.jpg</image>
<caption>web_pagecaption>
<url>http://www.test.com/wp</url>
</pic>
<pic>
.....
</pic>
</images>
adroit
September 9th, 2008, 08:43 AM
Hi,
Try to use below given code.
$xml = '';
$xml .= '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
$xml .= '<images>';
for($i=0;$i<$count_of_number_of_fields)$i++)
{
$xml .= '<pic>';
$xml .= '<image>UPLOADED IMAGE PATH</image>';
$xml .= '<caption>WEP_PAGE_CAPTION<caption>';
$xml .= '<url>LINK_URL</url>';
$xml .= '</pic>';
}
$xml .= '</images>';
echo $xml;
Now you can create xml file with $xml as content.
Regards,
adroit
tfg
September 9th, 2008, 10:27 AM
if you're saving the data to an xml file on the server, you'll need to use fopen() http://uk3.php.net/manual/en/function.fopen.php and fwrite() http://uk3.php.net/manual/en/function.fwrite.php
if you're adding new data to the end of the file then you'd probably want to open the file with fopen("/path/to/file.xml", "a"); or if you want to replace the file's contents each time then you'd use fopen("/path/to/file.xml", "w");
once you've used fopen you use fwrite to put your contents into the file.
benghee
September 9th, 2008, 11:55 PM
Thanks, adroit and tfg, for your replied. I went tru some advance tutorial here at kirupa and got a xml_intermediate files and run it in my server. However, there is an error that the php script cant parse the xml. Any idea how to solve it? Thanks.
<?php
$xml_file = "xml_intermediate.xml";
$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?
// A simple for loop that outputs our final data.
for($x=0;$x<count($story_array);$x++){
echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n";
echo "\t\t<br />\n";
echo "\t<i>" . $story_array[$x]->description . "</i>\n";
}
?>
</body>
</html>
Here is the error displayed after output in browser:
" . $story_array[$x]->headline . "\n"; echo "\t\t
\n"; echo "\t" . $story_array[$x]->description . "\n"; } ?>
tfg
September 10th, 2008, 07:30 AM
which version of php are you running?
upload this to your server as info.php and test in your browser. if you're using version 5 then you can use simplexml (http://uk3.php.net/manual/en/function.simplexml-load-file.php) to access the node far, far more easily than writing your own parser. remember to delete the info.php file after though, as anyone who comes across it will have access to important information about your server!
<?php
phpinfo();
?>
if you're using version 4, then you can use this excellent php 4 version of simplexml from here (http://www.phpclasses.org/browse/package/4484.html)
otherwise, if you'd rather go with the code you've got above, add the line
error_reporting(E_ALL);
immediately after the opening <?php tag and report back with the exact error.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.