jetlife76 Posted October 6, 2011 Share Posted October 6, 2011 Im new to php and i cant get my program to display correctly. I want it to take a user's guess of a single Die roll and display whether the user guessed correctly and display a picture of the corresponding Die. i think there is something wrong with my "If" statement or logic. Please Help. Here's the Code: INPUT FORM: <html> <body> <form name = "Dice Game" action="diceroll.php" method="post"> Your Guess: <br> <input type= "text" name="" size= "1"> <input type ="submit" name = "submit"> </form> </body> </html> OUTPUT FORM: <html> <body> <?php $roll = $_POST['submit']; $dice = rand(1, 6); if ($roll == 1 && $dice == 1){ print "Great Job, You're Good!"; }else if ($roll == 1 && $dice /= 1) { print "Wrong"; } else if ($roll == 2 && $dice == 2) { print "Great Job, You're Good!";} else if ($roll == 2 && $dice /= 2) { print "Wrong"; } else if ($roll == 3 && $dice == 3) { print "Great Job, You're Good!"; } else if ($roll == 3 && $dice /= 3) { print "Wrong"; } else if ($roll == 4 && $dice == 4) { print "Great Job, You're Good!"; } else if ($roll == 4 && $dice /= 4) { print "Wrong"; } else if ($roll == 5 && $dice == 5) { print "Great Job, You're Good!"; } else if ($roll == 5 && $dice /= 5) { print "Wrong"; } else if ($roll == 6 && $dice == 6) { print "Great Job, You're Good!"; } else if ($roll == 6 && $dice /= 6) { print "Wrong"; } else { print " "; } if($dice == 1) print "<img src='dice1.png'>"."<br>"; if($dice == 2) print "<img src='dice2.png'>"."<br>"; if($dice == 3) print "<img src='dice3.png'>"."<br>"; if($dice == 4) print "<img src='dice4.png'>"."<br>"; if($dice == 5) print "<img src='dice5.png'>"."<br>"; if($dice == 6) print "<img src='dice6.png'>"."<br>"; else { print "Thanks for playing". "<br>"; } ?> <form name= "back" action= "dice.php" method= "post"> <input type="submit" name="back" value ="Play Again"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/ Share on other sites More sharing options...
awjudd Posted October 6, 2011 Share Posted October 6, 2011 Please use the [ php ][ /php ] tags for your code (minus the spaces). ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276331 Share on other sites More sharing options...
jetlife76 Posted October 6, 2011 Author Share Posted October 6, 2011 ok sorry about that, can you help me? Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276332 Share on other sites More sharing options...
awjudd Posted October 6, 2011 Share Posted October 6, 2011 != is does not equal, not /= That said, there is a very much an easier way of doing this ... /* Check if the dice and roll match */ if ( $roll == $dice ) { /* They do, congrats */ print "Great Job, You're Good!"; } else { /* They don't, too bad */ print "Wrong"; } /* Print the dice number */ print "<img src='dice" . $dice . ".png'>"."<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276333 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 You are also using the incorrect form element to capture the "guess" You will need to assign a name to your "guess" input box and retrieve it. Currently, you are getting the value from the submit button as your roll variable. Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276334 Share on other sites More sharing options...
jetlife76 Posted October 6, 2011 Author Share Posted October 6, 2011 Ok cool, but now i have an undefined variable "submit" what should it be Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276335 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 What does your code look like now? Your previous code did not have a $submit used anywhere Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276336 Share on other sites More sharing options...
jetlife76 Posted October 6, 2011 Author Share Posted October 6, 2011 <?php $roll = $_POST['guess']; $dice = rand(1, 6); /* Check if the dice and roll match */ if ( $roll == $dice ){ /* They do, congrats */ print "Great Job, You're Good!";} else{ /* They don't, too bad */ print "Wrong";} /* Print the dice number */ print "<img src='dice" . $dice . ".png'>"."<br />"; ?> I gave it the name guess it works now thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/#findComment-1276339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.