Jump to content
xisto Community
BooZker

Php True False Script Im new and just curious how

Recommended Posts

I have looked up just about every PHP tutorial. I just dont get the if else statements. I am still very new and understand HTML, CSS, XML, but this is so hard for me for some reason. I want a basic if else statement. What i want is something like:"My name on trap 17 is BooZkerTrue | False"When you click true it gives a alert saying congrats and brings you to the next page and if its wrong then it will start ALL over from the beginning after a alert box comes up saying sorry, try again.Sorry about how easy this is, but i'm just not understanding PHP. I just need someone to write ONE question with that PHP and then i can figure it out from there. I just need have have an idea of how to even accomplish it.

Edited by BooZker (see edit history)

Share this post


Link to post
Share on other sites

Ok i'm not sure what you mean by this. But if you don't understand the if/else statements i'll show you below.

Basicly the 'if' statement is like this 'if something do this'.

Example:

<?php$var = "BooZker";if ($var == "BooZker"){ //If the $var is equal to BooZker do thisecho '<script language="Javascript">alert("The name is correct");</script>';}else{ //If it dosent equal itecho '<script language="Javascript">alert("The name is not correct");</script>';} //Ends the else?>

Hope that puts you in the clear

Share this post


Link to post
Share on other sites

At a glance the code above looks good :P So Boozker, take a look at it and see if you can work it out, if not then carry on reading!

The if/else statement isnt too complicated, it is basically:

If(this_thing == that_thing) {then im going to do this stuff.........} ELSE {but if they arent the same im ging to do this stuff instead ......... };


As with all PHP statements and comands you must end it with the semi colon. The curly brackets basically mean "then" eg
IF(1 == 2) {Ill do this...... }


Would translate in human speech as

If one is equal to two then i will do this...


Thats what makes me understand complicated IF statements better. You can have NOT EQUAL to or GREATER THAN, LESS THAN, etcetera but dont worry too much about them just yet. If you apply this explaination to the code above it might make it easier to understand.

A good thing i used to get used to this was make a very simple log in script which would work similar to the code above but the variable $var would come from another page, if you wanted to try that out check http://www.tizag.com/phpT/postget.php to learn about those variables and then use the code that is above and simply change the line

$var = "BooZker";


for

$var = $_POST['username'];
(username can be any variable name, the link above will hopefully explain) and then is you type the right username in you will get one message, and if its wrong you get another message. Then you can add like a password field or something and change the messages etc and get the hang of it :P


EDIT: Also see that instead of one EQUALS sign there are two in an IF statement, you always use two EQUALS signs in an IF because this means "is equal to" and one EQUALS sign means "give the value of"

EG
$var = "Bozker";


Means

$var GIVE THE VALUE OF "Boozker"


and

IF(1 == 2)


means

if "1" IS EQUAL TO "2" ...


:D
Edited by shadowx (see edit history)

Share this post


Link to post
Share on other sites

Thanks so much shadowx for saying that. Makes me understand it a lot more. Thanks. Now i just got to keep on learning, but that made me understand a lot better.Also Shining, if im not wrong isn't that script simply just a java script alert, and not a True | False? Is so how do i implement putting the word true or false?Example would be:1+1=2True | FalseAnd i want to know how to make a question that is false also. I dont want the script to always be True answers. Remember this is for a short true false quiz. I test that out though thanks. The alert is exactly what i wanted and i didnt even know you could put java script inside php thanks!

Edited by BooZker (see edit history)

Share this post


Link to post
Share on other sites

Shining's code is a mix of php and JS, the JS is inside the ECHO command, so the php does the IF statement and then uses ECHO to make some JS to show the user.

I think the best way to use Shining's code wuld be to use a GET variable. EG you have two or more pages, say page one says "MY name is Boozker. TRUE|FALSE" and "true" and "false" are hyperlinks. Now they should link to the second page like this:

<a href=page_two.php?answer=TRUE>TRUE</a>|<a href=page_two.php?answer=FALSE>FALSE</a>


note the GET variable 'answer' at the end of the URL. Then on page two you would use code like:

$answer = $_GET['answer';


So now we have either "TRUE" or "FALSE" in our variable, and then use the IF function from shining and change the variable $var to $answer in the IF function and delete the first line. And change "Boozker" in the IF to ether "TRUE" or "FALSE".

That will then show the message you want if its true or false and then after the end of the IF you can either use ECHO to build the rest of the HTML or end the php with "?>" and start html with "<HTML>" and write the HTML that way.

And if you want false you can either write "FALSE" in the code or you can use the NOT EQUAL to code which is simply instead of two equals signs "==" you do an exclamation mark and an equals sign : "!=". So if you wanted to say:

IF the answer IS NOT true { i will do this }

the code is
IF($answer != "TRUE"){do this ... ... ... ...};

and of course you can expand this code with the ELSE part too and that will be run if the answer IS true. So whereas in a normal IF TRUE function like shining wrote, the first lines are done if something *is* true in a "NOT TRUE" function like the one above, the first few lines are done if the IF statement is *not* true. So things are reversed, that can be confusing sometimes. Does that help?

Share this post


Link to post
Share on other sites

Awesome thanks so much. OK so just a couple questions about that. For stuff to add on, but it works great the way it is. I understand the if else statements now! It's a miracle.

OK so here are my questions

How could i put this?

if($answer != "TRUE"){<center>Want to try again? <br /><br /> <a href="index.php">Yes</a> <a href="http://google.com">No</a></center>};?>

obviously thats wrong, but how could i do that? OR how could i send them to another page, for example, you get the wrong answer you get sent to the retry.php and if you get it right you go on to the next page so page_2.php?

Next question is what was the:

$answer = $_GET['answer';


for? I put it in and it would never work, but when i didnt put it in it worked fine. Am i supposed to put it in?

Thanks for all the help guys. I know know how to make multiple choice tests also.

Share this post


Link to post
Share on other sites

How could i put this? How could i put this?

 

CODE

if($answer != "TRUE")

{<center>Want to try again? <br /><br /> <a href="index.php">Yes</a> <a href="https://www.google.de/?gfe_rd=cr&ei=7AkjVIatDsKH8QfNkoC4DQ&gws_rd=ssl;};

?>

 


Well its almost right except to send out HTML or any text you need to use either

 

echo "<center>Want to try again?etc etc...";

 

Or

 

print "text here...";

I normally use echo and according to the php manual site ECHO is slightly faster so id reccomend using that. In which case your code would be like this:

 

if($answer != "TRUE"){echo "<center>Want to try again? <br /><br /> <a href='index.php'>Yes</a> <a href='http://google.com'>No</a></center>";};?>

See the echo command and the double quotes enclosing the HTML. Also its very important that you remove all double quotes from within the text, eg you had double quotes, and rightly so, around the page filename in the hyperlinks but when ECHO finds a double quote it just stops and buggers the whole page up! So instead i replaced them with single quotes, you can also completely remove them.

 

If you really really need the double quotes to be displayed then you can use a backslash \ just before the double quotes eg:

 

echo "<a href=\"somewhere.html\"> somewhere </a>";

 

And that would be fine.

 

Now about redirecting someone to a different page, there is a method in php but the drawback is that you cant use any echo or print or most other commands before it. The best way i think is to use javascript, im not very god with JS so i use codes i find on google for that! Just search for a javascript redirect and use an echo command to send it to the browser remembering to deal with the double quotes!

 

There is also one more method for changing the page, you can use an INCLUDE function. All this does it take the code from one file and run it, so if you had two pages the user can access the first and then the INCLUDE function will open the second, take the code from inside and run it as if it was the first page.

 

In your code i would use something like this:

 

<?if($answer != "TRUE"){		include("wrong.htm");	}ELSE{		include("right.htm");};?>

 

if they get the answer wrong the first INCLUDE will run and it will load the file "wrong.htm" you can change this to whatever page you wanted, maybe a page saying you got it wrong start again or something.

 

if they get it right the second INCLUDE will run and it will load the file "right.htm" again you can change this, possibly so that it loads the next question.

 

the important thing to remember is that using INCLUDE the original file contents dont disappear, so if you include page 2 from page 1 then you will have code from both pages, and then you include another you will have 3 files worth of code, it wont save like this but it might make loading times longer for that user.

 

 

 

Overall for this example of the quiz i would use the javascript idea but in the future for things like making a log in system or things like that the INCLUDE idea is very usefull and i think a lot of content management systems use INCLUDE to load pages.

 

 

Hope that isnt confusing!

 

 

And the

 

$answer = $_GET['answer'];

 

is to set up a variable. Some places wont take a variable from the URL. So for example on my website i use a variable called "module" in my url like : "modules.php?module=something " and in some places my code will automatically use that variable but in other places i have to tell it t use that variable by using code like you asked about above.

 

$_GET simply means look for a variable in the URL of this page.

 

['answer'] Means look for the variable answer in the URL

 

So the code above is saying $answer EQUALS the variable named "answer" in the URL.

 

Not all hosts need this line but i think on my account here i needed to add it. Im not sure if you realized but there was a bracket missing from the code when you posted it, if thats how it is in your page then adding the bracket in like above should make it work.

 

Sorry this post is so long!!

Share this post


Link to post
Share on other sites

I did a bit of researching for you and found this on Hotscripts.
This is the URL:
http://forums.xisto.com/no_longer_exists/

I would recommend using one of those instead of making your own, as by the look of it, it looks pretty complex.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.