RSS 提要中的随机数 [英] Random Number in RSS feed

查看:51
本文介绍了RSS 提要中的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前的 PHP 文件:

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>

在 RSS 提要中正确显示了一个条目.但是,我需要它显示 30 个条目,每个条目都在项目中显示 NUMBER 的三个位置输入一个随机数.

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.

每个 RSS 项目都应该有一个介于 1 和 2779503 之间的不同数字,输入到三个表示 NUMBER 的地方.我知道 PHP 有 http://php.net/manual/en/function.rand.php 但我不知道该怎么做是每次加载提要时循环遍历 30 个随机数...

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

推荐答案

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

这篇关于RSS 提要中的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆