首先,如果我的措辞不佳,请见谅,因为我是代码初学者。
我目前正在学习一个在线计算机科学类(class),但是我对如何完成一小部分感到很困惑。我们需要为此事件使用数组,其中用户有多个选择,每个不同的选择都有不同/唯一的文本输出。一切正常,除了我需要一个选择随机选择的选项,但我对如何制作它有点困惑。您可以从我的代码中看到选项 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>";
?>
我敢肯定它也有点乱。希望你明白我的意思。如果您能够向我解释答案,那将非常有帮助。谢谢:)
最佳答案
无论用户选择如何,您都会随机生成一个值,并将其与用户的选择和其他奇怪的事情进行比较。
<?php
if($_POST['subButton']) {
$cart = $_POST['cart'];
$roll = rand(1,9);
您甚至在检查用户是否选择了“随机选择”之前就生成了一个随机值,为什么要生成 1 到 9 之间的随机值?您的 $train
数组以索引 0 开始,以索引 7 结束。
if($cart == show) {
字符串需要用引号引起来
for($x = 1; $x <= 9; $x++) {
再次从 1 到 9 循环 $x
是没有意义的,因为你的数组索引。
echo "<p> You could have ender up in... </p>";
echo "<h2> " . $train[$x] . "</h2>";
当 $x
达到 8 时将失败,因为 $train
中的最后一个索引是 7。
}
return;
}
echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
}
return;
因此,如果用户没有选择“显示所有选项”,您会向他显示他选择的位置。如果用户选择“随机选择”,这将失败,因为 $cart
的值为“any”,而 $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>";
}
}
}
?>
关于php - 在数组中使用随机变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150881/