Search code examples
phpxmlrssatom-feed

Random Number in RSS feed


This is what I currently have for my PHP file:

<?php header("Content-type: text/xml"); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>
<rss version="2.0">
<channel>
  <title>My Website</title>
  <link>http://www.mywebsite.com</link>
  <description>The Title of My Website</description>
  <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>

  <item>
    <title>Website Directory - Page NUMBER</title>
    <pubDate><?echo date('Y/m/d H:i:s');?></pubDate>
    <link>http://www.mywebsite.com/directory/NUMBER</link>
    <description>New update to page NUMBER in the Website Directory.</description>
  </item>

</channel>
</rss>

That right there is correctly showing one entry in the RSS feed. However, I need it to show 30 entries, each with a random number entered in the three places where NUMBER is shown in the item.

Each RSS item should have a different number between 1 and 2779503 entered in the three places that say NUMBER. I know that PHP has http://php.net/manual/en/function.rand.php but what I don't know how to do is have it loop through 30 random numbers each time the feed is loaded...


Solution

  • <?php
        foreach( range( 1, 30 ) as $i ):
            $number = mt_rand( 1, 2779503 );
    ?>
    <item>
        <title>Website Directory - Page <?php echo $number; ?></title>
        <pubDate><?echo date('Y/m/d H:i:s');?></pubDate>
        <link>http://www.mywebsite.com/directory/<?php echo $number; ?></link>
        <description>New update to page <?php echo $number; ?> in the Website Directory.</description>
    </item>
    <?php endforeach; ?>