【问题标题】:how to fix the displayed table in php如何在php中修复显示的表格
【发布时间】:2018-06-13 21:17:37
【问题描述】:

我有一个从文本文件中读取并允许用户进行搜索的代码,然后系统将结果显示在一个包含文件名和行号的表中。

我需要的是删除空的列。

我将不胜感激。

截图:

表格的错误在哪里?

表格代码和样式:

   //display the table
    echo '<table class = "minimalistBlack" border=2>';

    $filenameHtml    = '<tr>';
    $lineNumberHtml    = '<tr>';
    foreach ($result as $item){
     $filename = isset($item['filename']) ? $item['filename'] : '';
     $lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
     //$filenameHtml .= "<th>$filename</th>";
     $new_filename = str_replace('.txt', '.pdf',$filename);
     $filenameHtml .= "<th><a href ='".$new_filename."'target='_blank'>$filename</a></th>";
     $lineNumberHtml .= "<td>$lines</td>";
    }
    $filenameHtml    .= '</tr>';
    $lineNumberHtml  .= '</tr>';

    echo $filenameHtml.$lineNumberHtml;
    echo '</table>';
}
?>

<html>
    <head>
    </head>
    <meta http-equiv="Content-Language" content="ar-sa">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
          #form {
        background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        margin: auto;
        width: 200px;
        height: 200px;
        position: absolute;


        font-family: Tahoma, Geneva, sans-serif;
        font-size: 14px;
        font-style: italic;
        line-height: 24px;
        font-weight: bold;
        color: #09C;
        text-decoration: none;
        border-radius: 10px;
        padding: 10px;
        border: 1px solid #999;
        border: inset 1px solid #333;
        -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
      }


      table.minimalistBlack {
  border: 3px solid #80E0FF;
  width: 100%;
  text-align: left;
  border-collapse: collapse;
}
table.minimalistBlack td, table.minimalistBlack th {
  border: 1px solid #000000;
  padding: 5px 4px;
}
table.minimalistBlack tbody td {
  font-size: 12px;
}
table.minimalistBlack thead {
  background: #CFCFCF;
  background: -moz-linear-gradient(top, #dbdbdb 0%, #d3d3d3 66%, #CFCFCF 100%);
  background: -webkit-linear-gradient(top, #dbdbdb 0%, #d3d3d3 66%, #CFCFCF 100%);
  background: linear-gradient(to bottom, #dbdbdb 0%, #d3d3d3 66%, #CFCFCF 100%);
  border-bottom: 3px solid #000000;
}
table.minimalistBlack thead th {
  font-size: 15px;
  font-weight: bold;
  color: #000000;
  text-align: left;
}
table.minimalistBlack tfoot {
  font-size: 14px;
  font-weight: bold;
  color: #000000;
  border-top: 3px solid #000000;
}
table.minimalistBlack tfoot td {
  font-size: 14px;
}

【问题讨论】:

    标签: php css foreach html-table


    【解决方案1】:

    如果该列不包含值,则不要设置该列,如下所示:

    <?php
    
    echo '<table class = "minimalistBlack" border=2>';
    
    $filenameHtml    = '<tr>';
    $lineNumberHtml    = '<tr>';
    foreach ($result as $item){
    $filename = isset($item['filename']) ? $item['filename'] : '';
    $lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
    //$filenameHtml .= "<th>$filename</th>";
    $new_filename = str_replace('.txt', '.pdf',$filename);
    $filenameHtml .= !empty($filename) ? "<th><a href ='".$new_filename."'target='_blank'>$filename</a></th>" : ''; // added !empty()
    $lineNumberHtml .= !empty($lines) ? "<td>$lines</td>" : ''; // added !empty()
    }
    $filenameHtml    .= '</tr>';
    $lineNumberHtml  .= '</tr>';
    
    echo $filenameHtml.$lineNumberHtml;
    echo '</table>';
    

    【讨论】:

      【解决方案2】:

      如果文件名或行有值,您可以添加一个检查器。如果没有,则不显示。见下面的代码:

      echo '<table class = "minimalistBlack" border=2>';
      
          $filenameHtml    = '<tr>';
          $lineNumberHtml    = '<tr>';
          foreach ($result as $item){
           $filename = isset($item['filename']) ? $item['filename'] : '';
           $lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
           //$filenameHtml .= "<th>$filename</th>";
           $new_filename = str_replace('.txt', '.pdf',$filename);
      
           if(strlen($filename)>0 || strlen($lines)>0){ // Check if filename OR lines has value
      	     $filenameHtml .= "<th><a href ='".$new_filename."'target='_blank'>$filename</a></th>";
      	     $lineNumberHtml .= "<td>$lines</td>";
       	 }
          }
          $filenameHtml    .= '</tr>';
          $lineNumberHtml  .= '</tr>';
      
          echo $filenameHtml.$lineNumberHtml;
          echo '</table>';

      【讨论】:

        猜你喜欢
        • 2017-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多