我有4个php文件,里面都有一个小php和jQuery游戏。
文件如下:
/game1.php
/game2.php
/game3.php
/game4.php
每次刷新页面时,我都希望其中一个游戏显示在侧边栏中。当页面再次刷新时,一个不同的游戏等等。
有没有一种方法可以通过页面刷新时的某种查询将文件随机包含在侧边栏中,如果有,请有人帮我编写代码。谢谢
$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);
尝试:
include '/game' . rand(1, 4) . '.php';
$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>";
}
$file_array= array('game1.php','game2.php','game3.php','game4.php');
$rand = rand (0, count ($file_array));
include ($file_array[$rand]);
我想你会在会话中找到答案,因为你不希望最后一次观看的游戏出现在下一页刷新中:
//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;