我有一个简单的PHP脚本用于掷骰子,但所有骰子都有相同的数字

时间:2016-01-15 16:34:46

标签: php dice

我试图在php中创建一个骰子脚本,看起来像这样: http://u11626.hageveld2.nl/po/opdracht1b/index.php 这是我的链接: http://u10511.hageveld2.nl/po/opdracht1b/index.php 很抱歉,如果答案非常明显,但我无法弄清楚脚本无法正常工作的原因。 顺便说一句:" knop"意思是"按钮",我用作骰子的图片叫做dobbelsteen1,dobbelsteen2 ..... dobbelsteen 6

<?php  
session_start();
if (!isset($_SESSION["d1"]) || !isset($_SESSION["d2"]) || !isset($_SESSION["d3"])) {
    $_SESSION["d1"]=0;
    $_SESSION["d2"]=0;
    $_SESSION["d3"]=0;
}
elseif (isset($POST["knop1"])) {
    $_SESSION["d1"]=0;
}
elseif (isset($POST["knop2"])) {
    $_SESSION["d2"]=0;
}
elseif (isset($POST["knop3"])) {
    $_SESSION["d3"]=0;
}
elseif (isset($POST["knop4"])) {
    $_SESSION["d1"]=0;
    $_SESSION["d2"]=0;
    $_SESSION["d3"]=0;
}
echo "d1 =" . $_SESSION["d1"];
echo "d2 =" . $_SESSION["d2"];
echo "d3 =" . $_SESSION["d3"];
if ($_SESSION["d1"]==0) {
    $f = rand(1,6);
}

if ($_SESSION["d2"]==0) {
    $g=rand(1,6);
}

if ($_SESSION["d3"]==0) {
    $h=rand(1,6);
}
    echo $f;
for ($r=1; $r<4; $r++) {
    if (!$f==0) {
        $f = 0;
        echo "
    <div align='center'>
        <img src='dobbelsteen" . $f . ".gif'>
        <form method='post'>
        <input type='submit' name='knop1' value='Dobbelsteen gooien'>
        </form>
";
    }
    elseif (!$g==0) {
        $g = 0;
        echo "
    <div align='center'>
        <img src='dobbelsteen" . $g . ".gif'>
        <form method='post'>
        <input type='submit' name='knop2' value='Dobbelsteen gooien'>
        </form>
";
    }
    elseif (!$h==0) {
        $h = 0;
        echo "
    <div align='center'>
        <img src='dobbelsteen" . $h . ".gif'>
        <form method='post'>
        <input type='submit' name='knop3' value='Dobbelsteen gooien'>
        </form>
";
    }
}

&GT;

2 个答案:

答案 0 :(得分:1)

我写过我认为这是你正在做的骰子投掷练习的最佳脚本。我在这里给你所有的答案,但希望你能研究我的方法并从中学习。

<?php

//Start the php session
session_start();

//Initialise local dice array
//Check the session variable for already set values, if not set a random value
//Use TERNARY OPERATORS here to avoid multipl if else
$dice = array(
    0 => (!empty($_SESSION['dice'][0])) ? $_SESSION['dice'][0] : rand(1, 6),
    1 => (!empty($_SESSION['dice'][1])) ? $_SESSION['dice'][1] : rand(1, 6),
    2 => (!empty($_SESSION['dice'][2])) ? $_SESSION['dice'][2] : rand(1, 6)
);

//If form has been submitted, and our expected post var is present, check the dice we want to role exists, then role it
//$_POST['roll_dice'] holds the index of the local dice value array element we need to update
if(!empty($_POST['roll_dice']) && !empty($dice[intval($_POST['roll_dice'])])){
    $dice[intval($_POST['roll_dice'])] = rand(1, 6);
}

//Save the updated values to the session
$_SESSION['dice'] = $dice;

//Loop over the dice and output them
foreach($dice as $dice_index => $dice_val){
    echo "<div class='dice' style='height:100px;width:100px;background-color:red;text-align:center;margin-bottom:50px;padding-top:10px;'>";
    echo "<p style='style='margin-bottom:20px;'>".$dice_val."</p>";
    echo "<form method='post'>";
    echo "<input type='hidden' name='roll_dice' value='".$dice_index."' />";
    echo "<input type='submit' value='Roll Dice' />";
    echo "</form>";
    echo "</div>";
}

?>

要添加新骰子,只需增加$ dice数组的大小即可。例如,下一个是:

3 => (!empty($_SESSION['dice'][3])) ? $_SESSION['dice'][3] : rand(1, 6)

然后是4,依此类推。

我希望这会有所帮助。

答案 1 :(得分:0)

你的脚本有几个问题,因为@Marc B已经说过&#34;你永远不会将随机数保存回会话&#34;你也使用$ POST访问帖子数据,因为它应该是$ _POST,希望这可以帮助你修复脚本。