Jump to content

Archived

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

Seamless

help with IF statement

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

Share this post


Link to post
Share on other sites
Try:

[code]<?php
if($row['city'] == "0" || ereg("^[ ]*$",$row['city'])){
echo "<p>Not Given</p>";
}else{
echo "<p>$row[city]</p>";
}
?>[/code]

This means- if the city is "0" OR the city is nothing OR the city is a row of spaces (doesnt matter how many)- echo "not given".


Orio.

Share this post


Link to post
Share on other sites

×

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.