Click to See Complete Forum and Search --> : creating XML files?


mistafeesh
03-10-2008, 06:30 AM
Hi,

I've incorporated a gallery script (fotoplayer ("http://fotoplayer.com/)) into my site. It does exactly what I want, BUT it uses XML files to set it's preferences. I could really do with being able to dynamically change the contents of those files, so that I don't have to store multiple copies of the same files.

Any ideas? I'm not anywhere near what you'd call a PHP expert, but I'm not a total novice either!

Cheers,

Dan

stephan.gerlach
03-10-2008, 08:09 AM
Thats no problem.

If you post your xml file i can show you how.

mistafeesh
03-10-2008, 09:03 AM
cool - thanks!!

Here's the top part of the XML file (I tried to post the whole thing but it was too long!) This is the bit where I need to be able to make changes dynamically. I've bolded the bits I want to be able to change. The file is called "shoppingcart.xml" I can't change the filename, as it's hardcoded into the flash file that uses it....

This gallery script has a shopping cart built into it, and on the site I'm building people need to first choose between buying prints or an album, both of which have different parameters....

<?xml version="1.0" encoding="UTF-8" ?>
<order savecart="Y" currencysymbol="£" shipping="N"shmode="1" discountmode="1" emailorder="Y"
paypal="N" saveorder="Y" discount="N" tax="N" discountcoupon="N" discountcoupontype_percent="N" offlineorder="N" viewcartnotes="Y" creditcard="Y" googlecheckout="N" restorecontents="Y" minimumorder="1" minimumquantityorder="1">

<entry>
<format size="4 x 6" price="5" type="print" category=""></format>
<format size="5 x 7" price="10" type="print" category=""></format>
<format size="6 x 9" price="10" type="print" category=""></format>
<format size="8 x 10" price="13" type="print" category=""></format>
<format size="8 x 12" price="15" type="print" category=""></format>
<format size="12 x 16" price="25" type="print" category=""></format>
<format size="16 x 20" price="35" type="print" category=""></format>

</entry>

<shippinghandling mode="1" comment="based on number of quantities">
<foritems from="1" to="5">2.00</foritems>
<foritems from="6" to="10">4.00</foritems>
<foritems from="11" to="100">8.00</foritems>
</shippinghandling>

<SNIP>

Znupi
03-10-2008, 01:49 PM
You can just use echo or print or printf or whatever outputting function you desire, provided you send the appropiate headers. Here's an example:

<?php
header("Content-Type: application/xml"); // This can be text/xml, too
echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo '<order savecart="Y" currency......and so on';
// etc
?>

Hope this helps :)

mistafeesh
03-11-2008, 04:18 PM
Ta. I've just found a tutorial on fwrite that'll do the rest. here (http://www.tizag.com/phpT/filewrite.php)

Znupi
03-12-2008, 11:42 AM
Why would you use fwrite? Do you want to create an XML file with PHP and just leave it that way? Then don't bother using php, just create the XML yourself. I thought you wanted the xml file to be dynamic and change each time it's loaded..

mistafeesh
03-13-2008, 06:42 AM
Well, in order to use php commands in a xml file I'd have to change the file extension to .php wouldn't I? I can't do that because the .xml filenames are hardwritten into the flash files that use them.

Or is it possible to write into xml files without changing the extension.... I'm confused!!

Znupi
03-13-2008, 07:05 AM
You can write your code to a file with a .xml extension, then create a file called .htaccess, put it in the same directory as your .xml file with the php in it, and put this into it:

<Files "shoppingcart.xml">
ForceType application/x-httpd-php
</Files>

Supposing you are using Apache as your server. Tell us if that works :)

mistafeesh
03-13-2008, 07:20 AM
awesome - I didn't know you could do that!!

If it was on my server there'd definitely be no problem. I just need to check whether .htaccess files are allowed on my friends server....

MrCoder
03-13-2008, 09:26 AM
You can write your code to a file with a .xml extension, then create a file called .htaccess, put it in the same directory as your .xml file with the php in it, and put this into it:

<Files "shoppingcart.xml">
ForceType application/x-httpd-php
</Files>

Supposing you are using Apache as your server. Tell us if that works :)

Or use "RewriteEngine" and "RewriteRule"

Znupi
03-14-2008, 08:02 AM
Indeed, but, it's easier with ForceType, don't you think? :)

mistafeesh
03-14-2008, 08:16 AM
I'll go with force type. .htaccess files are fine with his host.

thanks for your help!

MrCoder
03-17-2008, 05:14 AM
Indeed, but, it's easier with ForceType, don't you think? :)

Typing ForceType makes me feel dirty..

I avoid it since it can be really insecure.

mistafeesh
04-09-2008, 05:57 AM
In what way can it be insecure? There's nothing that needs to be hidden from people in those files, but there is in other files nearby...

mistafeesh
04-10-2008, 07:01 AM
for some reason forcing the type to php makes the flash file not pick up any of the info in the file, even before I add in any php code.... any ideas?

Znupi
04-10-2008, 02:00 PM
Maybe the xml php outputs isn't correct?

mistafeesh
04-11-2008, 08:50 AM
this is before I even change the text of the file from the original xml. S'okay, I've got in touch with the author of the software who sounds like he's going to help. I'm used to open source - it's weird having code in my website that I can't read!!!

Znupi
04-12-2008, 04:07 AM
Maybe you're not sending the right headers..

header("Content-Type: application/xml");
// or..
header("Content-Type: text/xml");

mistafeesh
04-12-2008, 08:02 AM
Thanks. I gave it a try, as it just had <?xml version="1.0" encoding="UTF-8" ?> and no php content, but it still doesn't work...

Znupi
04-12-2008, 02:18 PM
Try accessing the file with your browser (e.g. http://example.com/shoppingcart.xml) and see if it outputs any errors. Most probably, your configuration has the short_open_tag (http://www.php.net/manual/en/ini.core.php#ini.short-open-tag) enabled. In this case, you'll have to output the <?xml tag using php:

<?php echo '<?xml blah blah..';?>
// instead of
<?xml blah blah..
// because otherwise php would try to parse what's after <?

mistafeesh
04-14-2008, 03:53 AM
Got it!!

PHP didn't like the first line of the original file, which read: <?xml version="1.0" encoding="UTF-8" ?> so it threw up an error. Daft mistake to make - because XML is all new to me I didn't think to look and see what the file looked like in a browser - I was just checking whether the flash file could read it..... oops!

Znupi
04-14-2008, 09:06 AM
Well, that's what I said, it's because the short_open_tag directive :)

mistafeesh
04-14-2008, 11:11 AM
I didn't see your post before.

At the moment I've just removed the xml tag, but I will make sure I output it with php before it goes live.

thanks!!!