0

我正在尝试一种方法,每次有新的 cookie 集或每次用户登录和注销时,我都可以从文件夹中的大约 100 张图像中显示 5 张图像。我已经尝试过使用 javascript 根据星期几显示不同的图像,但是我真正想要的是一次显示 5 张图像的方法,附带的链接每次用户登录和注销时都会改变?有人可以指出我的写作方向吗,我是 php 新手,所以即使是最小的代码示例也会有所帮助,谢谢。

这是我的 javascript 代码,它显示基于当天的图像,我不知道这是否有帮助?

<script type="text/javascript" language="JavaScript">
var now = new Date ();
var day = now.getDay();
//the week begins on SUNDAY
if(day == 0) document.write("<p><a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad0.png' alt='sunday' width='180' height='180' border='0'></a></p><p><a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad0.png' alt='sunday' width='180' height='180' border='0'></a></p>");
if(day == 1) document.write("<a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad1.png' alt='monday' width='180' height='180' border='0'></a>");
if(day == 2) document.write("<a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad2.png' alt='tuesday' width='180' height='180' border='0'></a>");
if(day == 3) document.write("<a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad3.png' alt='wednesday' width='180' height='180' border='0'></a>");
if(day == 4) document.write("<a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad4.png' alt='thursday' width='180' height='180' border='0'></a>");
if(day == 5) document.write("<a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad5.png' alt='friday' width='180' height='180' border='0'></a>");
if(day == 6) document.write("<a href='YOUR-LINK-HERE'><img src='../PTB1/data/adverts/ad6.png' alt='saturday' width='180' height='180' border='0'></a>");
</script>
4

3 回答 3

1

如果要显示一组已知图像中的 5 张随机图像,这是最好的方法

$imagepool=array('path1.jpg','path2.jpg', ..... ,'path100.jpg');
for($i=0;$i<5;$i++){
  $randid=floor(rand(0,100));
  echo '<a href="#"><img src="'.$imagepool[$randid].'"></a>';
}

或者,如果您在所有这些图像上都有该命名方案并且它们是有序的,那么您不必定义图像列表:

for($i=0;$i<5;$i++){
  $randid=floor(rand(0,100));
  echo '<a href="#"><img src="/path/to/image_'.$randid.'.jpg"></a>';
}
于 2012-11-02T16:44:13.710 回答
0
<script type="text/javascript" language="JavaScript">
var now = new Date ();
var day = now.getDay();
var images = new Array(7);
var weekday=new Array(7);
images[0] = '../PTB1/data/adverts/ad0.png';
images[1] = '../PTB1/data/adverts/ad1.png';
images[2] = '../PTB1/data/adverts/ad2.png';
images[3] = '../PTB1/data/adverts/ad3.png';
images[4] = '../PTB1/data/adverts/ad4.png';
images[5] = '../PTB1/data/adverts/ad5.png';
images[6] = '../PTB1/data/adverts/ad6.png';
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
document.write("<p><a href='YOUR-LINK-HERE'><img src='"+images[day]+"' alt='"+weekday[day]+"' width='180' height='180' border='0'></a></p>");
</script>
于 2012-11-02T16:59:19.570 回答
0

我认为您也可以使用date()strtotime()在 PHP 中完成五个链接/图像:

<?php
$today = strtotime("today");
$today = date('w',$today);
if ($today==0) echo '<a...><img.../></a>';
if ($today==1) echo '<a...><img.../></a>';
//etc...

如果您希望它是随机的,我相信您可以在登录时在 PHP 中设置一个$_SESSION变量,该变量将保存五个图像作为变量。如果他们注销并再次登录,变量会发生变化,您将拥有五个新图像/链接(无论您要查找什么)。

这是你可以做的。创建一个 php 变量文件和一个用于处理登录的 php 文件:

<?php
    //variables page
    $your_link_array = array("GOOGLE" => "http://www.google.com","Stackoverflow" => "http://www.stackoverflow.com", "Youtube" => "http://www.youtube.com"); //three as an example, but you should make a lot if you want to pull five each time.
    ?>

    <?php 
    //log in page

    if($login=='success'){ //check username/password etc.
      session_start(); //log in successful
      $_SESSION['links'] = array_rand($your_link_array, 5); //pull 5 random variables from your array of links
    }
    ?>

    <?php
    //any other page that has session_start() at the beginning:
    session_start()
    foreach($_SESSION['links'] as $k => $v){ // show the five random links
      echo '<a href="'.$k.'" />'.$v.'</a>';
    } 
    ?>

其他资源:array_randforeach

于 2012-11-02T16:59:33.827 回答