Categories > TinyButStrong general >

Functions & TBS

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Trent
Date: 2007-01-13
Time: 22:30

Functions & TBS

Okay, I have a function that formats Networth (cash value) of a player into the following format:  10,000 = 10 K, 1,000,000 = 1 M, etc.

Here is the function:
function fmtnum ($data) {

   $format = @func_get_arg(2);

   $decimal = @func_get_arg(1);

   if (!isset($decimal)) { $decimal = 2; }
   switch ($format) {
      case "FMT_QUINTILLION":
         $data  = sprintf("%0.".$decimal."f QUINT", $data / 1000000000000000000);
         break;
      case "FMT_QUADRILLION":
         $data  = sprintf("%0.".$decimal."f QUAD", $data / 1000000000000000);
         break;
      case "FMT_TRILLION":
         $data  = sprintf("%0.".$decimal."f TRIL", $data / 1000000000000);
         break;
      case "FMT_BILLION":
         $data  = sprintf("%0.".$decimal."f BIL", $data / 1000000000);
         break;
      case "FMT_MILLION":
         $data  = sprintf("%0.".$decimal."f MIL", $data / 1000000);
         break;
      case "FMT_THOUSAND":
         $data  = sprintf("%0.".$decimal."f K", $data / 1000); 
         break;
      default:  
         if       ($data > 1000000000000000000 ) {
            $data  = sprintf("%0.".$decimal."f QUINT", $data / 1000000000000000000);
         } elseif ($data > 1000000000000000 ) {
            $data  = sprintf("%0.".$decimal."f QUAD", $data / 1000000000000000);
         } elseif ($data > 1000000000000 ) {
            $data  = sprintf("%0.".$decimal."f TRIL", $data / 1000000000000);
         } elseif ($data > 1000000000 ) {
            $data  = sprintf("%0.".$decimal."f BIL", $data / 1000000000);
         } elseif ($data > 1000000 ) {
            $data  = sprintf("%0.".$decimal."f MIL", $data / 1000000);
         } elseif ( $data > 1000 ) {
            $data  = sprintf("%0.".$decimal."f K", $data / 1000);
         }
   }

   return $data;

}

Now I'm trying to call this via TBS within my template.

$TBS->MergeBlock('user','tbssql','SELECT * FROM users where uid = ' . $id . '');

And here is my HTML for displaying the information:

[user.networth;onformat='fmtnum']

Networth is a field in my db that contains the value that I want to format.  For example, let's say that the networth is 1,250,000, then what should be displayed when the template loads is 1.25 M, but it's displaying as 1250000.  Should I just do a separate call to my db outside of TBS and format the information the way I want it and place it into an array and then use TBS to process the array data?  Or is there a way to make the onformat function work the way I'm wanting?  Thanks in advance.
By: Skrol29
Date: 2007-01-15
Time: 19:00

Re: Functions & TBS

Hi,

The fmtnum() function must have a convenient declaration to works with parameter ondata. The value must not be returned but stored into an argument.

The expected syntax is here:
  http://www.tinybutstrong.com/manual.php#html_field_prm_onformat