Problem with IF statement

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, I'm lost and about to pull what hair I have out! I want the below table to display a date if the field has one, but if not, then just display <td>&nbsp;</td>. Here is my code, please let me know where I'm going wrong:

Code: [ Select ]
 
                                $tk_result = mssql_query("SELECT * FROM SNP_todo
                                        WHERE (user_id = {$user})", $connection);
                                    if (!$tk_result) {
                                        die("Database query failed: " . mssql_error());
                                    }
                                while($tk_row = mssql_fetch_array($tk_result)) {
                                echo "<tr>";
                                        echo "<td><a href=\"edit_task.php?tsk={$tk_row['task_id']}\">{$tk_row['task_name']}</a></td>";
                                        echo "<td>{$tk_row['status']}</td>";
                                        echo "<td>{$tk_row['priority']}</td>";                                        
                                        if (!empty($tk_row['due_date'])) {
                                           echo "<td>{$tk_row['due_date']}</td>";
                                        } else {
                                            echo "<td>&nbsp;</td>";                                            
                                        }
                                echo "</tr>";
                                }
 
  1.  
  2.                                 $tk_result = mssql_query("SELECT * FROM SNP_todo
  3.                                         WHERE (user_id = {$user})", $connection);
  4.                                     if (!$tk_result) {
  5.                                         die("Database query failed: " . mssql_error());
  6.                                     }
  7.                                 while($tk_row = mssql_fetch_array($tk_result)) {
  8.                                 echo "<tr>";
  9.                                         echo "<td><a href=\"edit_task.php?tsk={$tk_row['task_id']}\">{$tk_row['task_name']}</a></td>";
  10.                                         echo "<td>{$tk_row['status']}</td>";
  11.                                         echo "<td>{$tk_row['priority']}</td>";                                        
  12.                                         if (!empty($tk_row['due_date'])) {
  13.                                            echo "<td>{$tk_row['due_date']}</td>";
  14.                                         } else {
  15.                                             echo "<td>&nbsp;</td>";                                            
  16.                                         }
  17.                                 echo "</tr>";
  18.                                 }
  19.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Nevermind, I figured out for some reason, it's not seeing a blank field as empty. I changed the field to NULL and the query worked. So, how do I make this work where the field is just blank?

Jordon
  • Bogey
  • Bogey
  • Genius
  • Bogey
  • Posts: 8363
  • Loc: USA

Post 3+ Months Ago

Oh... I pointed this out some time before here

Here it is again...

When a field in a MySQL database is empty, it would be set to 'false' (As a string... not a false as a Boolean).

Here is what you should do
PHP Code: [ Select ]
<?php
if($tk_row['field_name'] == false)
{
    echo "<td>&nbsp;</td>";
}
else
{
     echo "<td>{$tk_row['field_name']}</td>";
}
?>
  1. <?php
  2. if($tk_row['field_name'] == false)
  3. {
  4.     echo "<td>&nbsp;</td>";
  5. }
  6. else
  7. {
  8.      echo "<td>{$tk_row['field_name']}</td>";
  9. }
  10. ?>

That's about it...

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 76 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.