Jump to content

changing format of data


joebudden

Recommended Posts

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.2mb

Would this be simple to achieve or too complicated ?

Any help appreciated

cheers guys
Link to comment
https://forums.phpfreaks.com/topic/33173-changing-format-of-data/
Share on other sites

no its easy..... i think

all 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 needed


good luck
Cheers PC Nerd GOT IT WORKING ;D

heres 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 MegaBytes
if($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 KilaBytes
else
{
echo '<p>You have used <b>';
$total = $a['total']/ 1024;
echo round($total);
echo ' KB</b> of your total file size allowance</p>';
}
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.