create rss/xml feed from database results

Hi

I have the following very dummy code to pull from my Mambo database. I want to be able to return from the results it brings back a valid XML file as shown below.

All help greatfullt received, if possible the code would auto name the XML tags based on the sql query

Neil



<?php

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

global $mosConfig_offset, $mosConfig_live_site, $mainframe, $database;

// query all content
$sql = "SELECT #1,#2,#3,#4,#5 FROM mos_###";
$database->setQuery( $sql );
$my_feed = $database->loadObjectList();

foreach ( $my_feed as $mf ) {

      $1 = $mf->#1;
      $2 = $mf->#2;
      $3 = $mf->#3;
      $4 = $mf->#4;
      $5 = $mf->#5;

      }

}

?>



What I'm lookin for the results to be like is a RSS / XML style feed that upon calling a url will provide the details below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
      <channel>
            <item>
                  <1>asdsadsad</1>
                  <2>fdsfdsf</2>
                  <3>gffdgdfg</3>
                  <4>dfgdg</4>
                  <5>fdfdg</5>
            </item>
            <item>
                  <1>asdsadsad</1>
                  <2>fdsfdsf</2>
                  <3>gffdgdfg</3>
                  <4>dfgdg</4>
                  <5>fdfdg</5>
            </item>
            <item>
                  <1>asdsadsad</1>
                  <2>fdsfdsf</2>
                  <3>gffdgdfg</3>
                  <4>dfgdg</4>
                  <5>fdfdg</5>
            </item>
      </channel>
</rss>
LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jbondcCommented:
It is unfortunatly not that simple...

If you're looking for pulling an XML record directly from a database, there is such a thing as an XML data type in the SQL standard but to my knowledge, it is only fully supported in oracle and DB2 with debated SQL Server 2005 (http://www.sqljunkies.com/WebLog/mrys/archive/2005/04/20/12380.aspx)

To create an RSS feed, you should query your database as you are doing but build your XML using DOM:

http://ca3.php.net/domxml


And a simple example:

$xmlDocument = new DOMDocument('1.0', 'utf-8');
$root = $xmlDocument->createElement('rss');
$root->setAttribute("version", "2.0");

$item = $xmlDocument->createElement('item');

$i1 = $xmlDocument->createElement("i1", utf8_encode("asdsadsad") );
$i2 = $xmlDocument->createElement("i2", utf8_encode("fdsfdsf") );

$item->appendChild($i1);
$item->appendChild($i2);

$root->appendChild($item);
$xmlDocument->appendChild($root);

/* send XML responce */
header('Content-Type: text/xml');
echo $xmlDocument->saveXML();


Combining this with your database...

$xmlDocument = new DOMDocument('1.0', 'utf-8');
$root = $xmlDocument->createElement('rss');
$root->setAttribute("version", "2.0");

$q = $database->Execute( $sql );

while(!$q->EOF) {
    $item = $xmlDocument->createElement('item');

   $i1 = $xmlDocument->createElement("i1", $q->fields['column_name'] );
   $i2 = $xmlDocument->createElement("i2", $q->fields['other_column_name'] );

   $item->appendChild($i1);
   $item->appendChild($i2);
   $root->appendChild($item);

      $q->MoveNext();
     }


$xmlDocument->appendChild($root);

/* send XML responce */
header('Content-Type: text/xml');
echo $xmlDocument->saveXML();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Major thanks!
Neil
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.