Jump to content


Photo

help with IF statement


  • Please log in to reply
1 reply to this topic

#1 Seamless

Seamless
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 07 August 2006 - 08:35 AM

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:

<?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>";
}
?>

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:

<?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>";
}
?>

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:

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

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

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 August 2006 - 08:46 AM

Try:

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

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.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users