tags:

views:

178

answers:

4

The code below works great. It prints out items in a MySQL database as an HTML table. However, the database has entries where the first column (the variable "site") is blank. How can I have this table exclude rows / entries where "site" is blank?

$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 "";
}
A: 

Just put the following before the print calls in your while loop:

if (trim($row['site']) == '') {
    // skip output of this row if site is empty
    continue;
}
Gumbo
why not in the where clause?
tharkun
@tharkun: Didn’t think of that.
Gumbo
A: 

The simplest solution would be to insert the following line into your inner while block:

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

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

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

Edit: Actually, it looks like that $itemstotal variable isn't needed; it's being calculated as a "virtual" itemstotal column in the SQL query. If you replace "number_format($itemstotal)" with "number_format($row['itemstotal'])" a few lines down, you can get rid of the "$itemstotal = ..." line entirely.

Ben Blank
`empty` only works with variable.
Gumbo
Gumbo is right, you cannot use empty(trim($foo)). It will not work: http://www.php.net/empty
Abinadi
why not in the where clause?
tharkun
@Gumbo, Abinadi — Clearly, my PHP-fu is getting weaker; fixed. @tharkun — Comment spam is bad, mmmk?
Ben Blank
but this is just hilarious guys! sorry for comment spam if you think it is... but the comment is indeed true at evey post I wrote it...
tharkun
+7  A: 

Add a where clause to the SQL query.

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

should work.

However, if you want the rows where site is blank for other PHP options, it would be better to filter out the blank sites in the PHP and not the MySQL query.

DeadHead
Duh, me. I like this *much* better than my own solution. :-)
Ben Blank
nice fix. Easy too.
Tony C
Elegant solution. +1
Abinadi
@..everyone: Thanks!
DeadHead
As they say in Mexico, Gracias. It works.
A: 
    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>";
}
}
Tim Hoolihan
why not in the where clause?
tharkun