简体   繁体   中英

How can I save my previous dice rolls when I refresh the page?

So I have to roll 6 six sided die and display their corresponding images. I also have to save up to 10 previous roles and append the new ones each time I roll. I'm so far able to create the array of 6 random numbers and link them to their images. I just need to save that role on page refresh. I've just started learning PHP but from what I've gathered I know sessions are needed. All the examples I looked at didn't help.

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dice Roller</title>
</head>
<style>
    h1, input {
        margin-left: auto;
        margin-right: auto;
    }

    img {
        width: 70px;
        height: 70px;
    }
</style>
<body>
    <h1>Dice Roller</h1>
    <input type="button" value="Roll">

    <?php 
        $newRoll = array(rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6));
        $oldRoll = $newRoll;
        
        foreach($newRoll as $num) {
            echo '<img src="img/dicefaces/dice0' . $num . '.png" alt="">';
        }

        echo "<br>";

        foreach($oldRoll as $num) {
            echo '<img src="img/dicefaces/dice0' . $num . '.png" alt="">';
        }
    ?>

</body>
</html>

You can use $_SESSION to 'store' the old rolls, following logic might help you on your way:

We store the history in $_SESSION['oldrolls'] allowing us to display up to 10 previous rolls (you can set the number you want to show with $max )

Also created a button to allow for session clear, to start with a clean slate.

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dice Roller</title>
</head>
<style>
    table, td, th {
        table-layout: fixed;
        width: 100%;
        border-collapse: collapse;
        border: 3px solid black;
        text-align: center;
    }
    h1, input {
        margin-left: auto;
        margin-right: auto;
    }

    img {
        width: 70px;
        height: 70px;
    }
</style>
<body>
<h1>Dice Roller</h1>
<!-- allow user to clear the session -->
<form  action='' method ='POST'>
    <input type="submit" name='clear' value="clear session">
</form>
<!-- submit a roll -->
<br />
<form  action='' method ='POST'>
    <input type="submit" name='submit' value="Roll">
</form>


<?php
if(isset($_POST['clear'])){
    $_SESSION = []; // clear session array
}
if(isset($_POST['submit'])) {
    $newRoll = array(rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6));
    $max = 10; // store up to 10 previous rolls
    $count = isset($_SESSION['oldrolls']) ? count($_SESSION['oldrolls']) : 0;
    if($count <= $max) {
        $_SESSION['oldrolls'][] = $newRoll; // add new
    } else {
        array_shift($_SESSION['oldrolls']); // remove oldest
        $_SESSION['oldrolls'][] = $newRoll; // add new
    }
    $_SESSION['oldrolls-reverse'] = array_reverse($_SESSION['oldrolls']); // show last first

    echo 'Current roll...';
    echo '<br />';
    foreach ($newRoll as $num) {
        echo $num;
        echo '<br />';
    }

    echo "<br>";

    echo 'previous rolls...';
    echo '<br />';
    echo '<table>';
    foreach ($_SESSION['oldrolls-reverse'] as $key => $num) {
        if($key === 0) continue;
        echo '<tr>';
        foreach($num as $roll) {
            echo '<td>' . $roll . '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
}
?>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM