Jump to content

csv,loop,xml


shage

Recommended Posts

i have a text file that is csv, it goes title,url,description

 

Im trying to break it apart and loop it to make a xml

 

<?xml version="1.0" ?>
<rss version="2.0">
  <channel>
    <title>test</title>
    <link>http://www.test.com/</link>
    <description>test</description>
    <item>
       <title>Title</title>
       <link>url</link> 
       <description>descrip</description>
    </item>
  </channel>
</rss>

 

that is the code i have for the xml file, now i need help trying to loop the item part by taking and breaking the line by line down in the text file thank you

Link to comment
https://forums.phpfreaks.com/topic/95444-csvloopxml/
Share on other sites

quick simple example

<?php
$handle = fopen("test.csv", "r");

echo "<?xml version=\"1.0\" ? >"; //remove space after ? (added for make it format in here)
echo "<rss version=\"2.0\">";
echo "  <channel>";
echo "    <title>test</title>";
echo "    <link>http://www.test.com/</link>";
echo "    <description>test</description>";

$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
    for ($c=0; $c < $num; $c++)
    {
        $title = $data[0];
        $link = $data[1];
        $description= $data[2];
        echo "    <item>";
        echo "       <title>$title</title>";
        echo "       <link>$link</link> ";
        echo "       <description>$description</description>";
        echo "    </item>";
    }
}
fclose($handle);

echo "  </channel>";
echo "</rss>";
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/95444-csvloopxml/#findComment-488654
Share on other sites

oops

updated

<?php
$handle = fopen("test.csv", "r");

echo "<?xml version=\"1.0\" ? >\n"; //remove space after ? (added for make it format in here)
echo "<rss version=\"2.0\">\n";
echo "  <channel>\n";
echo "    <title>test</title>\n";
echo "    <link>http://www.test.com/</link>\n";
echo "    <description>test</description>\n";

$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$title = $data[0];
$link = $data[1];
$description= $data[2];
echo "    <item>\n";
echo "       <title>$title</title>\n";
echo "       <link>$link</link>\n";
echo "       <description>$description</description>\n";
echo "    </item>\n";
}
fclose($handle);

echo "  </channel>\n";
echo "</rss>\n";
?>

 

REMEMBER the space on line 3

 

my csv file

a1,b1,c1

a2,b2,c2

 

Link to comment
https://forums.phpfreaks.com/topic/95444-csvloopxml/#findComment-488729
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.