Jump to content

dynamic xml from php


anf.etienne

Recommended Posts

can php call the last 10 entries from a sql database and once done can it then create a xml file dynamically?

 

 

i made this php code in a tutorial to dynamically create a xml file....is it of any use to my question?

 

<?php

//XML output of an existing MySql database

header("Content-type: text/xml");

 

//to create connection to database

$connection = mysql_connect("127.0.0.1","username", "password")

or die ("could not connect to database");

 

//to select the database here test is the sample database come with mysql

$db = mysql_select_db("test",$connection)

or die ("Couldn't select database.");

 

$rs = mysql_query("select * from tablename",$connection)

or die ("invalid query");

 

//count the no. of  columns in the table

$fcount = mysql_num_fields($rs);

 

//you can choose any name for the starting tag

echo ("<result>");

while($row = mysql_fetch_array( $rs ) )

{

echo ("<tablerow>");

for($i=0; $i< $fcount; $i++)

{

$tag = mysql_field_name( $rs, $i );

echo ("<$tag>". $row[$i]. "</$tag>");

}

echo ("</tablerow>");

}

echo ("</result>");

?>

Link to comment
https://forums.phpfreaks.com/topic/143804-dynamic-xml-from-php/
Share on other sites

Yes, you can do it.

 

Add 'ORDER BY date DESC LIMIT 0, 10' to your mysql query. This will order all the table rows by the date in descending order (giving you the newest post first, and the 10th newest post last). You will have to change 'date' to whatever your column name is in your table.

 

Then, instead of echoing the data, add it to a variable so you have one long string. call it $xml or something. So everywhere you have 'echo' you will change it to '$xml .='. This will give you one long string with all the XML in it. Finally, use fopen, fwrite, and fclose (use php.net to find out how these work) to write the $xml variable to a file. This will give you the XML file.

Link to comment
https://forums.phpfreaks.com/topic/143804-dynamic-xml-from-php/#findComment-755106
Share on other sites

hey haku, thanks for replying I am going to research into what you said and test it as best as i can (i am still a newbie to php)

 

so once i use fopen, fclose or fwrite i should be able to get flash to call on the variables to display the last 10 images posted to the DB? (the location of the images will be posted instead of the whole image....it should run quicker that way)

Link to comment
https://forums.phpfreaks.com/topic/143804-dynamic-xml-from-php/#findComment-755233
Share on other sites

Well, yes, as far as I know. I know you can take in XML with flash and use it. But I don't know actionscript, so I wouldn't know how to do it. But you can definitely create an XML file with PHP that can be used with other applications. I have done this with javascript though - creating a dynamic XML file that lists off a bunch of images, and then using the javascript to read the xml file and create a slideshow.

Link to comment
https://forums.phpfreaks.com/topic/143804-dynamic-xml-from-php/#findComment-755602
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.