joebudden Posted January 7, 2007 Share Posted January 7, 2007 Hi guys, I've written code to display the total size of files in a database...[code]$query2 = "select sum(size) as total from artefact where dbEmployeeId='".$_SESSION['sessEmployee']['id']."'"; $allowance = mysql_query($query2);$a = mysql_fetch_array($allowance, MYSQL_ASSOC);echo '<p>You have used <b>';$total = $a['total']/ 1024;echo round($total); echo ' KB</b> of your total file size allowance</p>';[/code]This works fine... However I was wondering if there was any way of displaying the size as MEGA BYTES once the total reaches a MEGA BYTE???I.E IF the total is 200kb it displays 200kb.... ...but IF the total is 1200kb it displays 1.2mbWould this be simple to achieve or too complicated ?Any help appreciated cheers guys Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/ Share on other sites More sharing options...
PC Nerd Posted January 7, 2007 Share Posted January 7, 2007 no its easy..... i thinkall you would bave to do, is once you have the kb count of the file length, you do something like this[code]$total2 = total / 1024;if($total2 > 1) {echo $total2;echo "KB</b> of your total file size allowance</p>";}[/code]you could use another else if () { } for GB if you really neededgood luck Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154734 Share on other sites More sharing options...
joebudden Posted January 7, 2007 Author Share Posted January 7, 2007 No that didnt work anyone else ??? Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154735 Share on other sites More sharing options...
PC Nerd Posted January 7, 2007 Share Posted January 7, 2007 is the $total variable text or an integar. if its text, make it an integar and that will allow you to do math orrerations on it, if its text, thats why it didnt work, becauase your deviding text by a number which obviously wont work Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154742 Share on other sites More sharing options...
joebudden Posted January 7, 2007 Author Share Posted January 7, 2007 Cheers PC Nerd GOT IT WORKING ;Dheres the code I used[code]<?php//query to select total size of files in database $query2 = "select sum(size) as total from artefact where dbEmployeeId='".$_SESSION['sessEmployee']['id']."'"; $allowance = mysql_query($query2);$a = mysql_fetch_array($allowance, MYSQL_ASSOC);$total = $a['total']/ 1024;$totalMB = $total / 1024;//display total size in MegaBytesif($totalMB > 1) {echo '<p>You have used <b>';echo round($totalMB,2);echo "MB</b> of your total file size allowance</p>";}//display total size in KilaByteselse{echo '<p>You have used <b>';$total = $a['total']/ 1024;echo round($total); echo ' KB</b> of your total file size allowance</p>';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154745 Share on other sites More sharing options...
marcus Posted January 7, 2007 Share Posted January 7, 2007 [code=php:0]<?phpif(is_numeric($total) && str_len($total) == 4){$nt = round($total);$dv = $nt/1024;echo "You have used $dv MB";}else if(is_numeric($total) && str_len($total) < 3){$nt = round($total);echo "You have used $nt KB";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154747 Share on other sites More sharing options...
marcus Posted January 7, 2007 Share Posted January 7, 2007 [code=php:0]$a = mysql_fetch_array($allowance, MYSQL_ASSOC);$total = $a['total']/ 1024;$totalMB = $total / 1024;[/code]If $a['total'] equaled 1024 then $total would equal 1 and then $totalMB would equal 1/1024 which is 0.0009765625. Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154748 Share on other sites More sharing options...
PC Nerd Posted January 7, 2007 Share Posted January 7, 2007 glad i could help Quote Link to comment https://forums.phpfreaks.com/topic/33173-changing-format-of-data/#findComment-154750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.