题
我想在我的网站上显示一个简单的推荐旋转器。
PHP可以从文本文件或DB中获取证词,但我不明白如何创建旋转器部分。
感谢您提供的任何帮助。谢谢。
解决方案
要从DB中获取它,您会在SQL中做类似的事情:
SELECT testimonial FROM testimonials ORDER BY RAND() LIMIT 1
要从文本文件中获取它,您会做这样的事情:
// load the file's contents
$testimonials = file_get_contents('text_file.txt');
// split the list by new lines, i.e. one testimonial per line
$testimonials = explode("\n", $testimonials);
// print a random testimonial
print $testimonials[rand(0, (count($testimonials) - 1))];
其他提示
如果您希望它们更新Live,则必须使用JavaScript或JQuery之类的框架。否则,@ceejayoz提供了一个完美的答案。
您可以使用rand()的订单选择随机记录并显示它们,或者如果您想顺序显示它们,请跟踪显示的ID,然后从第一个记录到达结束时。
不隶属于 StackOverflow