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
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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[code=php:0]
<?php

if(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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.