我想尝试一种方法,每次有新的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>
答案 0 :(得分: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>';
}
答案 1 :(得分: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>
答案 2 :(得分: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_rand,foreach