I want to read out data from an sql-database an show them in a table. This works well. Now, I would like to show only those columns with at least one value in it and not the empty ones (containing NULL, 0, empty string). This works with the following example:
enter code here
    <TABLE width="500" border="1" cellpadding="1" cellspacing="1">
    <?php
    $query = mysql_query("SELECT * FROM guestbook", $db);
    $results = array();
    while($line = mysql_fetch_assoc($query)){
    $results[] = $line;
    $Name = array_column($results, 'Name');
    $Home = array_column($results, 'Home');
    $Date = array_column($results, 'Date');
    $Emptycolumn = array_column($results, 'Emptycolumn');
    $Comment = array_column($results, 'Comment');
    $City = array_column($results, 'City');
    }
    echo "<TR>";
    if(array_filter($Name)) {echo "<TH>Name</TH>";}
    if(array_filter($Home)){echo "<TH>Home</TH>";}
    if(array_filter($Date)){echo "<TH>Date</TH>";}
    if(array_filter($Emptycolumn)){echo "<TH>Emptycolumn</TH>";}
    if(array_filter($Comment)){echo "<TH>Comment</TH>";}
    if(array_filter($City)){echo "<TH>City</TH>";}
    echo "</TR>";
    $query = mysql_query("SELECT * FROM guestbook", $db);
    while($line = mysql_fetch_assoc($query)){
    echo "<TR>";
  if(array_filter($Name)) {echo "<TD>".$line['Name']."</TD>";}
  if(array_filter($Home)) {echo "<TD>".$line['Home']."</TD>";}
  if(array_filter($Date)) {echo "<TD>".$line['Date']."</TD>";}
  if(array_filter($Emptycolumn)) {echo "<TD>".$line['Emptycolumn']."</TD>";}
  if(array_filter($Comment)) {echo "<TD>".$line['Comment']."</TD>";}
  if(array_filter($City)) {echo "<TD>".$line['City']."</TD>";}
  echo "</TR>";
  }
  ?>
  </TABLE>
Since the column-names of my table are highly variable (depending on the query), the table is generated by looping through the result-array, first the column-names, then the values in the rows:
enter code here
 $sql = "SELECT DISTINCT $selection FROM $tabelle WHERE  
 $whereclause"; //will be changed to PDO
 $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;
 }     // End of while-loop
 echo "</tbody></table>";
 }
 }
 mysqli_close($db);
Now, I tried to integrate the array_column() and array_filter()-blocks of the example above into the loops, but unfortunately, it didn´t work. I´m sure, this is easy for a professional and I would be very grateful, if someone could help me with this problem! Thank you very much in advance!!
