Jump to content

If a statement equals nothing, How do I display nothing?


FatDank

Recommended Posts

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.

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

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.

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>

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

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.

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.

Archived

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

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