Jump to content

Archived

This topic is now archived and is closed to further replies.

SelfObscurity

[SOLVED] If Statement Help

Recommended Posts

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.php
index.php?id=one
index.php?id=two

I 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");

else
include ("main.php");

Share this post


Link to post
Share on other sites
try adding $id instead of just id in the IF statement.

Mike

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]switch($_GET['id']) {
  case 1:
    // do stuff
  break;
  case 2:
  case 4:
    // do stuff
  break;
  default:
    // do stuff
  break;
}[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.