Jump to content

php if else


paulonline2501

Recommended Posts

hi everyone,

 

I'm having trouble with a PHP if statement.

I'm getting a value back from a database.

the values stored in the DB aren't exactly what I want to display so im going to do a inline conversion on them.

I want to say for example: if the region = 'central' then display 'central Scotland'.

 

ive added my conversion but the value that is stored in the DB is showing rather than the converted value.

 

can anyone spot what is wrong?

 

UPDATE: OK, so it works for all of these apart from the "North & Islands" one. all othere convert, but this one doesn't. it doesn't change to "North, Highlands & Islands" it just stays as "North & Islands". the others change.

 

the code is below:

<h3><strong>Region:</strong>				<?php 								if($db_oregion=="Central"){echo("Central Scotland");}				elseif($db_oregion=="East"){echo("East Anglia");}				elseif($db_oregion=="Mid"){echo("Mid & West Wales");}				elseif($db_oregion=="North"){echo("North Wales");}				elseif($db_oregion=="North & Islands"){echo("North, Highlands & Islands");}				elseif($db_oregion=="South"){echo("South Wales");}				else{echo($db_oregion);}								?>			</h3>
Edited by as_bold_as_love
Link to comment
Share on other sites

I'd recommend the switch() structure for this case.

 

Justsomeguy said it clearly.

 

Check to see what value the string has, var_dump() will be more useful as it will tell you how many characters the string has:

var_dump($db_oregion);

I'm suspecting it's the & problem that Don Jajo mentioned.

Link to comment
Share on other sites

elseif(htmlentities($db_oregion)==htmlentities("North & Islands")){echo("North, Highlands & Islands");}
switch ($db_oregion) {				case "Central":					echo "Central Scotland";					break;				case "East":					echo "East Anglia";					break;				case "Mid":					echo "Mid & West Wales";					break;				case "North":					echo "North Wales";					break;				case "North & Islands":					echo "North, Highlands & Islands";					break;				default:					echo "$db_oregion";}				

thanks for all the replies.

 

I tried using htmlentities [see above], and switch [see above] but neither worked. I wasn't sure how to do a switch with htmlentities.

 

 

 

however, its still not working.

 

as I said: the conversion should change entries coming back from the DB from "North & Islands" to "North, Highlands & Islands".

this is the only conversion that is not working.

the other conversions are all working.

the values are correct in the DB.

"North & Islands" is displaying incorrectly so I know the values in the DB are what i'm expecting, there just not converting.

 

 

 

 

 

 

 

asd

Link to comment
Share on other sites

Just print out the damn thing and see what it is, why are you trying to guess?

<pre><?php var_dump($db_oregion);?></pre>
If you don't want to use a pre tag, then print it out and view the page source to see what it actually printed instead of what the browser is rendering.

"North & Islands" is displaying incorrectly so I know the values in the DB are what i'm expecting, there just not converting.

No they are not what you are expecting. If they were what you are expecting, then the code would also be doing what you are expecting. PHP is not so fundamentally broken that a simple boolean comparison fails. The problem is that you are assuming things that are not true, and you are not verifying them. Stop assuming, and start verifying.
Link to comment
Share on other sites

 

 

elseif(htmlentities($db_oregion)==htmlentities("North & Islands")){echo("North, Highlands & Islands");}

I don't mean adding htmlentities here, am saying on adding the value to db and that's by the way. Just print the values and see what they are returning first :)

Link to comment
Share on other sites

North & Islands

 

might be breaking it. a single & would be considered as an operatoras in "if var is equal to north and islands is true".

var_dump($db_oregion=="North & Islands");

It should ether return true or false.

 

 

So use single quotes(the specials will not be processed) or escape it like: &

Edited by Agony
Link to comment
Share on other sites

& is not special character in context of string literal. It will be treated as character inside double and single quotes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...