随机<a> displaying every time</a>

时间:2014-10-05 10:26:48

标签: 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://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页面底部有另一个游戏列表,是的,每次有人继续改变另一个...请帮助

1 个答案:

答案 0 :(得分:1)

如果您将所有游戏存储在数组中,例如

$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

}

?>