FatDank Posted July 4, 2011 Share Posted July 4, 2011 Hi all. I am having a problem with this code I got. What it does is takes me to the url of a title. Example: www.example.com/index.php?status=24 All that works fine, however if you are just on index.php it displays the <div> that the status is ment to be in and the <div> has a blue background (which its ment to) but I dont want a blue box displaying if there is nothing in it. How do I get it so it doesnt display this <div>. I have a feeling it is something like: If status equals 0 display nothing else echo status However, I am new to php so can someone correct my code please? $sql = "SELECT id, post FROM comments WHERE id='" . mysql_real_escape_string($_GET['status']) . "'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_assoc($result); ?> <center> <div class="display-status"> <h3> <? echo nl2br($r['post']); ?> </h3> </div> </center> The class display-status is the blue box. Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/ Share on other sites More sharing options...
Fadion Posted July 4, 2011 Share Posted July 4, 2011 You can check if $_GET['status'] isset() or even if $r['post'] is not empty. I would go for the first choice, because it make more sense in your scenario. <?php if (isset($_GET['status'])) { ?> <div class="display-status"> <h3><?php echo nl2br($r['post']); ?></h3> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238129 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 Personally I would expand GuiltyGear's concept a bit.. and go something like if((isset($_GET['status']))AND(!empty($_GET['status']))AND(trim($_GET['status']) !== "")) { Which some may say is over kill. But a index.php?status= could be considered set just nothing in it. Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238132 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 Also if status is always going to be a numeric value.. I'd also throw in (is_numeric($_GET['status'])) Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238133 Share on other sites More sharing options...
FatDank Posted July 4, 2011 Author Share Posted July 4, 2011 Thanks for replying guys. So would my code look like this: <? $conn = mysql_connect('','','') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('',$conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT id, post FROM comments WHERE id='" . mysql_real_escape_string($_GET['status']) . "'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_assoc($result); if((isset($_GET['status']))AND(!empty($_GET['status']))AND(trim($_GET['status']) !== "")) { ?> <center> <div class="display-status"> <h3> <? echo nl2br($r['post']); ?> </h3> </div> </center> Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238174 Share on other sites More sharing options...
cyberRobot Posted July 4, 2011 Share Posted July 4, 2011 If $_GET['status'] isn't a valid value, you could even skip the MySQL query. Your code could be changed to: <?php $conn = mysql_connect('','','') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('',$conn) or trigger_error("SQL", E_USER_ERROR); if((isset($_GET['status']))AND(!empty($_GET['status']))AND(trim($_GET['status']) !== "")) { // find out how many rows are in the table $sql = "SELECT id, post FROM comments WHERE id='" . mysql_real_escape_string($_GET['status']) . "'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_assoc($result); //display box ?> <center> <?php } ?> As monkeytooth suggested, if status is always going to be a number, you should run an extra test to prevent query errors. But instead of is_numeric(), I would suggest that you use ctype_digit(): http://www.php.net/manual/en/function.ctype-digit.php <?php $conn = mysql_connect('','','') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('',$conn) or trigger_error("SQL", E_USER_ERROR); if((isset($_GET['status'])) AND (ctype_digit(trim($_GET['status'])) { // find out how many rows are in the table $sql = "SELECT id, post FROM comments WHERE id='" . mysql_real_escape_string($_GET['status']) . "'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_assoc($result); //display box ?> <center> <?php } ?> If you choose to use is_numeric(), keep in mind that the following will be considered numbers: 9.1 1e4 For more information, see: http://php.net/manual/en/function.is-numeric.php Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238240 Share on other sites More sharing options...
FatDank Posted July 4, 2011 Author Share Posted July 4, 2011 Nice. Thanks for the help dudes. Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238275 Share on other sites More sharing options...
TeNDoLLA Posted July 4, 2011 Share Posted July 4, 2011 I think it would be better to use intval() or (int) with a proper checks which ever you need to validate your int data. Since if you use ctype_digit, it will accept numbers like 012 and 00013 as a valid int. Sometimes it depends what you need though. Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238300 Share on other sites More sharing options...
cyberRobot Posted July 4, 2011 Share Posted July 4, 2011 I think it would be better to use intval() or (int) with a proper checks which ever you need to validate your int data. Since if you use ctype_digit, it will accept numbers like 012 and 00013 as a valid int. Sometimes it depends what you need though. Of course values with leading zeros could be caught after the query is processed. If it doesn't return a result, then it's invalid. Just like 5300 would be an invalid value if the database only contains 1000 entries. Quote Link to comment https://forums.phpfreaks.com/topic/241045-if-a-statement-equals-nothing-how-do-i-display-nothing/#findComment-1238356 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.