PHP Exception of empty lines when printing a table.

The code below works fine. It prints elements in a MySQL database as an HTML table. However, there are records in the database where the first column (the "site" variable) is empty. How can I make this table exclude rows / records where the "site" is empty?

$result=mysql_query("SHOW TABLES FROM database LIKE '$find'")
or die(mysql_error());


if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){
print "<p class=\"topic\">$table[0]</p>\n";
$r=mysql_query("SELECT * , itemsa - itemsb AS itemstotal FROM `$table[0]` ORDER BY itemstotal DESC");



print "<table class=\"navbar\">\n";
while($row=mysql_fetch_array($r)){


$itemstotal = $row['itemsa'] - $row['itemsb']; 

print "<tr>";


print "<td class='sitename'>".'<a type="amzn" category="books" class="links2">'.$row['site'].'</a>'."</td>";

print "<td class='class1'>".'<span class="class1_count" id="class1_count'.$row['id'].'">'.number_format($itemstotal).'</span>'."</td>";
print "<td class='selector'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Select.'</a>'.'</span>'."</td>";
}
print "</tr>\n";
}
print "</table>\n";



}
else{
print "";
}
+3
source share
4 answers

Add the where clause to the SQL query.

WHERE `site` <> ''
or
WHERE `site` != ''

must work.

However, if you need strings where the site is empty for other PHP parameters, it is better to filter out empty sites in PHP, rather than in a MySQL query.

+7
source

Just put the following before the printcalls in the loop while:

if (trim($row['site']) == '') {
    // skip output of this row if site is empty
    continue;
}
0
source

while:

while($row=mysql_fetch_array($r)){

if (!trim($row['site'])) continue; // skip empty rows

$itemstotal = $row['itemsa'] - $row['itemsb'];

Edit: Actually, it looks like the variable is $itemstotalnot needed; it is calculated as a "virtual" column itemstotalin the SQL query. If you replace " number_format($itemstotal)" with " number_format($row['itemstotal'])" with a few lines down, you can completely get rid of the line " $itemstotal = ...".

0
source
    while($row=mysql_fetch_array($r)){
if($row['site'] != "") {
    $itemstotal = $row['itemsa'] - $row['itemsb']; 
    print "<tr>";
    print "<td class='sitename'>".'<a type="amzn" category="books" class="links2">'.$row['site'].'</a>'."</td>";
    print "<td class='class1'>".'<span class="class1_count" id="class1_count'.$row['id'].'">'.number_format($itemstotal).'</span>'."</td>";
    print "<td class='selector'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Select.'</a>'.'</span>'."</td>";
}
}
0
source

Source: https://habr.com/ru/post/1712701/


All Articles