2012-04-04 38 views
0

我有这个脚本,但它不工作,不知道如何解决它,请提供任何提示?我得到了另一端是字母 “t”简单的随机生成器脚本不工作

谢谢:)

<?php 
$cachefile = './current_t_id'; 
$time = $id = null; // assume we have no cached quote 
$change_every = 3600; // seconds 
$pages = array(1 => 'text1-1.php', 2 => 'text1-2.php'); 

foreach($pages as $pagekey => $page){ 
    if($pagekey == $siteId){ 
     include($page); 
    } 
} 

// if cached quote exists, load it 
if(is_file($cachefile)) { 
    list($time, $id) = explode(' ', file_get_contents($cachefile)); 
} 

// if no cached quote or duration expired, change it 
if(!$time || time() - $time > $change_every) { 
    srand ((double) microtime() * 100000); 
    $id = rand(0,count($page)-1); 
    file_put_contents($cachefile, time().' '.$id); // update cache 
} 

// echo the quote, be it cached or freshly picked 
echo ($page[$id]); 
?> 
+2

嘿错误的举动。这是你的副本最后的帖子.. http://stackoverflow.com/questions/10011620/random-quotes-script-on-a-timer我不这是一个明智的举动 – Baba 2012-04-04 15:07:09

+0

这不是一个复制美食 – GibsonFX 2012-04-04 15:08:48

+0

我已经添加了最后一篇文章中提出的建议,但我无法让他们一起工作,所以我在寻求帮助,请不要在检查事实之前指责我。 – GibsonFX 2012-04-04 15:10:09

回答

1

OK我要再给你3个不同的例子

变量

$quotes = array (
     "hello", 
     "baba", 
     "luke" 
); 


$pages = array(1 => 'text1-1.php', 2 => 'text1-2.php'); 

A.使用随机引用

// Using Ramdom Quptes 
$key = array_rand ($quotes); 
echo $quotes [$key]; 
//Or 
include($pages[$key]) ; 

B.使用罗宾

// Using Robin 
$cacheFile = "robin.cache"; 
$robin = null; 
$quotesID = intval (file_get_contents ($cacheFile)); 
$totalQuotes = count ($quotes); 

$key = ($quotesID < ($totalQuotes - 1)) ? $quotesID ++ : 1; 
file_put_contents ($cacheFile, $quotesID); 

echo $quotes [$key]; 
//Or 
include($pages[$key]) ; 

使用定时器

// Using Timer 

$cacheFile = "timer.cache"; 
$expiration = 3600; 

$robin = null; 
list($quotesID, $time) = explode(' ', file_get_contents($cacheFile)); 
$totalQuotes = count ($quotes); 

if($time < (time() - $expiration)) 
{ 
    $key = mt_rand(0,count($pages)-1); 
    file_put_contents($cacheFile, time().' '.$id); 
} 
echo $quotes [$key]; 
//Or 
include($pages[$key]) ; 

我希望您的问题,现在可以解决

感谢 :)

+0

好的,我会付出这个,谢谢:) – GibsonFX 2012-04-04 15:30:35

+0

很高兴知道你已经解决了这个问题。 – Baba 2012-04-04 15:36:39

+0

我以前对我们的误会表示抱歉。我的错 – GibsonFX 2012-04-04 15:38:06