仅显示具有动态sql查询结果的值的列

时间:2017-01-23 18:40:37

标签: php sql dynamic html-table

我有一个html表单来读取sql-database中的数据。根据所选标准,结果显示在动态生成的表中。这适用于以下代码。 SELECT查询根据所选标准组成。

  $sql = "SELECT DISTINCT $selection FROM $tabelle WHERE $whereclause";

  $result = mysqli_query($db, $sql) or die("<b>No result</b>"); //Running 
     the query and storing it in result
  $numrows = mysqli_num_rows($result);  // gets number of rows in result 
     table
  $numcols = mysqli_num_fields($result);   // gets number of columns in 
     result table
  $field = mysqli_fetch_fields($result); // gets the column names from the 
     result table

  if ($numrows > 0) {

  echo "<table id='myTable' >";

  echo "<thead>";
  echo "<tr>";
  echo "<th>" . 'Nr' . "</th>";

  for($x=0;$x<$numcols;$x++){
          $key = array_search($field[$x]->name, $custom_column_arr);
          if($key !== false){
              echo "<th>" . $key . "</th>";
          }else{
              echo "<th>" . $field[$x]->name . "</th>";
          }
      }

  echo "</tr></thead>";

  echo "<tbody>";

  $nr = 1;

  while ($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $nr . "</td>";
    for ($k=0; $k<$numcols; $k++) {    //  goes around until there are no 
    columns left
          echo "<td>" . $row[$field[$k]->name] . "</td>"; //Prints the data
    }
  echo "</tr>";
  $nr = $nr + 1;


  }     // Ende der while-Schleife

  echo "</tbody></table>";
  }
  }

  mysqli_close($db);

现在,我想只显示那些包含至少一个值的列,而不是那些包含NULL,0,空字符串或空的列。我的问题是,$numcols也计算空列。

0 个答案:

没有答案