随机选择推荐书

时间:2011-12-14 20:50:50

标签: php

$ message似乎没有显示任何内容。

我有以下代码:

<?php
$files = glob("testimonials/*.txt");
$filename = $files[rand(0, count($files)-1)];
$lines = file($filename);
$author = array_shift($lines);
$message = explode("", $lines);
?>

<h1>Testimonial</h1>
<p><b>Author:</b> <?php echo $author; ?></p>
<?php echo $message; ?>

在我的推荐文件夹中,我有第一行的作者姓名的文本文件,然后是直接在其下的行上的消息。

为了使这项工作正常,我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php
$files = glob("testimonials/*.txt");
                 //mt_rand is more random
$filename = $files[mt_rand(0, count($files)-1)];

$lines = file($filename);
//Get authors name
$author=$lines[0];
/*Glue the whole $lines array into a string & remove the author,
  so whats left is just the message*/
$message=str_replace($author,'',implode(' ',$lines));
?>

<h1>Testimonial</h1>
<p><b>Author:</b> <?php echo $author; ?></p>
<?php echo $message; ?>

答案 1 :(得分:0)

这是我使用的,但我还没有很多推荐!

function getTestimonial(){
// testimonials for Stay-in-Touch.ca    
$q = array ();
$q[] = " I was very impressed by your gadget - Grandma used it to show me
            loads of new photos of her latest batch of great-grandchildren - it's
            just the thing for her.<br>Philip Exeter, UK ";
$q[] = "The best time for the tablet was when my mom was in the hospital for three weeks before 
    and after her procedure. J went all around my mom&#39;s house, taking pictures of all her plants with
    her iPhone (many many plants, and my mom loves them all) and uploading them to the tablet. My mom 
    would just lay in her bed and see how her plants were doing (comments like &#39;oh, it&#39;s flowering 
    this year!&#39;), and it would calm her down. <br> M Toronto";
$q[] = "Mum really likes the pictures of the new babiy in the family. She can see how fast he is growing.<br> J Montreal";
$thisOne = rand(0,count($q)-1);
return $q[$thisOne];

}