在数组中使用随机变量

时间:2016-05-11 00:01:24

标签: php html arrays

首先,对不起,如果措辞不好,因为我是代码的初学者。

我目前正在从事在线计算机科学课程,但是我对如何做一小部分感到很困惑。我们需要为此活动使用数组,其中用户有多个选择,每个不同的选择具有不同/唯一的文本输出。一切正常,除了我需要一个选择随机选择的选项,但是我对如何制作它有点困惑。您可以从我的代码中看到选项1-8。我希望它随机选择其中一个选项。

这是我的代码:

<?php
$train[0] = "Canada";
$train[1] = "Sahara Desert";
$train[2] = "Russia";
$train[3] = "Chernobyl";
$train[4] = "United States";
$train[5] = "North Korea";
$train[6] = "Germany";
$train[7] = "Hawaii";
?>

<!DOCTYPE html>
<html>
<head>
Took out everything here, it's not important.
</head>

    <body>
        <center>
    <h1>Vacation Time!</h1>

    <h4>You and your family just won the lottery! You all want to go on vacation, but nobody can agree where to go. Inside each train cart has a card with a location written on it. Whatever you find is where you're going! </h4>

        <form name="form1" action="activity-2-7-arrays-a.php" method="post">

            <label>Which cart on the train do you want to choose?</label>
            <br>
            <select name="cart" required>
                <option value="1">First Cart</option>
                <option value="2">Second Cart</option>
                <option value="3">Third Cart</option>
                <option value="4">Fourth Cart</option>
                <option value="5">Fifth Cart</option>
                <option value="6">Sixth Cart</option>
                <option value="7">Seventh Cart</option>
                <option value="8">Eight Cart</option>
                <option value="show">Show all options</option>
                <option value="any">Choose Randomly</option>
                <br>
            </select><br/>
            <input type="submit" name="subButton" class="subButton" value="Go!"/><br/>
            </form>

    <h1><u>Final Results</u></h1>

<?php
if($_POST['subButton']) {
    $cart = $_POST['cart'];
    $roll = rand(1,9);

    if($cart == show) {
        for($x = 1; $x <= 9; $x++) {
            echo "<p> You could have ender up in... </p>";
            echo "<h2> " . $train[$x] . "</h2>";
        }
        return;
    }
    echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
}
return;

if ($cart == $roll) {

}
echo "<h2>"."Can't handle the pressure? You were selected to go to  " . $train[$roll] . "! Have fun! </h2>";
?>

我也确定它有点乱。希望你明白我的意思。如果你能够向我解释这个非常有用的答案。谢谢:))

4 个答案:

答案 0 :(得分:3)

无论用户选择如何,您都会随机生成一个值,并将其与用户的选择和其他奇怪的事物进行比较。

<?php
if($_POST['subButton']) {
    $cart = $_POST['cart'];
    $roll = rand(1,9); 

在检查用户是否选择&#39;随机选择&#39;之前,您正在生成随机值。为什么产生1到9之间的随机?您的$train数组以索引0开头,以索引7结束。

    if($cart == show) { 

需要引用字符串。

        for($x = 1; $x <= 9; $x++) {

再次将$x从1循环到9是没有意义的,因为你的数组索引。

            echo "<p> You could have ender up in... </p>";
            echo "<h2> " . $train[$x] . "</h2>";

$x中的最后一个索引为7以来$train达到8时,将失败。

        }
        return;
    } 
    echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
    }
    return;

因此,如果用户没有选择显示所有选项&#39;你告诉他他所选择的位置。如果用户选择&#39;随机选择&#39;这将失败,因为$cart具有价值&#39;任何&#39;并且$train['any']不存在。

这是具有正确逻辑的代码。

<?php
if($_POST['subButton']) {
    $cart = $_POST['cart'];
    if ($cart == 'any') {// Check if user selected 'Choose Randomly'
        $roll = rand(0, 7);
        echo "<h2>"."Can't handle the pressure? You were selected to go to  " .  $train[$roll] . "! Have fun! </h2>";
    }
    else {
        if ($cart == 'show') { // If user selected 'Show all options'
            echo "<p> You could have ender up in... </p>";
            for($x = 0; $x <= 7; $x++) {
                echo "<h2> " . $train[$x] . "</h2>";
            }
        }
        else { // User selected cart so show him chosen location
            echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
        }
    }
}
?>

答案 1 :(得分:2)

以下是代码中的一些问题

  1. 从哪里以及为什么我们return
  2. 没有关闭html-tags
  3. 我会像那样改变php代码的最后一部分:

    <?php
    if ($_POST['subButton']) {
        $cart = $_POST['cart'];
        $value = intval($cart);
        $roll = mt_rand(1, 8); // mt_rand() has more entropy than rand()
    
        if ($cart == 'show') {
            // show target locations (according the OP-source)
            for($x = 1; $x <= 8; $x++) {
                echo "<p> You could have ender up in... </p>";
                echo "<h2> " . $train[$x-1] . "</h2>";
            }
        } else if ($value > 0 && $value <= 8) {
            echo "<h2>"."Well, it looks like you're going to " . $train[$value-1] . "! Have fun! </h2>";
            // idk why it needed, but move it here
            if ($value == $roll) {
            }
        } else if ($cart == 'any') {
            echo "<h2>"."Can't handle the pressure? You were selected to go to  " . $train[$roll-1] . "! Have fun! </h2>";
        } else {
            // $_POST['cart'] has something wrong
        }
    }
    ?>
    <!-- lines below was added to close html-tags -->
    </body>
    </html>
    

答案 2 :(得分:1)

您要找的是array_rand()

$random = array_rand($train);

答案 3 :(得分:1)

而不是rand()使用mt_rand(),因为PHP DOCUMENTATION字面上说

  

mt_rand - 生成更好的随机值

关于你的php代码,你有很多错误。这是你的底层PHP代码应该是这样的:

<?php
  if($_POST['subButton']) 
  {
    $cart = $_POST['cart'];
    $roll = mt_rand(0,7);
       //0-7 because those are the values you inserted into
       //the $train[] array

    if($cart == 'show') {
      //another correction here, it has to be 0-7
      for($x = 0; $x <= 7; $x++) {
        echo "<p> You could have ender up in... </p>";
        echo "<h2> " . $train[$x] . "</h2>";
      }
    } 
    else if ($cart!='any')
    {
       "<h2>Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
    }
    else //took out return and placed an else
    {
        echo "<h2>Well, it looks like you're going
        to " . $train[$cart] . "! Have fun! </h2>";
    }

?>