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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.