Jump to content

[SOLVED] Need some simple help


Lime

Recommended Posts

Well I am making a number guessing game. It works just fine. But if the person doesn't pick a number and just clicks submit, it says that you got the number wrong. How can I make it say something like "Please go back and select a number." Thanks to all that can help. Here is my current script with the radiobuttons to select a number.

 

<form action="if.php" method="post">
Please select a number to guess.<br>
<input type="radio"
name="num" value="1">1   
<input type="radio"
name="num" value="2">2   
<input type="radio"
name="num" value="3">3   
<input type="radio"
name="num" value="4">4   
<input type="radio"
name="num" value="5">5<br>
<input type="submit" value="Guess that number!">
</form>

<?php
$cpu_number = rand(1, 5);

If (isset ($_POST['num'])) {
$number = $_POST['num'];
}

    If ($number == $cpu_number) { 
        echo "You guessed $number... Your number the same! You win!";

    } elseif ($number != $cpu_number) { 
        echo "You guessed $number... Your number does not match. Sorry, you lose. The computer's number was $cpu_number.";

   } elseif (noNumberSelected) {
echo "You haven't pressed a number. Please go back and select a number.";
}
?>

Link to comment
Share on other sites

If looks like you need a loop to do that. Do you know how to make a while loop? Basically, you can set up a while(x = false) loop, where x is a boolean variable. When they get the question right, set x to true, and it will exit the loop. Otherwise, it will continue to loop through the selections.

 

Err...I guess that would only work if the options were inside the PHP code, and you had a while loop around all that. I'm not sure, but someone else can probably help you out.

Link to comment
Share on other sites

<?php
$cpu_number 	= rand(1, 5);
$number 	= (int)$_POST['num'];

switch($number)
{
  case $cpu_number:
echo "You guessed $number... Your number is the same! You win!";
  break;
  case 0:
echo "You haven't selected a number. Please go back and select a number.";
  break;
  default:
echo "You guessed $number... Your number does not match. Sorry, you lose. The computer's number was $cpu_number.";
  break;
}

?>

Link to comment
Share on other sites

Okay, thats one way of doing it. I'll try it.

 

P.S. Please forget the:

   } elseif (noNumberSelected) {
echo "You haven't pressed a number. Please go back and select a number.";

That was a sad attempt that I forgot to take out... :P

Link to comment
Share on other sites

<?php

    $number = $_POST['num'];
    $back_link = $_SERVER['HTTP_REFERER'];

         if (empty($number) || (int)$number == 0)
             {
                  echo ('Please go back and select a number. ');
                  echo ('<a href="'.$back_link.'">Go back</a>');
             }
             else
             {
                    // your code
             }
?>

Link to comment
Share on other sites

The following code has been tested and works fine:

<form action="" method="post">
Please select a number to guess.<br>
<?php
for ($i = 1; $i < 6; $i++)
echo '<input type="radio" name="num" value="' . $i . '">' . $i;
?>
<br><input type="submit" name="submit" value="Guess that number!">
</form>

<?php

If (isset($_POST['submit'])) {
$cpu_number = rand(1, 5);
$number = $_POST['num'];
switch ($number) {
           case $cpu_number:
        echo "You guessed $number... Your number the same! You win!";
                break;
   case 0:
                echo "You haven't pressed a number. Please go back and select a number.";
                break;
           default:
                echo "You guessed $number... Your number does not match. Sorry, you lose. The computer's number was $cpu_number.";
}
}
?>

 

Ken

Link to comment
Share on other sites

Here's one way -- I modified your script a little to be more interesting... :)

 

<?php
session_start();
If (isset($_POST['submit'])) {
       $cpu_number = $_SESSION['cpu_guess'];
       unset($_SESSION['cpu_guess']);
       $number = $_POST['num'];
switch ($number) {
	case $cpu_number:
        echo "You guessed $number... Your number the same! You win!";
		  break;
   case 0:
			echo "You haven't pressed a number. Please go back and select a number.";
			break;
	default: 
	     echo "You guessed $number... Your number does not match. Sorry, you lose. The computer's number was $cpu_number.";
}
}
else {
    $guesses = rand(5,15);
    $_SESSION['cpu_guess'] = rand(1,$guesses);
?>
<form action="" method="post">
I'm thinking of a number between 1 and <?php echo $guesses ?> Can you guess what it is?.<br>
<?php
    for ($i = 1; $i <= $guesses; $i++)
echo '<input type="radio" name="num" value="' . $i . '">' . $i;
?>
<br><input type="submit" name="submit" value="Guess that number!">
</form>
<?php } ?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.