Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,295 members, 7,808,003 topics. Date: Thursday, 25 April 2024 at 02:21 AM

Need Help With This Basic Php - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Need Help With This Basic Php (1672 Views)

Pls After Learning D Basic Php Wat Next? / Need Help On This Basic Php / Q-basic Program To Compute Simple Interest. (2) (3) (4)

(1) (Reply) (Go Down)

Need Help With This Basic Php by Setag: 7:41pm On Jan 22, 2016
Let's say we're gonna be rolling a die:

$die = rand(1, 6);


Whilst the die is being rolled (the value of $die remains the same till user makes the right guess), the user tries to guess the value of the die. Program tells the user if the guess is higher (or lower) than the die value if guess is incorrect, program tells user how many times it took to guess the value of the die right if it took more than one turn to make the right guess.

Immadiately user gets it right, game starts over again(the die is rolled again).
Re: Need Help With This Basic Php by hollyfat(m): 7:44pm On Jan 22, 2016
Setag:
Let's say we're gonna be rolling a die:

$die = rand(1, 6);


Whilst the die is being rolled (the value of $die remains the same till user makes the right guess), the user tries to guess the value of the die. Program tells the user if the guess is higher (or lower) than the die value if guess is incorrect, program tells user how many times it took to guess the value of the die right if it took more than one turn to make the right guess.

Immadiately user gets it right, game starts over again(the die is rolled again).

Get the value of $die and set it as a session variable. When the user get the answer, you reset the value
Re: Need Help With This Basic Php by hollyfat(m): 7:53pm On Jan 22, 2016
hollyfat:


Get the value of $die and set it as a session variable. When the user get the answer, you reset the value

Try this


<!DOCTYPE html>
<html>
<head>
<title>
Die
</title>
</head>
<body>
<?php
session_start();
if (isset($_POST['guess'])) {
//check if session is set for die

if (isset($_SESSION['die'])) {
//check user answer
if ($_POST['answer'] == $_SESSION['die']) {
echo "<p>Thumbs up!, you guess right.</p>";
unset($_SESSION['die']);
}else{
echo "<p>Incorrect Guess, please try again</p>";
}
}else{
//roll the die again
$die = rand(1,6);
if ($_POST['answer'] == $die) {
echo "<p>Thumbs up!, you guess right.</p>";
}else{
$_SESSION['die'] = $die;
echo "<p>Incorrect Guess, please try again</p>";
}
}
}
?>

<form action="" method="post">
<label for="answer">Enter your guess</label>
<input type="text" name="answer" id="answer" required />
<br>
<input type="submit" name="guess" value="Guess!">
</form>

</body>
</html>
Re: Need Help With This Basic Php by paranorman(m): 8:31pm On Jan 22, 2016
hollyfat:


Try this


<!DOCTYPE html>
<html>
<head>
<title>
Die
</title>
</head>
<body>
<?php
session_start();
if (isset($_POST['guess'])) {
//check if session is set for die

if (isset($_SESSION['die'])) {
//check user answer
if ($_POST['answer'] == $_SESSION['die']) {
echo "<p>Thumbs up!, you guess right.</p>";
unset($_SESSION['die']);
}else{
echo "<p>Incorrect Guess, please try again</p>";
}
}else{
//roll the die again
$die = rand(1,6);
if ($_POST['answer'] == $die) {
echo "<p>Thumbs up!, you guess right.</p>";
}else{
$_SESSION['die'] = $die;
echo "<p>Incorrect Guess, please try again</p>";
}
}
}
?>

<form action="" method="post">
<label for="answer">Enter your guess</label>
<input type="text" name="answer" id="answer" required />
<br>
<input type="submit" name="guess" value="Guess!">
</form>

</body>
</html>
oga, there is no object in the form with the name 'guess', //second line of your php code.
Re: Need Help With This Basic Php by hollyfat(m): 8:48pm On Jan 22, 2016
paranorman:

oga, there is no object in the form with the name 'guess', //second line of your php code.

Check very well, it's in the submit button
<input type="submit" name="guess" value="Guess!">

1 Like

Re: Need Help With This Basic Php by tgmservice: 9:00pm On Jan 22, 2016
Setag:
Let's say we're gonna be rolling a die:

$die = rand(1, 6);


Whilst the die is being rolled (the value of $die remains the same till user makes the right guess), the user tries to guess the value of the die. Program tells the user if the guess is higher (or lower) than the die value if guess is incorrect, program tells user how many times it took to guess the value of the die right if it took more than one turn to make the right guess.

Immadiately user gets it right, game starts over again(the die is rolled again).
try this
guess = 0;
$ die = rand(1,6);
for($count = 1; $count <= 5; $count++){
if guess == 0{ echo "correct"; }break
else{if count == 5{echo "game over";}
Re: Need Help With This Basic Php by paranorman(m): 2:08am On Feb 13, 2016
tgmservice:
try this
guess = 0;
$ die = rand(1,6);
for($count = 1; $count <= 5; $count++){
if guess == 0{ echo "correct"; }break
else{if count == 5{echo "game over";}

without even trying it out, I know it won't work!
Re: Need Help With This Basic Php by paranorman(m): 2:09am On Feb 13, 2016
hollyfat:


Check very well, it's in the submit button
<input type="submit" name="guess" value="Guess!">

you base a condition on a button?! I wouldn't do that though.
Re: Need Help With This Basic Php by paranorman(m): 2:11am On Feb 13, 2016

//roll the die and create a session variable for it
//create another '$count' session variable to manage pesistence - that is, the number
//of trials till you make a right guess

//main program control logic; I call him Jarvis
function jarvis() {
if (!isset($_SESSION["die"])) {
$die = rand(1, 6);
$_SESSION["die"] = $die;
$_SESSION["count"] = 0;
print <<<here
<h1>Welcome Dude</h1>
<p>
A die was rolled, try to guess the value, okay?
Good luck.
</p>
<form action = ""
method = "post">
<button type = "submit">
continue
</button>
</form>
here;
} else {
ultron();
} //end main logic if
} //end jarvis

//I love this main function, reminds me of Ultron
function ultron() {
$dice = $_SESSION["die"]; //$dice is just the session variable 'container'
$guess = filter_input(INPUT_POST, 'guess' ) ;
$count = $_SESSION["count"]; //you wouldn't want $_SESSION["count"] 'naked', would you?

//print "<p>die: $dice</p>";

if ($guess == null) {
print "<h1>Die rolled, make a guess - already!</h1>";
} else if ($guess > $dice) {
$count++;
print "<h1>Greater than, try again</h1>";
} else if ($guess < $dice) {
$count++;
print "<h1>Lesser than, try again</h1>";
} else if ($guess == $dice) {
print "<h1>Weldone skipper, you guessed right!</h1>";
//print "<p>count: $count</p>";
if ($count > 1) {
print "<div>.. but it took you $count times to get it right</div>";
} //end $count if
} //end if

//print "<p>$count</p>";

print <<<HERE
<form action = ""
method = "post">
<fieldset>
<legend>Make the guess</legend>
<input type = "text"
name = "guess"/>
<button type = "submit">
submit
</button>
</fieldset>
</form>
HERE;
//keep the session variables 'alive'!
$_SESSION["die"] = $dice;
$_SESSION["count"] = $count;

//kill 'em off if objective is accomplished
if ($guess == $dice) {
unset($_SESSION["count"]);
unset($_SESSION["die"]);
print "<p><a href = \"dieGuesser.php\">Start game again</a>, or click the submit button.</p>";
}
} //end ultron

//initiate jarvis
jarvis();
//forgive me, Ultron and Jarvis are friends in Paranorman's world


cc: hollyfat
Re: Need Help With This Basic Php by Nobody: 4:42am On Feb 13, 2016
Oga o
Re: Need Help With This Basic Php by paranorman(m): 9:07am On Feb 13, 2016
dhtml18:
Oga o
bros, wetin happen na?
Re: Need Help With This Basic Php by Nobody: 4:54pm On Feb 13, 2016
I dey look all these codes and fear dey catch me?
Re: Need Help With This Basic Php by paranorman(m): 5:17pm On Feb 13, 2016
dhtml18:
I dey look all these codes and fear dey catch me?
na basic code be this one na, noting pro at all. Na you be baba na

(1) (Reply)

Laptop & Desktop Prices In Nigeria (for Programmers) / Autocad 2010 64 Bit Activation Crack / Accept Airtime Payment Using Hosted Sim, Automatically Credit The Users Units

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.