Jump to content

[SOLVED] Turning mySQL Data into xml with php


rilana

Recommended Posts

Hello all, I am trying to do a News Script in Flash using PHP, xml and a mySql-Database.

So far I am still stuck at getting the xml in the right format. I get this

 

<?xml version="1.0"?>
<news>
<item>Sommercheck und Frischluft Service!</item>
<item>Geniessen Sie die warmen Jahreszeiten und profitieren Sie von unserem Gratis-Klimaanlagecheck und viel mehr!</item>
<item>http://www.rilana.biz</item>
<item>Neues Ford-Zubehör</item>
<item>Bla Bla Bla</item>
<item>das ist ein url</item>
</news>

 

unstead of this

 

<?xml version="1.0"?>
<news>
<item>
<titel>Sommercheck und Frischluft Service!</titel>
<text>Geniessen Sie die warmen Jahreszeiten und profitieren Sie von unserem Gratis-Klimaanlagecheck und viel mehr!</text>
<link>http://www.rilana.biz</link>
</item>
</news>

 

my php code to do this looks like this:

 

while($line = mysql_fetch_assoc($results)) {
echo "<item>" . $line["titel"] . "</item>\n"; 
echo "<item>" . $line["text"] . "</item>\n"; 
echo "<item>" . $line["link"] . "</item>\n"; 
}

 

I tryed to change it and put my text and link in between the item's, but I always get errors and I have no clue how to write it out right. I realy would aprechiate some help or hints.. thank you verry much, Rilana

	echo "<item>" . $line["titel"] . "</item>\n"; 
echo "<item>" . $line["text"] . "</item>\n"; 
echo "<item>" . $line["link"] . "</item>\n";

 

Should be:

 

             echo "<item>\n";
echo "<title>" . $line["titel"] . "</title>\n"; 
echo "<text>" . $line["text"] . "</text>\n"; 
echo "<url>" . $line["link"] . "</url>\n";
             echo "</item>\n";

Or:

 

echo "<item>\n";
echo "<title>{$line["titel"]}</title>\n"; 
echo "<text>{$line["text"]}</text>\n"; 
echo "<url>{$line["link"]}</url>\n";
echo "</item>\n";

 

And last but not least, there are specialised functions for XML output.

For example: http://www.php.net/manual/en/book.xmlwriter.php

Here's a general-purpose function

 

<?php
function sql2xml ($sql, $root = 'root')
{
    $res = mysql_query($sql);
    $xml = "<?xml version=\"1.0\"?>\n<$root>\n";
    while ($row = mysql_fetch_assoc($res))
    {
        $xml .= "<item>\n";
        foreach ($row as $k=>$v)
        {
            $xml .= "\t<$k>$v</$k>\n";
        }
        $xml .= "</item>\n";
    }
    $xml .= "</$root>";
    
    return $xml;
}

$sql = "SELECT * FROM mytablename";

echo sql2xml($sql, 'news');
?>

thanks a lot guyes, both of them work. But I understand the first one better.... Barand i wished my brain would work like yours, everytime I feel like I learned someting, I end up realizing I know nothing..... anyway I am trying to figure out the actionsscript now, I have gone thrue so many tutorials and it seems like everybody does it different. And I dont get some things. Does anyone has a good tutorial on how to use xml in flash, my bigest thing I dont understand is if lets say i have 2 news, but I only wanna show about 1 sentens of each news, how do I connect the sentence to the whole news.... ???? Must be with making movieclips somehow... anyway if anyone has a good tutorial for me, I would aprechiate it. Thanks, Rilana

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.