我有 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;