Random Number in RSS feed

雨燕双飞 提交于 2019-12-11 15:24:12

问题


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


回答1:


<?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; ?>


来源:https://stackoverflow.com/questions/13547569/random-number-in-rss-feed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!