paulonline2501 Posted June 3, 2014 Share Posted June 3, 2014 (edited) 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 June 3, 2014 by as_bold_as_love Link to comment Share on other sites More sharing options...
Don Jajo Posted June 3, 2014 Share Posted June 3, 2014 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 Link to comment Share on other sites More sharing options...
shreyaskudav Posted June 3, 2014 Share Posted June 3, 2014 It shouldn't show North & Islands?? I see no where it is been echoed? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 There's nothing wrong with the code, it just means the value from the database doesn't match the values you're checking for. Link to comment Share on other sites More sharing options...
Ingolme Posted June 3, 2014 Share Posted June 3, 2014 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 More sharing options...
paulonline2501 Posted June 3, 2014 Author Share Posted June 3, 2014 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 More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 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 More sharing options...
Don Jajo Posted June 3, 2014 Share Posted June 3, 2014 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 More sharing options...
Agony Posted June 5, 2014 Share Posted June 5, 2014 (edited) 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 June 6, 2014 by Agony Link to comment Share on other sites More sharing options...
birbal Posted June 5, 2014 Share Posted June 5, 2014 & 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now