Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Hi All,
I am using this Php Code in Couch "Amount to word Convert "

So I have to set variable

Code: Select all
echo convert_number_to_words(<cms:show amount />); 


But I Know that I have to declare Master Page also.. so I configured it like below

Code: Select all
<cms:pages masterpage='amount.php'>
echo convert_number_to_words(<cms:show amount />);
</cms:pages>


but its not working.... kindly help me..

Below I add full php code in running condition without variable. kindly take a look.

Code: Select all
<cms:php> 
function convert_number_to_words($number)

    $hyphen      = '-';
    $conjunction = ' and ';
    $separator   = ', ';
    $negative    = 'negative ';
    $decimal     = ' point ';
    $dictionary  = array(
        0                   => 'zero',
        1                   => 'one',
        2                   => 'two',
        3                   => 'three',
        4                   => 'four',
        5                   => 'five',
        6                   => 'six',
        7                   => 'seven',
        8                   => 'eight',
        9                   => 'nine',
        10                  => 'ten',
        11                  => 'eleven',
        12                  => 'twelve',
        13                  => 'thirteen',
        14                  => 'fourteen',
        15                  => 'fifteen',
        16                  => 'sixteen',
        17                  => 'seventeen',
        18                  => 'eighteen',
        19                  => 'nineteen',
        20                  => 'twenty',
        30                  => 'thirty',
        40                  => 'fourty',
        50                  => 'fifty',
        60                  => 'sixty',
        70                  => 'seventy',
        80                  => 'eighty',
        90                  => 'ninety',
        100                 => 'hundred',
        1000                => 'thousand',
        1000000             => 'million',
        1000000000          => 'billion',
        1000000000000       => 'trillion',
        1000000000000000    => 'quadrillion',
        1000000000000000000 => 'quintillion'
    );
   
    if (!is_numeric($number)) {
        return false;
    }
   
    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }
   
    $string = $fraction = null;
   
    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }
   
    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }
   
    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }
   
    return $string;
}

echo convert_number_to_words(1560); </cms:php>


Thanks
Subhamoy
Hi,

You are invoking a PHP function, so you'll need to wrap the code in cms:php block - for example as follows -
Code: Select all
<cms:pages masterpage='amount.php'>
    <cms:php> echo convert_number_to_words(<cms:show amount />); </cms:php>
</cms:pages>

As an aside - since the convert_number_to_words() function, per se, does not contain any Couch tags, you might want to move it (excluding the <cms:php></cms:php> tags) to couch/addons/kfunctions.php file. That way the function would be available in all templates.

Does this help?
Thanks KK for quick response.

I think its so difficult to add function on kfunctions.php by myself. because I never add any php file as addons before.

Ok I will try.

After adding function on kfunctions.php
and
Code: Select all
<cms:php> 
echo convert_number_to_words(<cms:show amount />); </cms:php>


It displaying this warning...

Warning: Missing argument 1 for convert_number_to_words(), called in C:\xampp\htdocs\note\couch\tags.php(2864) : eval()'d code on line 2 and defined in C:\xampp\htdocs\note\couch\addons\ntw\ntw.php on line 4
Yes! its working.....

Thanks KK
I'm glad :) Thanks for letting us know, Subhamoy.
5 posts Page 1 of 1