我有4个php文件,里面都有一个小的PHP和jQuery游戏。
文件如下:
/game1.php
/game2.php
/game3.php
/game4.php
每次刷新页面时,我都希望其中一个游戏显示在侧边栏中。当页面再次刷新时,不同的游戏等等。
有没有办法通过页面刷新时的某种查询随机包含侧边栏中的文件,如果是这样,有人可以帮我解决这些问题。谢谢!
答案 0 :(得分:4)
$games = array('game1.php','game2.php','game3.php','game4.php');
session_start();
$used = array();
if (isset($_SESSION['used'])){
$used = $_SESSION['used'];
}
$usable = array_diff($games,$used);
if (sizeof($usable)==0){
$usable = $games;
$_SESSION['used'] = array();
}
$inUse = $usable[array_rand($usable)];
$_SESSION['used'][] = $inUse;
include($inUse);
答案 1 :(得分:3)
尝试:
include '/game' . rand(1, 4) . '.php';
答案 2 :(得分:1)
$FileNames = array ('File1.php','File2.php','File3.php','File4.php'); // Can later be autodetected if file structure gets too big
$FileNames = array_rand($FileNames);
foreach ($FileNames AS $Links){
echo $Links;
}
如果您希望点击这些内容:
foreach ($FileNames AS $Links){
echo "<a href=".$Links.">".$Links."</a>";
}
答案 3 :(得分:1)
$file_array= array('game1.php','game2.php','game3.php','game4.php');
$rand = rand (0, count ($file_array));
include ($file_array[$rand]);
答案 4 :(得分:1)
我认为你会在会话中找到你的答案,因为你不希望最后一次观看的游戏出现在下一页刷新:
//Fill in array
$games = array('game1' => 'PONG', 'game2' => 'Donkey Kong', 'game3' => 'Patience', 'game4' => 'LoL');
//Delete the current game from the array
unset($games[$_SESSION['currentGame']]);
//Shuffle the array and pick the last one
$game = end(shuffle($games));
include($game.'.php');
//Update session for next page refresh
$_SESSION['currentGame'] = $game;