随机<;a>;每次显示


Random <a> displaying every time

基本上我有一个游戏网站,我添加了一个名为othergames的div,它看起来有点像这个

<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>

更改为另一个。。

这是我正在处理的页面http://akgames.tk/games/run2在底部有一个其他游戏列表,是的,每次有人在上面时,都会更改为不同的列表。。请帮助

如果你想把所有游戏都存储在一个数组中,比如

$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
}
?>