Jump to content

[SOLVED] If Statement >= 0


yandoo

Recommended Posts

Hi I was hoping for a little assistance with my current php problem..

 

Im using a DATEDIFF query to find the number of days there are between the current date and a date stored in my database. This part is all working fine :)

The problem is when i try and add an if statement to the query. I am trying to make it so that if the days are <= 0 then to not output the data for that particular record..

 

$sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` "; 

$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}
while ($row = mysql_fetch_assoc($result)) {
    echo $row["intval"]."<br />\n";	

}

 

Ive tried to add

if ($result <= 0) {
  echo "N/A";    
}

 

But the results i get seem strange as they seem to output the N/A and also the result for that record too...

 

I just need it to NOT output the record if the number of days are <= 0.

 

Im obvoously missing something here ..

 

Any help would be ace

 

Thank You  ;D

Link to comment
Share on other sites

You could just put exit; in that if() statement too.

 

$sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` ";

$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}
if ($result <= 0) {
  echo "N/A";  
  exit; 
}
while ($row = mysql_fetch_assoc($result)) {
    echo $row["intval"]."<br />\n";   

}

 

Without the exit; it will run the while() loop.

Link to comment
Share on other sites

If Else?

 

$sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` ";

$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
}
else if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
}
else if ($result <= 0) {
  echo "N/A";  
}
else {
while ($row = mysql_fetch_assoc($result)) {
    echo $row["intval"]."<br />\n";   
}
}

Link to comment
Share on other sites

Hi thanks for the reply,

 

i have 3 records that are outputted and only 1 record has <= 0 days left and wouldnt that mean that the other 2 records wouldnt be displayed also???

 

Only need the record with <= 0 days to not output the other 2 must display as normal.

 

Thanks

 

 

Link to comment
Share on other sites

$result isn't the value returned by the query. It is a result resource that contains any rows retrieved by the query.

 

One of the great points of using a database is to retrieve just the rows you want in the order you want them.

 

If you are trying to retrieve only the rows WHERE the datediff() expression is greater than zero, use that as a condition in your query -

 

$sql = "SELECT * FROM `vegeschedule` WHERE DATEDIFF(ThinDate, CURDATE()) > 0"; 

Link to comment
Share on other sites

<?php
$sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` "; 

$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}
while ($row = mysql_fetch_assoc($result)) {
    if ($row["intval"] <= 0) echo 'N/A'; else echo $row["intval"];
    echo "<br />\n";	
}
?>

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.