Seamless Posted August 7, 2006 Share Posted August 7, 2006 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]<?phpif(!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 advanceSeamless Quote Link to comment Share on other sites More sharing options...
Orio Posted August 7, 2006 Share Posted August 7, 2006 Try:[code]<?phpif($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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.