Jump to content

Hello :) Just a little help for if else & switch please


Rider_007

Recommended Posts

Hello there.!this is a little game with 6 dice pictures, when you reload the page it shows you how many 'kings' and 'soldiers' are in totally in the given random pictures! each time you reload the page the number of kings and soldiers change..
Now my problem is that i want to make it like, the user should write how many 'kings' and 'soldiers' are there and when he press the submit button, a message will show if his answer is correct or not..if its not, it should display the correct number of kings and soldiers that are there!
(the 'kings' are the circles in the middle of each pic! if there's no circle in the middle of any pic, that means there are no 'kings' there)
i spent lot of hours with this, trying to do it with if else if and with switch cases but the only thing i got, were errors -.-
i hope some 1 of you will help me to solve this! :)
HERE ARE 2 PICS OF HOW IT LOOKS LIKE:
http://imgur.com/EVP8mMQ,HaMZL6q#0
http://imgur.com/EVP8mMQ,HaMZL6q#1
 

THIS IS THE CODE OF THE FIRST PAGE:

<?php
function count_soldiers($res)
{
switch($res){
case 1:
return 0;
break;
case 2:
return 2;
break;
case 3:
return 2;
break;
case 4:
return 4;
break;
case 5:
return 4;
break;
case 6:
return 6;
break;
}
}
 
function count_kings($res)
{
switch($res){
case 1:
return 1;
break;
 
case 2:
return 0;
break;
 
case 3:
return 1;
break;
 
case 4:
return 0;
break;
 
case 5:
return 1;
break;
 
case 6:
return 0;
break;
}
}
function total_kings($r1,$r2,$r3,$r4)
{
 
$totking = count_kings($r1)+count_kings($r2)+count_kings($r3)+count_kings($r4);
 
return $totking;
 
}
 
function total_soldiers($r1,$r2,$r3,$r4)
{
 
$totsoldier = count_soldiers($r1)+count_soldiers($r2)+count_soldiers($r3)+count_soldiers($r4);
 
return $totsoldier;
 
}

?>

AND THIS IS THE CODE OF THE SECOND PAGE:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How many kings are?</title>
</head>
 
<body bgcolor="#e0e0e0">
<div align="center">
<p><h2>How many kings and soldiers are in totally?
</h2>
</p>
<hr/>
<?php
require_once("funksione.php");
 
$ran = rand(1, 6);
$ran1 = rand(1, 6);
$ran2 = rand(1, 6);
$ran3 = rand(1, 6);
echo "<br/>";
echo "In totally there are ". total_kings($ran,$ran1,$ran2,$ran3). 
" kings and ". total_soldiers($ran,$ran1,$ran2,$ran3). " soldiers.";
echo "<br/>";
print "<img src=images/". $ran .".jpg>";
print "<img src=images/". $ran1 .".jpg>";
print "<img src=images/". $ran2 .".jpg>";
print "<img src=images/". $ran3 .".jpg>";
?>
 
<p><h2>How many kings and soldiers are in totally?
</h2>
</p>
<form method="post" action="example11-2.php">
Kings:
<input type="text" name="name" />
<form method="post" action="example11-2.php">
Soldiers:
<input type="text" name="name" />
<input type="submit" />
</form>
</div>
</body>
</html>
 
Edited by Rider_007
Link to comment
Share on other sites

ahh i'm so confused right now, i couldn't make it in the right way to get the result that i wanted, i'm not much advanced in php, i'm new..
that is the problem my friend, i need help on code that is used to compare the expected value with the value that the user provides,

i saw some other examples on google, i tried to do something but couldnt connect it :S 

Edited by Rider_007
Link to comment
Share on other sites

So the main thing you need to do is figure out how to remember the correct answer.

 

There are a few ways it could be done, for example:

using a database,

using a plain text file on the server,

using session variables.

 

Session variables will probably be the easiest to set up in the short term.

 

When you set your variables $ran, $ran1 etc., save them to the session like such

 

$_SESSION['answers']['ran'] = $ran;
$_SESSION['answers']['ran1'] = $ran1;
//etc

 

Then you can compare the values stored in the session array with those that the user provided.

 

Have a crack at implementing something like that. If you need more help, just post what you're able to come up with, and I'll guide you some more.

 

Denno

Link to comment
Share on other sites

ohh, yes the best way for me as i can see is using these session variables...i totally understood how it works!
but i still have problems, i know i'm doing something wrong here but i don't know exactly what, when the page opens it always displays the message:"
Wrong Answer.The Correct Answer is:" and when i press the submit button it shows:
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404

localhost
4/19/2014 2:11:18 PM

Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1.....
Take a look on what i did please and help me :/ thanks!

<?php
require_once("funksione.php");
if (isset($_POST['ran,ran1,ran2,ran3']))
  {
  echo "Correct Answer!";
  }
else
  {
  echo "Wrong Answer.The Correct Answer is:!";
  }
$ran  = rand(1, 6); $_SESSION['answers']['ran'] = $ran;
$ran1 = rand(1, 6); $_SESSION['answers']['ran'] = $ran1;
$ran2 = rand(1, 6); $_SESSION['answers']['ran'] = $ran2;
$ran3 = rand(1, 6); $_SESSION['answers']['ran'] = $ran3;
echo "<br/>";
echo "In totally there are ". total_kings($ran,$ran1,$ran2,$ran3). 
" kings and ". total_soldiers($ran,$ran1,$ran2,$ran3). " soldiers.";
echo "<br/>";
print "<img src=images/". $ran .".jpg>";
print "<img src=images/". $ran1 .".jpg>";
print "<img src=images/". $ran2 .".jpg>";
print "<img src=images/". $ran3 .".jpg>";
?>
Link to comment
Share on other sites

So the code you've just posted is quite confusing.

 

First, $_POST['ran, ran1, ran2, ran3']

I really don't think that checks the 4 different POST array values.. That's checking for an array value with index "ran, ran1, ran2, ran3", which you definitely don't have.

 

Break that up to be $_POST['ran'], $_POST['ran1'] etc.

 

Second, you're not even doing any checking of the correct answer. Basically your code says that whatever answer they submit, they're right, otherwise, they're wrong. So if they don't provide an answer (or they load the page for the first time), they're wrong.

 

Third, these lines:

 

$ran = rand(1, 6); $_SESSION['answers']['ran'] = $ran;
$ran1 = rand(1, 6); $_SESSION['answers']['ran'] = $ran1;
$ran2 = rand(1, 6); $_SESSION['answers']['ran'] = $ran2;
$ran3 = rand(1, 6); $_SESSION['answers']['ran'] = $ran3;

will leave you with a session array looking like this

 

array (
  "answers" => array (
      "ran" => X //Where X is the random number between 1 and 6
   )
)

So you're only ever storing 1 value in the session array, and you're over-writing it each time, so only the last random number, $ran3, will ever be saved.

 

Your code is actually quite a ways off what you require. You might want to go through it from the start and make sure you understand what you're trying to achieve.

Link to comment
Share on other sites

ohh actually, we did this thing in the classroom, the teacher was there helping us, and when we finished he just told us to take this at home, to add 2 new forms and to do the stuff that i already asked before...i know my code it's confusing you,(it's confusing me too) but as i told you i know only few things about php, and if you open my mind right now you will find only confusing php codes coming around lol! i really don't know what to do, i tried some things earlier before you give me this answer but no way, i cant do it, and these dice pictures on the code are making my job even harder!
i see you know of course something more than me, so cant you try do the code for me please :confused:  i will never make it,even if im trying to see other examples on internet, this makes me more confused  :/  :psychic:

Thank you!  ::)

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.