I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount; } ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"??
regards
Neil
Echo sprintf("%01d", $count);
Warren Vail
Vail Systems Technology
mailto:warren@vailtech.net warren@vailtech.net
(510) 444-5380
_____
From: Neil Coghlan [mailto:neil@esl-lounge.com] Sent: Friday, April 09, 2010 9:50 AM To: support@drupal.org Subject: [support] Printing "0" when a db query returns nothing
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount; } ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"??
regards
Neil
that's worked fine for those members with 0 downloads, but for example, someone with 5 is now showing 50. ----- Original Message ----- From: Warren Vail To: support@drupal.org Sent: Friday, April 09, 2010 2:03 PM Subject: Re: [support] Printing "0" when a db query returns nothing
Echo sprintf("%01d", $count);
Warren Vail
Vail Systems Technology
warren@vailtech.net
(510) 444-5380
------------------------------------------------------------------------------
From: Neil Coghlan [mailto:neil@esl-lounge.com] Sent: Friday, April 09, 2010 9:50 AM To: support@drupal.org Subject: [support] Printing "0" when a db query returns nothing
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount; } ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"??
regards
Neil
------------------------------------------------------------------------------
-- [ Drupal support list | http://lists.drupal.org/ ]
On Fri, Apr 9, 2010 at 10:19 PM, Neil Coghlan neil@esl-lounge.com wrote:
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount;
print (is_int($output->DownloadCount) && (int) $output->DownloadCount > 0) ? $output->DownloadCount : NICE_FAT_ZERO;
} ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"??
this has the same problem as the first solution suggested...people with 5 downloads are now shown as having 50. People without any downloads correctly have 0. ----- Original Message ----- From: sivaji j.g To: support@drupal.org Sent: Friday, April 09, 2010 2:06 PM Subject: Re: [support] Printing "0" when a db query returns nothing
On Fri, Apr 9, 2010 at 10:19 PM, Neil Coghlan neil@esl-lounge.com wrote:
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount;
print (is_int($output->DownloadCount) && (int) $output->DownloadCount > 0) ? $output->DownloadCount : NICE_FAT_ZERO;
} ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"??
-- Thanks Sivaji
------------------------------------------------------------------------------
-- [ Drupal support list | http://lists.drupal.org/ ]
Cast it as an integer:
print (int) $output->DownloadCount;
Jamie Holly http://www.intoxination.net http://www.hollyit.net
On 4/9/2010 12:49 PM, Neil Coghlan wrote:
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing. here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount; } ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"?? regards
Neil
this seems to work!!
thanks Jamie and to everyone else for lending a hand.
Neil ----- Original Message ----- From: Jamie Holly To: support@drupal.org Sent: Friday, April 09, 2010 2:18 PM Subject: Re: [support] Printing "0" when a db query returns nothing
Cast it as an integer:
print (int) $output->DownloadCount;
Jamie Holly http://www.intoxination.net http://www.hollyit.net On 4/9/2010 12:49 PM, Neil Coghlan wrote: I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
here's what I have so far (with download count module installed which creates file_downloads table)
<?php $query= "SELECT SUM(count) AS DownloadCount FROM upload JOIN files ON upload.fid = files.fid JOIN file_downloads ON files.filepath = CONCAT( 'sites/default/files/', file_downloads.filename ) WHERE (files.uid = $account->uid)"; $results = db_query($query); while($output = db_fetch_object($results)){ print $output->DownloadCount; } ?>
works perfectly for those whose content have created downloads. For those who haven't, the result is a blank space. How can I print a nice fat "0"??
regards
Neil
------------------------------------------------------------------------------
-- [ Drupal support list | http://lists.drupal.org/ ]
On Fri, 9 Apr 2010 13:49:43 -0300 "Neil Coghlan" neil@esl-lounge.com wrote:
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
I'd check why a sum() is returning null before casting in php.
sum() is going to return a 0 if NO record was returned. That means there is at least one count that is null (and some may not be), a null in a sum is enough to nullify the sum. 10 + null = null.
select sum(coalesce(count, 0))... You'd be better to set default 0 on files_download.count and update the null record to 0.
the file_downloads table only has records of files that have been downloaded so User A may have created 3 content pages with an attachment on each, but if they haven't been downloaded, he will have no record in the file_downloads table.
that's why it was returning no results...I don't know enough about mysql to say whether it was actually returning NULL or no result, but the solution offered by Jamie (print (int) $output->DownloadCount;) seems to be working now, with a "0" printed if a user has no records in the file_downloads table.
Neil
----- Original Message ----- From: "Ivan Sergio Borgonovo" mail@webthatworks.it To: support@drupal.org Sent: Friday, April 09, 2010 2:58 PM Subject: Re: [support] Printing "0" when a db query returns nothing
On Fri, 9 Apr 2010 13:49:43 -0300 "Neil Coghlan" neil@esl-lounge.com wrote:
I am trying to print the number of downloads a user's attachments have produced and am 95% there. I just don't know how to print zero if the SUM returns nothing.
I'd check why a sum() is returning null before casting in php.
sum() is going to return a 0 if NO record was returned. That means there is at least one count that is null (and some may not be), a null in a sum is enough to nullify the sum. 10 + null = null.
select sum(coalesce(count, 0))... You'd be better to set default 0 on files_download.count and update the null record to 0.
-- Ivan Sergio Borgonovo http://www.webthatworks.it
-- [ Drupal support list | http://lists.drupal.org/ ]
On Fri, 9 Apr 2010 15:12:39 -0300 "Neil Coghlan" neil@esl-lounge.com wrote:
the file_downloads table only has records of files that have been
OK that exclude the problem of a null + not null. Still if there isn't one set a not null default 0 on the file_downloads.count column.
sum() is going to return a 0 if NO record was returned.
BTW this is wrong. sum() != count() My fault.