Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using random variables inside Arrays

Tags:

arrays

html

php

First of all, sorry if this is worded poorly, as I'm a beginner at code.

I'm currently working on an online computer science course, however I'm quite confused on how to do one small part. We need to use arrays for this activity, where the user has multiple selections and each different selection has a different/unique text output. Everything works fine, except I need an option to choose a random selection, however I'm a bit confused on how to make it. You can see from my code the options 1-8. I want it to randomly select one of the selections.

Here's my code:

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

I'm sure it's a bit messy, also. Hopefully you understand what I mean. If you're able to explain the answer to me that would be extremely helpful. Thank you :)

like image 841
Yesh _ Avatar asked May 11 '16 00:05

Yesh _


4 Answers

You are randomly generating a value regardless of the user choice and comparing it with user's selection and other weird things.

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

You are generating a random value before even checking if user selected 'Choose randomly' and why generate a random between 1 and 9? Your $train array starts with index 0 and ends with index 7.

    if($cart == show) { 

String needs to be quoted.

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

Again looping $x from 1 to 9 makes no sense because of your array indexes.

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

Will fail when $x reaches 8 since last index in $train is 7.

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

So if user didn't select 'Show all options' you show him his chosen location. If user chose 'Select Randomly' this will fail since $cart would have value 'any' and $train['any'] does not exist.

Here is code with correct logic.

<?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>";
        }
    }
}
?>
like image 169
TheDrot Avatar answered Nov 10 '22 08:11

TheDrot


Here is some problems in the code

  1. from where and why we return?
  2. no closing html-tags

I would change the last part of php-code like that:

<?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>
like image 28
Wizard Avatar answered Nov 10 '22 07:11

Wizard


What you're looking for is array_rand():

$random = array_rand($train);
like image 25
Darren Avatar answered Nov 10 '22 09:11

Darren


Instead of rand() use mt_rand() because the PHP DOCUMENTATION literally says

mt_rand — Generate a better random value

Regarding your php code, you have a lot of errors. This is how your bottom php code should look:

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

?>
like image 26
Webeng Avatar answered Nov 10 '22 08:11

Webeng