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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.