View Full Version : Attachment Count in MB
Boofo
05-26-2003, 07:56 PM
Can anyone please tell me how to pull the MB out of an attachment query? Here is the code for pulling out the KB but I can't seem to find the right code for the MB. I know it is simple, but I must not be very good in the math department.
SUM(LENGTH(attachment.filedata))/1024 AS KBbytes
assassingod
05-26-2003, 08:06 PM
SUM(LENGTH(attachment.filedata))/1048576 AS KBbytes
?:confused:
Boofo
05-26-2003, 08:19 PM
I had that but I want to get it like 2.12 MB. instead of just 2 MB. How do I get it to go the 2 decimal places?
filburt1
05-26-2003, 08:39 PM
<a href="http://www.php.net/round" target="_blank">http://www.php.net/round</a>
Boofo
05-26-2003, 08:45 PM
I tried that, too. When I round 2,174,891, I get 2 MB. This is how I used round:
$attachs['MBbytes'] = round($attachs['MBbytes']);
filburt1
05-26-2003, 09:00 PM
Then:
...SUM(LENGTH(attachment.filedata)) / 1024 * 1024 * 1024 AS mbytesum...
...then round it. :)
Boofo
05-26-2003, 09:11 PM
That threw it way off. ;) Here's the code I am working with if that will help.
/** Count of Attachments Per Forum and Bytes **/
$attachs=$DB_site->query_first("SELECT forum.title AS forum, COUNT(attachment.attachmentid) AS count, SUM(LENGTH(attachment.filedata)) AS bytes, SUM(LENGTH(attachment.filedata))/1024 AS KBbytes, SUM(LENGTH(attachment.filedata))/1048576 AS MBbytes
FROM attachment,post,thread,forum
WHERE attachment.attachmentid = post.attachmentid
AND post.threadid=thread.threadid
AND forum.forumid=thread.forumid
AND forum.forumid=$forumid
GROUP BY thread.forumid
ORDER BY count DESC");
$attachs['count'] = number_format($attachs['count']);
$attachs['bytes'] = number_format($attachs['bytes']);
$attachs['KBbytes'] = number_format($attachs['KBbytes']);
$attachs['MBbytes'] = number_format($attachs['MBbytes']);
$attachs['MBbytes'] = round($attachs['MBbytes']);
If ($attachs['MBbytes']<1) {
$attachs['MBbytes']='';
} else {
$attachs['MBbytes']="=" .$attachs['MBbytes']. "MB";
}
If ($attachs['count'] !="0") {
$attachinfo="<tr>
<td nowrap><smallfont><b>Total Attachments:</b> $attachs[count]</smallfont></td>
<td nowrap><smallfont><b>Total Attachments Bytes:</b> $attachs[bytes] bytes ($attachs[KBbytes] KB$attachs[MBbytes])</smallfont></td>
</tr>";
} else {
$attachinfo="";
}
/** Count of Attachments Per Forum and Bytes **/
noppid
05-26-2003, 11:00 PM
Use the php modulus operator to work with the remainder.
http://us3.php.net/manual/en/language.operators.arithmetic.php
Boofo
05-26-2003, 11:17 PM
LOL I just tried that and I was way off. All I am trying to do is go 2 decimal places with the MB and have it rouind up if it needs to.
filburt1
05-26-2003, 11:23 PM
Okay, get the sum in bytes, and assume it's in the $bytes variable.
$megabytes = $bytes / (1024 * 1024);
$megabytes = number_format($megabytes, 2); // add commas and only 2 dec places
Boofo
05-26-2003, 11:27 PM
I have something like that here:
$attachs['MBbytes'] = number_format($attachs['MBbytes'], 2);
But it won't round the last decimal up if the one before it is 5 or more, will it? It just chops it off there, right?
filburt1
05-26-2003, 11:37 PM
It won't round at all, probably.
Boofo
05-27-2003, 07:50 AM
Is there any way to make it round up or is what I got the best I can get?
Gary King
05-28-2003, 10:01 PM
<a href="http://www.php.net/round" target="_blank">http://www.php.net/round</a>
Boofo
05-28-2003, 10:07 PM
Today at 06:01 PM Gary W said this in Post #14 (https://vborg.vbsupport.ru/showthread.php?postid=401324#post401324)
http://www.php.net/round
I tried that already. Thanks, anyway.
Gary King
05-28-2003, 11:52 PM
And it doesn't work?
Boofo
05-28-2003, 11:58 PM
No, it rounds 2.12 MB to 2 MB and if I use number_format(attachsmb), 2), then it rounds it to 2.00. It must be the way I am trying to cache it.
filburt1
05-29-2003, 12:07 AM
round will still work. Look at the second argument.
Boofo
06-04-2003, 04:53 AM
05-28-03 at 08:07 PM filburt1 said this in Post #18 (https://vborg.vbsupport.ru/showthread.php?postid=401380#post401380)
round will still work. Look at the second argument.
You totally lost me now. What 2nd argument?
gmarik
06-11-2003, 10:06 AM
$megabytes = $bytes / (1024 * 1024);
$megabytes = number_format($megabytes, 2); // add commas and only 2 dec places
Boofo
06-11-2003, 10:36 AM
Already tried that and it doesn't work either.
gmarik
06-11-2003, 02:52 PM
There was a fine solution in HiveMail,
Chen is a really perfect php coder - clean and nice.
filburt1
06-11-2003, 04:52 PM
Nobody's a perfect coder...
gmarik
06-12-2003, 06:15 AM
Nope, I doen't believe you, Fil.
Chen has coded this and that and it's all so smooth, the functions and, oh, I just love it.
Gary King
06-12-2003, 07:23 PM
Yesterday at 01:52 PM filburt1 said this in Post #23 (https://vborg.vbsupport.ru/showthread.php?postid=407604#post407604)
Nobody's a perfect coder...
Agreed.
You see, gmarik, nobody is perfect.
Boofo
06-12-2003, 11:40 PM
That all depends on your idea of perfection. Maybe perfection for him IS Chen's coding ability. Nothing wrong with that.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.