SelfObscurity Posted July 8, 2006 Share Posted July 8, 2006 Hello everyone. I am trying to do some basic scripting, and I'm finding out it isn't so basic, for me at least.I'm trying to create a page, that would be constructed of if statements, for example...index.phpindex.php?id=oneindex.php?id=twoI thought I had an idea of how to make that happen, but it doesn't work. Here is my code...if (id == ['one'])include ("page1.php");elseif (id == ['two'])include ("page2.php");elseinclude ("main.php"); Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/ Share on other sites More sharing options...
oMIKEo Posted July 8, 2006 Share Posted July 8, 2006 try adding $id instead of just id in the IF statement.Mike Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-54940 Share on other sites More sharing options...
ShogunWarrior Posted July 8, 2006 Share Posted July 8, 2006 Basically, you're probably forgetting some basic PHP rules because you're new to PHP.1) All variables start with a dollar sign: $.2) If you want to get a variable from the address like "page?var=3" then you should use this:[code]$var = $_GET['var'];[/code]Inside $_GET['NAME'] replace NAME with the name of the variable in the address string.3) Try using brackets, which will stop errors when you use longer if code blocks, e.g:[code]if ($id == 'one'){ include ("page1.php");}elseif ($id == 'two'){ include ("page2.php");}else{ include ("main.php");}[/code]Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-54943 Share on other sites More sharing options...
pipeten Posted July 8, 2006 Share Posted July 8, 2006 [code]switch($_GET['id']) { case 1: // do stuff break; case 2: case 4: // do stuff break; default: // do stuff break;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-54947 Share on other sites More sharing options...
SelfObscurity Posted July 9, 2006 Author Share Posted July 9, 2006 Thanks for your help everyone, I'm working towards a database driven site; slowly. Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-54985 Share on other sites More sharing options...
nloding Posted July 9, 2006 Share Posted July 9, 2006 That looks like a question I just posted.As far as processing goes on the server-side, is using several if-else statements or a long switch statement more effective? Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-55001 Share on other sites More sharing options...
redarrow Posted July 9, 2006 Share Posted July 9, 2006 I prefer to use the else elsif statement but some users like to use the switch statement all down to your programming prefrence ok.most users will post all types of ways to get the result wanted under there own programming prefrence ok.good luck. Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-55081 Share on other sites More sharing options...
.josh Posted July 9, 2006 Share Posted July 9, 2006 i would also like to mention here that even though those methods work, it opens up a security risk for people to inject their own pages into your website. You should setup your code to only allow specific values from your get variable. here is an example of what you should be doing:[code]<?php$allowable_page = array('home','about','faq');$id = $_GET['id'];if((isset($id)) && in_array($id,$allowable_page)) { include ("http://www.yoursite.com/".$id.".php");} else { include ("http://www.yoursite.com/index.php");}?>[/code]there are 2 main elements to this code. a) the list of allowable values as set in $allowable_page. b) explicitly using the specific url in your include, instead of a relative path Quote Link to comment https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/#findComment-55142 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.