Dice roll problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
quintin1
Forum Newbie
Posts: 4
Joined: Tue Oct 04, 2011 7:28 pm

Dice roll problem

Post by quintin1 »

Hi guys, first post here
Looks like im going to be here alot, since im taking a PHP course and im quite lost.
Anyways, im having trouble with this code. I think its the way i assigned my variables. Basically i need to make a program that he user inputs a number from 1-6 and a die is rolled and an image is displayed. If the user input the correct number shown on the die, they are awarded one point (this is to be done with session variables which i havent a clue how to do them correctly.
Anything will help.

Heres the form:
<body>

<form name = "n" action="diceroll.php" method="post">
Enter your roll: <br>
<input type= "text" name="1">

<input type ="submit" name = "submit">

</form>
</body>

and the PHP:

Code: Select all



<body>
<?php
$WIN = $_POST['guess'];
$dice =	 rand(1, 6);
    if ($WIN == $dice){ 
	echo "You Guessed Correctly!";
	}else {
	     echo "You have Guesed Incorrectley". "<br>";
		 }
          
	 
	if($dice == 1) echo "<img src='dice1.png'>"."<br>";
	if($dice == 2) echo "<img src='dice2.png'>"."<br>";
	if($dice == 3) echo "<img src='dice3.png'>"."<br>";
	if($dice == 4) echo "<img src='dice4.png'>"."<br>";
	if($dice == 5) echo "<img src='dice5.png'>"."<br>";
	if($dice == 6) echo "<img src='dice6.png'>"."<br>";

//else {
	     //echo "You have Guesed Incorrectley". "<br>";
		 //}
		 
?>

<form name= "back" action= "diceroll.html" method= "post">
<input type="submit" name="back" value ="Play Again">
</form>
</body>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dice roll problem

Post by Celauran »

Your form has

Code: Select all

<input type= "text" name="1">
but you're trying to process

Code: Select all

$WIN = $_POST['guess'];
They need to match.
quintin1
Forum Newbie
Posts: 4
Joined: Tue Oct 04, 2011 7:28 pm

Re: Dice roll problem

Post by quintin1 »

i fixed it but now the else statement isnt working, do i need to set a condition for it?
quintin1
Forum Newbie
Posts: 4
Joined: Tue Oct 04, 2011 7:28 pm

Re: Dice roll problem

Post by quintin1 »

scratch that, now im stuck on the session variable.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dice roll problem

Post by Celauran »

quintin1 wrote:scratch that, now im stuck on the session variable.
Can you elaborate? What are you doing? What isn't working?
quintin1
Forum Newbie
Posts: 4
Joined: Tue Oct 04, 2011 7:28 pm

Re: Dice roll problem

Post by quintin1 »

im trying to store the amount of times that the user correctly inputs the variable, which is guessing the dice roll correctly so that it stores the data like a cookie.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dice roll problem

Post by Celauran »

Yes, I got that, but how are you going about it. We can't tell you what's wrong with code we can't see.
Post Reply