2012-02-08 43 views
5

我有一组图片广告,我想改变每天午夜的顺序。更改午夜图片的顺序

基本上这样有一天它会是这样

<img src="image1"> 
<img src="image2"> 
<img src="image3"> 
<img src="image4"> 

,第二天它看起来像这样

<img src="image4"> 
<img src="image1"> 
<img src="image2"> 
<img src="image3"> 

我怎么能做到这一点与JavaScript,jQuery的或php。不关心我使用什么语言,只需要弄清楚。谢谢..

+1

您只需将订单保存在文件/数据库中,并在每个午夜更改。任何特定的问题? – zerkms 2012-02-08 02:54:22

回答

4

试试这个http://jsfiddle.net/KQwf2/1/

HTML:

<img src="http://solarpanels2green.com/images/one.gif" title='1'> 
<img src="http://solarpanels2green.com/images/two.gif" title='2'> 
<img src="http://solarpanels2green.com/images/three.gif" title='3'> 
<img src="http://solarpanels2green.com/images/four.gif" title='4'> 

和js代码

var all = $('img'), 
     shift = Math.floor(
      (new Date().getTime() - new Date().getTimezoneOffset() * 60000) 
     /(24 * 3600 * 1000)) % all.length; 

all.filter(':gt(' + (all.length - shift - 1) + ')') 
    .insertBefore(all.first()); 

它计算自1970年1月1日午夜以来经过的天数除以图像列表中元素的数量的MOD,从列表底部获取此数量的图像并移动它们在列表前面。

更新考虑访问者的时区。

+0

美丽, 谢谢!也感谢所有其他人。其他解决方案看起来像是可以工作的,但这太容易实现了。 – Zach 2012-02-08 07:46:39

+0

@ user1166556不客气:)实际上,这个'if(shift!= 0)'也可以被删除。这是一个深夜,所以我有一个'慢思考') – Cheery 2012-02-08 07:50:55

2

如果您有权访问数据库,那么在PHP中每天将订单存储在数据库中将是一件简单的事情。然后,当页面加载时,检查订单所来自的日期,并在与当前日期不匹配时更新。更新过程将包括通过php的rand生成新订单。

如果您无法访问数据库,则需要提供一种完全基于日期的不同随机化机制。一种选择是生成日期的散列并使用它来驱动您的订购。

这里的非DB选项的一些PHP伪代码:

$fullhash = md5(date("Ymd")); 
$hash = $fullhash; 
$countImages = 4; //or whatever the actual number of images you have is 
$shownImages = array(); 
while ($countShown < $countImages) 
{ 
    $num = ord($hash); //get ascii value of first char of $hash 
    $num = $num % $countImages; //convert the number to something that corresponds to an image 
    if (!(in_array($num, $shownImages))) 
    { 
    echo "<img src='image" . $num . "'>"; 
    $shownImages[] = $num; 
    } 
    $hash = substr($hash,1); 
    if (strlen($hash) == 0) 
    { 
    $fullhash = md5($fullhash); //generate a new hash in case the previous one did not catch all images 
    $hash = $fullhash; 
    } 
} 

这可能显得过于复杂。如果您可以在服务器上始终为随机数生成设置种子,那么可以用上述代码替换大部分上述代码。然而,越来越多的实现正在从种子自己的随机数生成器中移除,这使得重复生成相同的随机数序列变得不那么简单。

0

你可以用javascript获取时间。然后,我将创建一个数组,随机顺序和输出的图像

var d = new Date(); 
var time= d.getHours(); 
if(time>=24){ 
    //code here for randomizing 
} 

http://www.w3schools.com/jsref/jsref_gethours.asp
http://www.javascriptkit.com/javatutors/randomorder.shtml

+0

如果时间<24,该怎么办? – zerkms 2012-02-08 03:27:16

+0

什么也没有,如果观众在他/她的计算机上午夜过后,你正在改变秩序 – 2012-02-08 04:30:51

+1

@tq:1.我不是OP 2.你需要再次阅读这个问题,你不明白 – zerkms 2012-02-08 06:44:55

1

这里有一个在PHP中,只有在一个给定的目录依赖于一组图像的最后修改时间:

<?php 

function cmp($a, $b){ 
    $atime = filemtime($a); 
    $btime = filemtime($b); 

    return $atime == $btime ? 0 : ($atime < $btime ? -1 : 1); 
} 

$paths = array(); 

if ($dh = opendir('images')){ 
    while (($path = readdir($dh)) !== false){ 
    if (substr($path, -4) == '.jpg'){ 
     array_push($paths, "images/$path"); 
    } 
    } 
} 

$count = count($paths); 
$offset = time() % $count; 

usort($paths, 'cmp'); 

for ($i = 0; $i < $offset; $i++){ 
    $path = array_shift($paths); 
    array_push($paths, $path); 
} 

?> 

然后,只要您需要在您的网页:

<?php foreach ($paths as $path): ?> 
    <img src="<?php echo $path; ?>" ... /> 
<?php endforeach; ?>