Jump to content
Sign in to follow this  
paulonline2501

php if else

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

Share this post


Link to post
Share on other sites

I see no errors so far, as you say "North & Islands" doesn't change. Check if you did convert htmlentities on insertion which will convert "&" to entities

Share this post


Link to post
Share on other sites

There's nothing wrong with the code, it just means the value from the database doesn't match the values you're checking for.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...