anf.etienne Posted February 4, 2009 Share Posted February 4, 2009 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>"); ?> Quote Link to comment Share on other sites More sharing options...
haku Posted February 5, 2009 Share Posted February 5, 2009 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. Quote Link to comment Share on other sites More sharing options...
anf.etienne Posted February 5, 2009 Author Share Posted February 5, 2009 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) Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2009 Share Posted February 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.