joaquin joaquin - 2 years ago 177
MySQL Question

Hide empty td if count value is empty in datatables

I have this script which works hidden NULL values in mysql db, but with EMPTY values always count them...so How can I hide this values too?

<table class="table table-bordered table-striped table-condensed bootstrap-datatable datatable" >
<thead>
<tr>
<th>Diagn&oacute;sticos</th>
<th class="center sorting_desc">Casos vistos</th>
</tr>
</thead>
<tbody>
<?
$sql = $conn->prepare("select diagnostico, count(diagnostico) from ( select diagnostico as diagnostico from DIAGNOSTICON WHERE id_doctor = $id_doctor union all select diagnostico1 as diagnostico from DIAGNOSTICON union all select diagnostico2 as diagnostico from DIAGNOSTICON union all select diagnostico3 as diagnostico from DIAGNOSTICON) t group by t.diagnostico order by count(diagnostico) desc ");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>\n";
echo "<td>";
if (!empty($row["diagnostico"]))
{
echo $row["diagnostico"]."</td>\n"; }
echo "<td>";
if (!empty($row["count(diagnostico)"]))
{
echo $row["count(diagnostico)"]."</td>\n";
echo "</tr>\n"; }
}
?>
</tbody>
</table>

Answer Source

Replace

count(diagnostico)

with

count(case when diagnostico = '' then NULL else diagnostico end)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download