【问题标题】:How to fix the display of the table using PHP and html如何使用 PHP 和 html 修复表格的显示
【发布时间】:2017-12-16 18:28:36
【问题描述】:

我有一个代码,它使用 foreach 循环 和 if 语句在表格中显示获取的数据,以检查该字段是否为空,但问题是表格显示在每一行字段名。

如何修复显示?如果我知道错误在 foreach 循环中,我将不胜感激。

使用 Doomenik 的答案后

代码:

 $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 "<table class='t1' width='30%'> ";
  echo     "<tr>";
  echo           "<th>Site ID</th>";
  echo           "<th>Site Name</th>";

  if(!empty($query_submit->latitude)) { 
     echo "<th>Lattitude</th>";


  } else { echo '<th></th>'; } 



  if(!empty($query_submit->longitude)){ 
     echo "<th>Longitude</th>";


  } else { echo '<th></th>'; } 

  echo "<th>Owner Name</th>";

  if(!empty($query_submit->ownerCONTACT))
  { 
     echo "<th>Owner Contact</th>";


  } else { echo '<th></th>'; } 
  echo           "<th>Company Name</th>";
  if(!empty($query_submit->equipmentTYPE)){ 
     echo "<th>Equipment Type</th>";


  } else { echo '<th></th>'; } 

  if(!empty($query_submit->height)) { 
     echo "<th>Height</th>";


  } else { echo '<th></th>'; } 

  if(!empty($query_submit->subcontractorCONTACT)){ 
     echo "<th>Sub Contact</th>";


  } else { echo '<th></th>'; } 


  if(!empty($query_submit->subcontractorCOMPANY)) { 
     echo "<th>Sub company Name</th>";
  } else { echo '<th></th>'; } 
  echo   "</tr>";
  foreach ($query_submit as $obj) {
    echo   "<tr>";   
    echo         "<td>".$obj->siteID."</td>";
    echo         "<td>".$obj->siteNAME."</td>";
    if(!empty($obj->latitude)) { 
         echo "<td>".$obj->latitude."</td>";
    } else { echo '<td></td>'; } 


    if(!empty($obj->longitude)) { 
        echo "<td>".$obj->longitude."</td>";
    } else { echo '<td></td>'; } 

    echo "<td>".$obj->ownerNAME."</td>";
    if(!empty($obj->ownerCONTACT)) { 
        echo "<td>".$obj->ownerCONTACT."</td>";
    } else { echo '<td></td>'; } 

    echo "<td>".$obj->companyNAME."</td>";
    if(!empty ($obj->equipmentTYPE)){ 
        echo "<td>".$obj->equipmentTYPE."</td>";
    } else { echo '<td></td>'; } 

    if(!empty($obj->height)){ 
        echo "<td>".$obj->height."</td>";
    } else { echo '<td></td>'; } 

    if(!empty($obj->subcontractorCONTACT)){ 
        echo "<td>".$obj->subcontractorCONTACT."</td>";
    } else { echo '<td></td>'; } 


    if(!empty($obj->subcontractorCOMPANY)){ 
        echo "<td>".$obj->subcontractorCOMPANY."</td>";
    } else { 
        echo '<td></td>'; 
    } 
    echo "</tr>";
}
 echo "</table>";

【问题讨论】:

    标签: php mysql foreach html-table


    【解决方案1】:

    ** 删除了提供新信息的原因 *

    基本上,我将 th 的创建移到了 for each 的前面,否则你将创建这一行的每个循环。因此,这将首先创建“”行,然后在每个数据行的内部创建。你可以直接在for each下看到,我打开一个“”并在循环结束时关闭它。一切完成后,我关闭了桌子。

    编辑 3: 这是针对您的情况的解决方案,因为我没有太多次它是一个非常快速和肮脏的解决方案。有很大的改进潜力,但这将取决于你

    echo "<table class='t1' width='30%'> ";
    echo     "<tr>";
    echo           "<th>Site ID</th>";
    echo           "<th>Site Name</th>";
    
    //First we gonna check which columns contain at least 1 value
    $latitude = false;
    $longitude = false;
    $ownerCONTACT = false;
    $equipmentTYPE = false;
    $height = false;
    $subcontractorCONTACT = false;
    $subcontractorCOMPANY = false;
    
    foreach ($query_submit as $header_obj) {
        //If only one cel in the column has an value we put his variable to true
        if(!empty($header_obj->latitude)) {
            $latitude = true;
        }
        if(!empty($header_obj->longitude)) {
            $longitude = true;
        }
        if(!empty($header_obj->ownerCONTACT)) {
            $ownerCONTACT = true;
        }
        if(!empty($header_obj->equipmentTYPE)) {
            $equipmentTYPE = true;
        }
        if(!empty($header_obj->height)) {
            $height = true;
        }
        if(!empty($header_obj->subcontractorCONTACT)) {
            $subcontractorCONTACT = true;
        }
        if(!empty($header_obj->subcontractorCOMPANY)) {
            $subcontractorCOMPANY = true;
        }
    }
    
    //Now we check and if there is content we place the th
    if($latitude === true) {
        echo "<th>Lattitude</th>";
    }
    
    if($longitude === true){
        echo "<th>Longitude</th>";
    }
    
    echo "<th>Owner Name</th>";
    
    if($ownerCONTACT === true) {
        echo "<th>Owner Contact</th>";
    }
    
    echo "<th>Company Name</th>";
    
    if($equipmentTYPE === true){
        echo "<th>Equipment Type</th>";
    }
    
    if($height === true) {
        echo "<th>Height</th>";
    }
    
    if($subcontractorCONTACT === true){
        echo "<th>Sub Contact</th>";
    }
    
    if($subcontractorCOMPANY === true) {
        echo "<th>Sub company Name</th>";
    }
    echo "</tr>";
    
    //Here we loop again trough the obj, we write now every row.
    //If we had an empty column (so also no head) we will skip that value.
    foreach ($query_submit as $obj) {
        echo   "<tr>";
        echo         "<td>".$obj->siteID."</td>";
        echo         "<td>".$obj->siteNAME."</td>";
    
        if($latitude === true) {
            //In this situation the column exists but we dont know if the specific cell has 
            // a value so we check this here
            if(!empty($obj->latitude)) {
                echo "<td>".$obj->latitude."</td>";
            } else {
                //If it doesnt has one we make at least an empty cell so the layout gets not broken
                echo "<td></td>";
            }
        }
    
        if($longitude === true){
            if(!empty($obj->longitude)) {
                echo "<td>".$obj->longitude."</td>";
            } else {
                echo "<td></td>";
            }
        }
    
        echo "<td>".$obj->ownerNAME."</td>";
    
        if($ownerCONTACT === true) {
            if(!empty($obj->ownerCONTACT)) {
                echo "<td>".$obj->ownerCONTACT."</td>";
            } else {
                echo '<td></td>';
            }
        }
    
        echo "<td>".$obj->companyNAME."</td>";
    
        if($equipmentTYPE === true){
            if(!empty ($obj->equipmentTYPE)){
                echo "<td>".$obj->equipmentTYPE."</td>";
            } else {
                echo '<td></td>';
            }
        }
    
        if($height === true) {
            if(!empty($obj->height)){
                echo "<td>".$obj->height."</td>";
            } else { echo '<td></td>'; }
        }
    
        if($subcontractorCONTACT === true){
            if(!empty($obj->subcontractorCONTACT)){
                echo "<td>".$obj->subcontractorCONTACT."</td>";
            } else {
                echo '<td></td>';
            }
        }
    
        if($subcontractorCOMPANY === true) {
            if(!empty($obj->subcontractorCOMPANY)){
                echo "<td>".$obj->subcontractorCOMPANY."</td>";
            } else {
                echo '<td></td>';
            }
        }
        echo "</tr>";
    }
    echo "</table>";
    

    【讨论】:

    • 我试过你的答案它没有用它只是在表格中显示第一个结果,其余的都出来了
    • 是的,看到你的照片后我明白了你的问题给我一点时间
    • 我需要检查表头是否没有我不想显示的值
    • 现在我又困惑了,你能告诉我们它应该是什么样子吗?你想在每一行都有一个头吗?只在第一行还是没有头?
    • 我需要在第一次显示字段名称(例如站点 ID)时,然后在其下方显示所有选择的值,而不必每次都显示字段名称
    【解决方案2】:

    看看这里https://www.w3schools.com/html/html_tables.asp,问题在于你在循环中迭代标题,而它们只应该在表格标记的开头被调用一次。 即只有标签应该被循环/迭代并从每个标签中取出标题标签。

    【讨论】:

    • 我知道我正在迭代标题,但我需要这个,因为并非所有标题都有值,所以我不想显示空的标题
    • 啊,好吧,我明白了,那么您必须调试并检查为什么那些 if 语句仍在添加标题,也许该值对您来说似乎是空的,但实际上不是,例如空字符串“ " 可能会被返回,并且实际上会在 if 语句中传递为 true,因为它的长度是 1
    【解决方案3】:

    您的 th 标签取决于 $obj ,每次循环时都可能不同。 因此,要克服这个问题,您可以做的是使表头静态,并且表行值将是空白或值,具体取决于您的 $obj 变量。 所以你需要搬出去

    <table class='t1' width='30%'>
    <tr>
    <th>Site ID</th>
    <th>Site Name</th>
    // all th tags
    </tr>
     <?php foreach ($query_submit as $obj) { // then foreach 
      echo   "<tr>";   
      echo         "<td>".$obj->siteID."</td>";
      echo         "<td>".$obj->siteNAME."</td>";
       ?><td><?php if(!empty($obj->latitude))
      {  echo $obj->latitude;} ?></tr>  // your td will be fixed contain of td will depend on your value.
      echo "<td>".$obj->siteID."</td>"; // and so on
    

    在这里获取您的表格 html 是使用 foreach 循环的基本 html 表格的示例。 希望这会对你有所帮助。

    <table>
    <tr>
    <th>heading 1</th>
     <th>heading 2</th>
    <th>heading 3</th>
    <th>heading 4</th>
    <th>heading 5</th>
    </tr>
    <?php foreach($array as $data){ ?>
    <tr>
    <td><?php if(!empty($data->data1))  { echo $data->data1; } ?></td>
    <td><?php if(!empty($data->data2))  { echo $data->data2; } ?></td>
    <td><?php if(!empty($data->data3))  { echo $data->data3; } ?></td>
    <td><?php if(!empty($data->data4))  { echo $data->data4; } ?></td>
    <td><?php if(!empty($data->data5))  { echo $data->data5; } ?></td>
    </tr>
     <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 2018-06-13
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 2016-03-03
      • 2015-06-16
      • 2019-04-27
      相关资源
      最近更新 更多