2011-12-14 122 views
0

$消息似乎沒有顯示任何內容。隨機選擇的推薦書

我有以下代碼:

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

在我的推薦文件夾,我的文本文件與作者名字一個第一線,然後直接在該行的消息。

我在這裏錯過了什麼讓這個工作正常?

+0

您的移動/刪除第一行,你說這就是作者的名字,是在第二行`$ message`的不換行的其餘部分?你可以使用`$線[0]`作者姓名,然後`implode($ lines)`到字符串中使用str_replace()從作者信息中刪除作者 – 2011-12-14 21:00:09

+0

作者姓名在第一行,其餘的信息在第二行;沒有換行符,沒有額外的空間。讓我試試你的答案。 – gstricklind 2011-12-14 21:13:18

回答

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

}