Search code examples
phpdice

I have a simple php script for throwing dices, but all dices have the same number


I tried to make a dice script in php that should look like this one: http://u11626.hageveld2.nl/po/opdracht1b/index.php this is the link to mine: http://u10511.hageveld2.nl/po/opdracht1b/index.php Sorry if the answer is really obvious but i just can't figure out why the script doesn't work. btw: "knop" means "button",and the pictures that I use as dices are called 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>
";
    }
}

?>


Solution

  • i have written what i consider an optimal script for this dice throwing exercise you are doing. I am giving you all the answers here but hopefully you will research my approach and learn from it.

    <?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>";
    }
    
    ?>
    

    To add a new dice simply increase the size of the $dice array. For example the next one would be:

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

    and then 4, and so on.

    I hope this helps.