Jump to content

help with IF statement


Seamless

Recommended Posts

Hi,

when extracting data from my database, i have placed and IF statement to output the results.

For example in the db column 'city' i have set the default to '0', when a user updates their information they often input what i expect and that is their City. So i inserted an IF statement shown below:

[code]
<?php
//db connection info etc..
if($row[city] == "0"){ // the user did not input their city
  echo "<p>Not Given</p>";
}else{ // the user inserted information
  echo "<p>$row[city]</p>";
}
?>
[/code]

While browsing through my database i noticed that some users 'city' columns contain nothing, which is ok, because it is not a required field in my form. So i modified my IF statement below:

[code]
<?php
//db connection info etc..
if($row[city] == "0"){ // the user did not input their city
  echo "<p>Not Given</p>";
}elseif($row[city] == ""){ // the db column conatins nothing
  echo "<p>Not Given</p>";
}else{ // the user inserted information
  echo "<p>$row[city]</p>";
}
?>
[/code]

But this does not seem to work. I don't know if the user entered nothing or if they entered a space or even if using the "" in my code is correct.

Now i could correct this when i parse the form by adding an if statement stating:

[code]
<?php
if(!isset($_POST[city]){
  $city = "0";
}
?>
[/code]

But once again if it contains a space how do specifiy that in my if statement.

I hope some one can shine some light on this for me,

Thanks in advance

Seamless
Link to comment
https://forums.phpfreaks.com/topic/16770-help-with-if-statement/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.