-4

帮助伙计们

<ul>
<li><p>“I should say upfront that I have never been in a cellar in my life. In fact, I can see no reason why anyone should ever go into a cellar unless there is wine involved.”&lt;/p>
<span class="author">~Rachel Hawkins, Hex Hall</span></li>
<li><p>“There is truth in wine, but you never see it listed in the ingredients on the label”&lt;/p><span class="author">~Josh Stern</span></li>
<li><p>Wine is bottled poetry.</p><span class="author">~Robert Louis Stevenson</span></li>
</ul>

只是想在每次页面刷新或加载时显示 1 个随机列表

4

2 回答 2

2

这可以通过使用 PHP数组array_rand() 函数轻松完成。这是一个简单的例子:

<?php
 $quote_list = array("<li>Quote 1</li>", "<li>Quote 2</li>", "<li>Quote 3</li>");
  $rand_quote = $quote_list[array_rand($quote_list)];
   echo $rand_quote;
?>

由于 PHP 只在页面加载时执行,因此每次刷新页面时,都会随机生成一个新的数组值并从 $quote_list 数组中打印出来。

于 2013-01-30T00:13:06.630 回答
0
$array = array('I should say upfront that I have never been in a cellar in my life. In fact, I can see no reason why anyone should ever go into a cellar unless there is wine involved.”&lt;/p><span class="author">~Rachel Hawkins, Hex Hall</span></li>',
'<p>“There is truth in wine, but you never see it listed in the ingredients on the label”&lt;/p><span class="author">~Josh Stern</span></li>');

$rand = rand( 0, count( $array));
echo $array($rand);
于 2013-01-29T23:59:22.267 回答