0

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!!

Columbus
  • 89
  • 1
  • 9
  • I'm sorry, I stopped reading when I spotted `mysql_`. Is there any particular reason you're using this? `array_column()` is a function added to a PHP version that supports both `mysqli` and `PDO` so that should not be an issue. Or.. was it that you overlooked the huge red block on [this](http://www.php.net/manual/en/function.mysql-connect.php) page? It's only been there for 4.5 years, so you could have missed it. – Xorifelse Jan 26 '17 at 21:07
  • 1
    Why are you mixing `mysql` and `mysqli` APIs in your project? – Rajdeep Paul Jan 26 '17 at 21:11
  • 2
    Possible duplicate of [How to integrate array\_column() and array\_filter()-blocks into dynamic table generation of sql results](http://stackoverflow.com/questions/41860220/how-to-integrate-array-column-and-array-filter-blocks-into-dynamic-table-gen) – Don't Panic Jan 26 '17 at 21:22
  • Thank you very much for your comment! @Xorifelse, I am aware that database queries are safe with PDO, only. However, I´m new to PDO and will first have to get acquainted with the work. It will be the next step after having solved the current problem. – Columbus Jan 26 '17 at 21:30
  • @Rajdeep Paul, I didn´t know that mixing of mysql and mysqli is a bad behaviour and so far, the code worked. – Columbus Jan 26 '17 at 21:32
  • @Columbus This means you're connecting to the same database 2 times, for 1 user? `mysql` and `mysqli` are 2 sepperate API's, both require a connection. – Xorifelse Jan 26 '17 at 21:32
  • @Columbus Read this SO thread, [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Rajdeep Paul Jan 26 '17 at 21:34
  • @Don´t Panic You are right, I posted this question yesterday, already. However, something went wrong and I was not found by anyone! Today, this works! – Columbus Jan 26 '17 at 21:34
  • That doesn't mean you should repost the same question. See [here](http://meta.stackoverflow.com/questions/280601/can-i-ask-the-same-question-twice) and [here](http://meta.stackoverflow.com/questions/266338/how-can-i-improve-my-questions-or-how-to-get-more-attention-for-my-questions). – Don't Panic Jan 26 '17 at 21:38
  • @Xorifelse, I found this code http://stackoverflow.com/questions/30966399 that does exactly what I need. And that´s why I am now trying to adapt my script accordingly. – Columbus Jan 26 '17 at 22:03
  • @Don´t Panic, thanks for your hint and I´m sorry, I didn´t know. I will take note of this in the future! – Columbus Jan 26 '17 at 22:07
  • @Columbus No worries. – Don't Panic Jan 26 '17 at 22:12
  • I tried the following: for($x=0;$x<$numcols;$x++){ $column[$x] = array_column($results, '$field[$x]'); if(array_filter($column[$x])) { echo " . $field[$x]->name . "; } } However, it didn´t work. When I checked $column with var_dump(), I got an Array with 7 empty values (the table has 7 columns). But $results and $field are displayed correctly. Could anyone tell me, how I could solve this problem? – Columbus Jan 27 '17 at 16:32

0 Answers0