如何在php中检查变量是否为空

时间:2017-06-28 12:57:44

标签: php mysql for-loop if-statement

我有一个带有MYSQL的PHP​​代码,其中代码获取数据库选择并在表格中显示结果。

我需要的是能够检查所选变量是否为空。

如果它是空的我不想在表格中显示该字段。

的代码:

$sql = $wpdb->prepare("select i.siteID
     , i.siteNAME
     , i.equipmentTYPE
     , c.latitude
     , c.longitude
     , c.height 
     , o.ownerNAME
     , o.ownerCONTACT
     , x.companyNAME
     , y.subcontractorCOMPANY
     , y.subcontractorNAME
     , y.subcontractorCONTACT
  from site_info i
  LEFT  
  JOIN owner_info o
    on i.ownerID = o.ownerID
  LEFT  
  JOIN company_info x
    on i.companyID = x.companyID
  LEFT 
  JOIN subcontractor_info y
    on i.subcontractorID = y.subcontractorID
    LEFT JOIN site_coordinates2 c
    on i.siteID=c.siteID 
    where 
    i.siteNAME = %s
    AND 
    o.ownerID = %d
    AND 
    x.companyID = %d
   ",$site_name,$owner_name,$company_name);

 $query_submit =$wpdb->get_results($sql, OBJECT);

    echo "<br>";
    echo "<br>";




// table that will dsiplay the results based on the user's selection //
echo "<table class='t1' width='30%'> ";
echo     "<tr>";
echo           "<th>Site Name</th>";
echo           "<th>Owner Name</th>";
echo           "<th>Company Name</th>";
//echo           "<th>Subcontractor Name</th>";
echo           "<th>Site ID</th>";
echo           "<th>Equipment Type</th>";
echo           "<th> Lattitude</th>";
echo           "<th>Longitude </th>";
echo           "<th> Height</th>";
echo           "<th> Owner Contact</th>";
echo           "<th> Sub Contact</th>";
echo           "<th> Sub company Name</th>";
echo   "</tr>";  


foreach ($query_submit as $obj) {

echo   "<tr>";       
echo         "<td>".$obj->siteNAME."</td>";
echo         "<td>".$obj->ownerNAME."</td>";
echo         "<td>".$obj->companyNAME."</td>";
   if(!$obj->subcontractorNAME){
      echo           "<th>Subcontractor Name</th>";
      echo           "<td>".$obj->subcontractorNAME."</td>";

   }
   else{

   }
echo         "<td>".$obj->siteID."</td>";
echo         "<td>".$obj->equipmentTYPE."</td>";
echo         "<td>".$obj->latitude."</td>";
echo         "<td>".$obj->longitude."</td>";
echo         "<td>".$obj->height."</td>";
echo         "<td>".$obj->ownerCONTACT."</td>";
echo         "<td>".$obj->subcontractorCONTACT."</td>";
echo         "<td>".$obj->subcontractorCOMPANY."</td>";
echo  "</tr>";            

} 

0 个答案:

没有答案