Jump to content


Photo

[SOLVED] If Statement Help


  • Please log in to reply
7 replies to this topic

#1 SelfObscurity

SelfObscurity
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts
  • LocationDetroit, MI

Posted 08 July 2006 - 09:50 PM

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

Jon Edney
Aspiring Web Developer

@JonEdney1
 


#2 oMIKEo

oMIKEo
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationLeeds, UK

Posted 08 July 2006 - 09:52 PM

try adding $id instead of just id in the IF statement.

Mike

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 08 July 2006 - 09:55 PM

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:
$var = $_GET['var'];
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:
if ($id == 'one')
{
   include ("page1.php");
}
elseif ($id == 'two')
{
   include ("page2.php");
}
else
{
   include ("main.php");
}

Hope it helps.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 pipeten

pipeten
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 July 2006 - 10:03 PM

switch($_GET['id']) {
  case 1:
    // do stuff
  break;
  case 2:
  case 4:
    // do stuff
  break;
  default:
    // do stuff
  break;
}


#5 SelfObscurity

SelfObscurity
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts
  • LocationDetroit, MI

Posted 09 July 2006 - 12:05 AM

Thanks for your help everyone, I'm working towards a database driven site; slowly.

Jon Edney
Aspiring Web Developer

@JonEdney1
 


#6 nloding

nloding
  • Members
  • PipPipPip
  • Advanced Member
  • 321 posts

Posted 09 July 2006 - 01:31 AM

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?

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 12:06 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 01:45 PM

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:
<?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");
}
?>
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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users