11 Replies - 104339 Views - Last Post: 01 April 2008 - 08:14 PM Rate Topic: -----

#1 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

if statement matches multiple values

Posted 27 June 2006 - 08:29 AM

I need to know how to make an if statement that
checks to see if a $locationid is equal to one of the following numbers. (dont have the numbers yet, but it only has to equal one, and then execute the action, not all of them)

then i need a command to tell the webpage to go to another page if that requirement is met.

can you do

if ($locationid == "152,234,598");

152 , 234, 598 all being seperate numbers.

the numbers will be the same forever, or at least not changed very often at all, so yeah. thanks for the help

Is This A Good Question/Topic? 0
  • +

Replies To: if statement matches multiple values

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: if statement matches multiple values

Posted 27 June 2006 - 08:37 AM

Assuming 152, 234, and 598 are variables of the integer type
if ($locationid == 152 || $locationid==234 || $locationid==598)


if statements do not take a semi colon as terminator

the syntax will change somewhat if the numbers are actually of the string type.
Was This Post Helpful? 1

#3 max302  Icon User is offline

  • Proud supporter of the lulz
  • member icon

Reputation: 2
  • View blog
  • Posts: 1,281
  • Joined: 05-March 06

Re: if statement matches multiple values

Posted 27 June 2006 - 08:50 AM

Yup, and and statements work the same way:
if ($foo == 1337 && $var == "superduper")

This post has been edited by max302: 27 June 2006 - 08:51 AM

Was This Post Helpful? 1

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: if statement matches multiple values

Posted 27 June 2006 - 09:16 AM

the reason you can't use the ';' is it is an end of expression operator. So by placing it as if(...); the if statement is evaluated then ignored, as the body of the if if cut out from the ;

Quote

Yup, and and statements work the same way

The styles are the same, but you definatly want OR in this case.

If at any time you are looking for an identity equality (not just a number equality) use ===
Was This Post Helpful? 0
  • +
  • -

#5 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: if statement matches multiple values

Posted 27 June 2006 - 09:35 AM

well im actually having trouble with this and i dont know why..

just for simplicitys sake
lets say i define two variables

<?php $note = "HEllO WORLD. NOTE ONE"; ?>
<?php $note2 = "Note 2"; ?>

//later on.....

<?php

if ($id == 152 || $id==234 || $id==598)
	echo "$note2";
} else {
	echo "$note";
} 
?>



now, the id number is coming from the fact the site name is
www1.airimba.com/tlee/test3.php?id=

now,
all i get when i do this is a blank page

This post has been edited by capty99: 27 June 2006 - 09:37 AM

Was This Post Helpful? 1

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: if statement matches multiple values

Posted 27 June 2006 - 10:02 AM

Try putting an echo iof the $id at the top of the code, see if it comes up...there are a couple of things that could be proving problematic...first, you may have to set the valued of $id in the following manner:
$id=$_GET['id'];


It may also be the fact that the if statement has a closing brace, but know opening brace. Or it could be that the variable $id is interpreting it's value as a string, not a number.
Was This Post Helpful? 0
  • +
  • -

#7 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: if statement matches multiple values

Posted 27 June 2006 - 10:24 AM

the get id worked,
thanks to all you helped.
i am very pleased.
:)
Was This Post Helpful? 0
  • +
  • -

#8 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: if statement matches multiple values

Posted 27 June 2006 - 11:07 AM

alright new question
just for my own sake,
because instead of a couple values as i had imagined, it turned out to be about 35, which i inputted just like that.
but would there have been an easier way to do that?
Was This Post Helpful? 0
  • +
  • -

#9 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: if statement matches multiple values

Posted 27 June 2006 - 11:25 AM

You could put each value in an array and than check to see if the value in question exists in the array.

Something like this.
$someValue = "My brain melts at the thought.";
$arr = array(1,2,3,4,5,6,7,8,9,0);
if(in_array($someValue, $arr)) {
	echo "I'm smart!";
} else {
   echo "I liek milk!";
}

Was This Post Helpful? 0
  • +
  • -

#10 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: if statement matches multiple values

Posted 27 June 2006 - 11:27 AM

using a switch statement would be a lot easier I think, especially with like 35 cases.

<?php $note = "HEllO WORLD. NOTE ONE"; ?>
<?php $note2 = "Note 2"; ?>

//later on.....

<?php
$id = var1;

switch($id)
 {
   case 152:
	 echo $note;
	 break;
   case 234:
	 echo $note;
	 break;
   case 598:
	 echo $note;
	 break;
   default:
	 echo $note2;
	 break;
 }
?>


i dunno exactly how you are going to have it all layed out, but the way i just posted would let you echo a different thing with each number that you returned. I'm sure you could do an || (or) to condense the code if you were going to have more than one number echo the same thing.

the default setting is what would be echoed if none of the other cases are true.. so if some number other than 152, 234, or 598 was returned it would display $note2


edit:added code tag

This post has been edited by the_hangman: 27 June 2006 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#11 kEpEx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-October 06

Re: if statement matches multiple values

Posted 17 October 2006 - 01:16 PM

[quote name='the_hangman' date='27 Jun, 2006 - 11:27 AM' post='159994']

better this way

<?php $note = "HEllO WORLD. NOTE ONE"; ?>
<?php $note2 = "Note 2"; ?>

<?php
$id = var1;

switch($id)
 {
   case 152:
   case 234:
   case 598:
	 echo $note;
	 break;
   default:
	 echo $note2;
	 break;
 }
?>



regards :)
Was This Post Helpful? 0
  • +
  • -

#12 CyberPetWorld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-April 08

Re: if statement matches multiple values

Posted 01 April 2008 - 08:14 PM

View PostAmadeus, on 27 Jun, 2006 - 10:02 AM, said:

Try putting an echo iof the $id at the top of the code, see if it comes up...there are a couple of things that could be proving problematic...first, you may have to set the valued of $id in the following manner:
$id=$_GET['id'];


It may also be the fact that the if statement has a closing brace, but know opening brace. Or it could be that the variable $id is interpreting it's value as a string, not a number.



Hello everyone,

I was very pleased to see someone had the same problem as me and got it sorted! I was wondering if I could have some help with my coding....

I have a website that has a rank structure for members and the $rank is set in the global document.

I have done the same regarding the coding and also end up with a blank page. I see you say to set the $id=$_GET['id']; for this guys particular problem. So how do I set up a $id=$_GET['id']; and what does this actually mean and do?

So far all I can do to allow/refuse access to a page is use a $rank_check command at the top of the document. However, using this I can only seem to set one rank level with $rank_check == 5; for example or allow ranks above a certain level with $rank_check >=5;.

I would like to be able to specify which exact ranks I want to have access to the page content and have this so far:


<?php

$interactive = 1;
$page_title = "Council Offices Meeting Room";
$rank_check = 3;
include "../header.inc.php";

$game=$_GET['game'];

print "$openHTML";


if ($rank == 10 || $rank == 30 || $rank == 57 || $rank == 59 || $rank == 95 || $rank ==96 ||$rank ==97 || $rank ==98 || $rank ==99) 

{

print "$content";

}
else
{
		print "<center><p><p><br><br><p><font size=3>Sorry But you do not have access to the City Council Meeting Room. <br>This is a restricted area! Please click <a href=$base_url/city_offices/reception.php>here</a> to go back to the council offices";
}

$content .= "  PAGE CONTENT HERE  ";

print "$closeHTML";

?>




But as explained... all I get is a blank screen with the header working.

The log in page is where all the cookies and sessions are done so do I need to build a new session? Sorry but I am quite new to PHP so am a little vague and only know bits and pieces.


Would appreciate any help.
Thanks

Justin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1