繁体   English   中英

用于显示数组中的一个随机元素的 Html 代码,但不应为超过 1 个用户重复

[英]Html code to display one random element from an array but should not be repeated for more than 1 user

最初有不同的代码/文本存储在一个数组中。 用户单击按钮并显示代码/文本。 重要提示:1) 必须从列表中随机选择代码。 2)代码是唯一的,不能多次显示! (例如,如果在提交表单时显示了一个代码,则下次提交表单时就不能再显示该代码了)。

*这将是一个基本的html js网站

我猜你是和我一样的学生,当编写新的东西时,可能很难开始。 这就是为什么我想分享它。

<html>
<body>
<form action="#" method="POST">
    <p> Add 5 unique numbers</p>
    <input type="number" name="0" min="0" max="100">
    <input type="number" name="1" min="0" max="100">
    <input type="number" name="2" min="0" max="100">
    <input type="number" name="3" min="0" max="100">
    <input type="number" name="4" min="0" max="100">
    <button>Send</button>
    <br> 
</body>
</html> // This is our html code. We get the numbers.
<?php
$array=$_POST; // Adding numbers to the array

if(isset($array)) // isset checks the array we set any data otherwise we get an error message
{
    $result=array_unique($array); // checks number differences
    if (count($result)<5)
    {
        echo "All numbers are not unique!";
        die(); // if numbers are same, program ends here
    }
    else{
        for($i=0;$i<5;$i++)
        {
            echo ($i+1)."."."number=".$array[0]."<br>";
            array_shift($array); // every loop it deletes the first index and prints the next one
        }
}
}
?>

最后一部分不是你需要的,你可能需要用 js 或 ajax 来做这件事。 希望它可以成为一个很好的例子。 它还帮助我做我学到的东西。 GL :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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