Php 随机每次显示
基本上我有一个游戏网站,我添加了一个名为othergames的div,看起来有点像这样Php 随机每次显示,php,html,random,Php,Html,Random,基本上我有一个游戏网站,我添加了一个名为othergames的div,看起来有点像这样 <div id='othergames'> <h2>Other Games</h2> <div class="row"> <a href="http://akgames.tk/games/run2"> <div> <img src="http
<div id='othergames'>
<h2>Other Games</h2>
<div class="row"> <a href="http://akgames.tk/games/run2">
<div>
<img src="http://akgames.tk/pictures/run2.png" width="60" height="60" alt=""> <p>Run 2</p>
</div>
</a>
<a href="http://akgames.tk/games/effingworms">
<div>
<img src="http://akgames.tk/pictures/effingworms.png" width="60" height="60" alt=""> <p>Effing Worms</p>
</div>
</a>
<a href="http://akgames.tk/games/sportsheads">
<div>
<img src="http://akgames.tk/pictures/footballheads.png" width="60" height="60" alt=""> <p>Football Heads Championship</p>
</div>
</a>
<a href="http://akgames.tk/games/floodrunner2">
<div>
<img src="http://akgames.tk/pictures/floodrunner2.png" width="60" height="60" alt=""> <p>Flood Runner 2</p>
</div>
</a>
<a href="http://akgames.tk/games/happywheels">
<div>
<img src="http://akgames.tk/pictures/happywheels.png" width="60" height="60" alt=""> <p>Happy Wheels</p>
</div>
</a>
<a href="http://akgames.tk/games/mineblocks">
<div>
<img src="http://akgames.tk/pictures/mineblocks.png" width="60" height="60" alt=""> <p>Mineblocks</p>
</div>
</a>
<a href='http://akgames.tk/games/bmxpark'>
<div>
<img src="http://akgames.tk/pictures/bmxpark.png" width="60" height="60" alt=""/> <p>BMX Park</p>
</div>
</a>
<a href='http://akgames.tk/games/soccerballs2'>
<div>
<img src="http://akgames.tk/pictures/soccerballs2.png" width="60" height="60" alt=""/> <p>Soccer Balls 2</p>
</div>
</a>
<p><br>to add: games change every time, padding on #row</p>
</div>
</p>
</div>
我想做的是在页面中添加一个php脚本,就像每次有人进入我的页面一样:
<a href="http://akgames.tk/games/effingworms">
<div>
<img src="http://akgames.tk/pictures/effingworms.png" width="60" height="60" alt=""> <p>Effing Worms</p>
</div>
</a>
更改为另一个
这是我正在处理的页面,在底部有一个其他游戏列表,是的,每次有人上它时,都会换到另一个。。如果要将所有游戏存储在一个数组中,请提供帮助,如
$gamesArray = array('effingworms','run2');
等等,您可以使用PHP的rand进行随机游戏
$maxRand = sizeof($gamesArray);
$selected = rand(0, $maxRand);
$result = $gamesArray[$selected];
<a href="http://akgames.tk/games/<?php echo $result; ?>"><img src="http://akgames.tk/pictures/<?php echo $result; ?>.png"></a>
这只是一个例子,上面的脚本没有测试语法错误,可能会生成相同的数字。也许将结果添加到一个新数组中,并在每次检查生成的值是否唯一时,都可以消除这种情况。暗示
更新
要想在每一场比赛中都有一个随机的结果,类似这样的方法应该是可行的:
<?php
$gamesArray = array('effingworms','run2','game3','game4','game5','game6','game7','game8','game9','game10');
# To get the length of the array.
$maxRand = sizeof($gamesArray)-1;
# Initialize an array - to be used later.
$selectedArray = array();
# Items to be shown per page
$items = 7;
# Perform the action '$items' times
for($i=0;$i<$items;$i++) {
# Generates a random number (which will reference one element in the array)
$selected = rand(0, $maxRand);
# This will disallow to have the same game twice
if(!in_array($selected,$selectedArray)) {
# If it is not present in the array yet, push it there
array_push($selectedArray,$selected);
} else {
# If it is already there, redo this step.
$i--; }
}
for ($j = 0; $j<sizeof($selectedArray); $j++) {
# List all selected items in HTML - this can be done easier using foreach
$current = $selectedArray[$j];
$result = $gamesArray[$current];
?>
<a href="http://akgames.tk/games/<?php echo $result; ?>"><img src="http://akgames.tk/pictures/<?php echo $result; ?>.png"></a>
<?php
}
?>
你不应该在标签中使用标签。所以是的,我这样做了,但当我添加更多标签时,所有6个标签都会显示相同的$result,就像akgames.tk/games/run2上的look一样,所有标签上都显示sportheads。我该如何做才能在每个问题上得到不同的答案?@ttyredfg我已经更新了答案,请看一看。你可能必须纠正拼写错误,因为我无法测试此代码。谢谢你,你帮了我很多,但我还有一个问题,每当我进入页面时,有时会显示2个游戏,有时会显示4个,但如果你知道我的意思,我希望它显示所有7个。因此,连续7场比赛将发生变化。请帮忙mate@ttyredfg我已经更正了plus$I++;还有一些零碎的东西。现在在我身上看起来不错,我得去测试一下。希望对你有用!