2014-09-30 78 views
1

我需要帮助如何添加一个“if else”语句到我的PHP脚本,我需要new_license_issued_date字段为空,如果为空,而不是01/01/1970像它目前正在拉动....请帮忙!谢谢添加PHP,如果其他人的代码

<table cellpadding='0' cellspacing='0' border='0' class='display' id='table' width='100%'> 
    <thead> 
    <tr> 
     <th>Emp Code</th> 
     <th width='130'>Emp Name</th> 
     <th width=100>Manager</th> 
     <th width=150>Business Unit</th> 
     <th width=240>Training Course Name</th>  
     <th width=65>Issued Date</th> 
     <th width=65>Expiry Date</th> 
     <th width=100>Cert Number</th> 

    </tr> 
    </thead> 
    <tbody> 
    <?php 
    //display the results 
    while($row = mssql_fetch_array($result)) 
    { 
    echo "<tr> 
     <td width=40>" . $row['Emp Code'] . "</td> 
     <td>". $row['new_EmployeeName'] . "</td> 
     <td>". $row['Manager'] . "</td>  
     <td>". $row['BusinessUnit'] . "</td> 
     <td>". $row['new_TrainingCourseLookName'] . "</td> 
     <td>". date('d-M-Y', strtotime($row['new_license_issued_date'])) . "</td> 
     <td>". date('d-M-Y', strtotime($row['new_license_expiry_date'])) . "</td> 
     <td>". $row['new_license_no'] . "</td> 
    </tr>"; 
    } 

    echo "</tbody> 
    </table> 
    <br> 
     <p><a href='index.php'><< Back to Portal</p> 
    </div>"; 
    //close the connection 
    mssql_close(); 
    ?> 
    <!-- Le javascript 
    ================================================== --> 
    <!-- Placed at the end of the document so the pages load faster --> 
    <script type="text/javascript" language="javascript" src="assets/js/jquery.js"></script> 
    <script type="text/javascript" language="javascript" src="assets/js/jquery.dataTables.js">      </script> 

    <script type='text/javascript' charset='utf-8'> 
      $(document).ready(function() { 
       $('table').dataTable(); 
      }); 
    </script> 
    </body> 
</html> 

回答

1

使用三元运算符。

<td>". (is_null($row['new_license_issued_date']) 
       ? "NULL" 
       : date('d-M-Y', strtotime($row['new_license_issued_date']))) ."</td> 
+0

省长,非常感谢你:-) – 2014-09-30 10:35:42

+0

不客气@JohnOwen – 2014-09-30 10:38:37

0

你试过:

($row['new_license_issue_date'] ? date('d-M-Y', strtotime($row['new_license_issue_date'])) : '&nbsp'); 

0

或者如果它更容易让你使用:

echo "<td>"; 

    if($row['new_license_issued_date'] !== NULL){ 
     echo date('d-M-Y', strtotime($row['new_license_issued_date'])); 
    } else { 
     echo "&nbsp;"; 
    } 

echo "</td>"; 
+0

使用''==而不是'=' – 2014-09-30 10:41:26

+0

!你是对的...我还没有看到......感谢修正! – 2014-09-30 18:11:15