使用PHP加载多个random.html文件
我有6个html文件(test1.html、test2.html、test3.html等等),我想随机显示其中的3个,没有重复。然后,每次加载网页时,都会使用不同的顺序和文件 比如:test1.html、test4.html、test2.html 或者:test5.html、test2.html、test3.html 或者:test3.html、test6.html、test2.html 等等。。。。没有重复的 我从前面的问题中得到了这段php代码,这是一个很好的开始。它只包括1个html文件,其中我需要3个使用PHP加载多个random.html文件,php,html,select,random,no-duplicates,Php,Html,Select,Random,No Duplicates,我有6个html文件(test1.html、test2.html、test3.html等等),我想随机显示其中的3个,没有重复。然后,每次加载网页时,都会使用不同的顺序和文件 比如:test1.html、test4.html、test2.html 或者:test5.html、test2.html、test3.html 或者:test3.html、test6.html、test2.html 等等。。。。没有重复的 我从前面的问题中得到了这段php代码,这是一个很好的开始。它只包括1个html文件,
<?php
$files = glob('*.html');
$random_file = $files[array_rand($files)];
include($random_file);
?>
非常感谢您的帮助。基于以上示例
$i = 0;
while($i < 3) {
$randInt = rand(1, 6);
if(isset($used)) {
while($a < 1) {
if(array_search($randInt, $used)) {
$a++;
} else {
$randInt = rand(1, 6);
continue;
}
}
}
include_once("path/to/test$randInt.html");
$used[] = $randInt;
$i++;
}
$i=0;
而($i<3){
$randInt=兰特(1,6);
如果(isset(已使用)){
而($a<1){
if(数组搜索($randInt,$used)){
$a++;
}否则{
$randInt=兰特(1,6);
继续;
}
}
}
包括一次(“path/to/test$randInt.html”);
$used[]=$randInt;
$i++;
}
基于AD7six的想法,这更加简洁,还允许使用非数字文件名:
$files = array(
1=>'file_foo.html',
2=>'file2_bar.html',
3=>'file3_make.html',
4=>'file4_it.html',
5=>'file5_more.html',
6=>'file6_succinct.html');
shuffle($files);
for($i=0;$i<3;$i++) {
include_once("path/to/$files[$i]");
}
$files=数组(
1=>'file_foo.html',
2=>'file2_bar.html',
3=>'file3_make.html',
4=>'file4_it.html',
5=>“file5\u more.html”,
6=>'file6_success.html');
洗牌($文件);
对于($i=0;$iSorry,但你需要学习基本编程。如果你想要3个文件,那么你必须做3次。如何做取决于你自己…3次调用array_rand,或者一个循环,重复3次。今天最好的3个建议词:Google“random file php”/实际上这更像是4次,但谁在计算(?)如果我回答了你的问题,你能接受吗请+1,因为op已经投入了研究工作。当我在我的服务器上测试文件“include_once('test$randInt.html');”php代码正在寻找一个名为“test$randInt.html”的文件时,我是php的新手不是file1.html到file6.html。这是代码中的一个小问题吗?请看我的更新,我应该用双引号将它括起来。对不起,这段代码比要求的要多。例如,带有无序排列和数组pop的循环更易于读取、执行和调试。@MartynGray在循环的末尾添加$i++;AD7six不要把他弄糊涂;)谢谢保持基本的东西,这让我更容易理解。我可以看到“$I”在代码顶部有3个设置。但是底部的“$i++”循环在我的服务器上似乎不起作用,因为我只得到一个test.html文件。这里是我的php文件的链接