maztrin Posted February 27, 2009 Share Posted February 27, 2009 hi i am having a little trouble with an if else statement basically in my forum type application is this sort of format with an elseif statement ok? as i keep opening and closing the the php statements basically depending on what state $t is at will determine whether a form or message is presented to the user. $t = $_GET['topic']; if ($t == 0) { ?> //a html form goes here <?php } #end of if elseif ($t == 'c') { //code ?> //A MESSAGE IS DISPLAYED HERE INSTEAD OF A FORM <?php } else { //JUST A DEFAULT MSG OR SOMETHING } ?> end of html code and page what's happening at the moment is that depending on the status of '$t' it is showing only the first if statement any help is appreciated thanks MazTrin Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/ Share on other sites More sharing options...
keeps21 Posted February 27, 2009 Share Posted February 27, 2009 Looks fine to me Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772715 Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 try it with two if statements if the first doesnt return anything the second will try changing elseif for if looks fine to me too but try what i suggested Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772720 Share on other sites More sharing options...
Mark Baker Posted February 27, 2009 Share Posted February 27, 2009 Your first test is comparing $t with a numeric value If the value of $t is a string, PHP will convert it to a numeric (integer) so that it can do that comparison. If $t contains plain text, this conversion will result in it being seen as a numeric 0 by the comparison... therefore, the first if test is comparing 0 with 0, which matches To demonstrate how PHP tries doing this conversion try doing echo 0 + "Hello"; echo 1 + "Hello"; echo 0 + "3rd Test"; To resolve this, you need to make the first test a string to string test: if ($t == '0') so that there is no need for PHP to try and convert $t to a numeric value Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772721 Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 you are sending topic accross ok? in the url? does it say something.php?topic=c or topic= Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772724 Share on other sites More sharing options...
maztrin Posted February 27, 2009 Author Share Posted February 27, 2009 ah well i have tried this try it with two if statements if the first doesnt return anything the second will try changing elseif for if looks fine to me too but try what i suggested but it didn't work at all so i tried testing the first if statement and commented out the rest. and i noticed that it didn't even work...so its probably not the elseif statement at all. so do you think that maybe the get isn't getting the topic my adapted code: <?php $t = $_GET['topic']; if ($t == '0') { ?> //the code that i want it do display <?php } else { echo 'something went so wrong'; } when i use this it goes straight to the else statement. Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772747 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 r u sending topic variable via GET i.e. URL ? Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772748 Share on other sites More sharing options...
premiso Posted February 27, 2009 Share Posted February 27, 2009 Most likely, if it does not work, then no. The get is not getting the id. <?php $t = isset($_GET['topic'])?(int)$_GET['topic']:null; if (isset($t) { echo "{$t} was passed in, yay!"; }else { echo "Get data was not appened to the url like http://yoursite.com/script.php?t=2"; } ?> Give that a try and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772750 Share on other sites More sharing options...
Mark Baker Posted February 27, 2009 Share Posted February 27, 2009 Do I need to repeat what I said in Reply #3? Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772756 Share on other sites More sharing options...
daviddth Posted February 27, 2009 Share Posted February 27, 2009 Have you tried echoing the value of $t in a few places. i.e. <?php $t = $_GET['topic']; echo $t; and later... if ($t == '0') { echo $t; } I have exactly the same setup, except I dont close the php section and throw in html code - I just converted the html into php echo commands, so that way each if statement has a solid section of php between the { and } - I just prefer it that way lol Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772759 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 <?php echo "GET...."; var_dump($_GET); echo "POST...."; var_dump($_POST); this will show u if variables are passed properly to the page or not.. Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772768 Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 your link to this page must be <a href="pagename.php?topic=$srt"> or <a href="pagename.php?topic=0"> or <a href="pagename.php?topic=c"> for it to be able to get the value of topic Quote Link to comment https://forums.phpfreaks.com/topic/147200-solved-is-this-kind-of-an-elseif-statement-ok/#findComment-772869 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.