WebsiteBaker Logo
  • *
  • Templates
  • Help
  • Add-ons
  • Download
  • Home
*
Welcome, Guest. Please login or register.

Login with username, password and session length
 

News


WebsiteBaker 2.13.8 is now available!


R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WB
https://forum.websitebaker.org/index.php/topic,32355.0.html


* Support WebsiteBaker

Your donations will help to:

  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!

You can donate by clicking on the button below.


  • Home
  • Help
  • Search
  • Login
  • Register

  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • percentage droplet
  • Print
Pages: [1]   Go Down

Author Topic: percentage droplet  (Read 6950 times)

snark

  • Guest
percentage droplet
« on: June 16, 2010, 11:32:56 AM »
the following script calculates a percentage:

Code: [Select]
<?php
function percent($num_amount, $num_total) {
$count1 = $num_amount / $num_total;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
echo 
$count;
}
?>


which can be called by:

Code: [Select]
<?php percent(144, 12);?>

now I want to use this as a droplet which will app. be something like:
[[percentage?firstnumber=144&secondnumber=12]]

is there anyone who can help me here?




Logged

Offline DarkViper

  • Forum administrator
  • *****
  • Posts: 3087
  • Gender: Female
Re: percentage droplet
« Reply #1 on: June 16, 2010, 01:16:07 PM »
Droplet:  [percentage]

  [[percentage?basevalue=200&percentvalue=20&decimals=0]]

  20€ of  200€ are 10%

  [[percentage?basevalue=55&percentvalue=5&decimals=4]]

  5€ of 55€ are 9.0909%

The Droplet can handle integer and floatingpoint also
The number of decimals can be between 0 and 10

Code: (Droplet percentage) [Select]
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue : 0;
  if( !
$basevalue ) return &#39;0&#39;;  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percentvalue : 0;
  if( !
$percentvalue ) return &#39;0&#39;;
  
$decimals = isset($decimals) ? (intval($decimals) % 11) : 0;

  return  
number_format( (( 100 * $percentvalue ) / $basevalue ), $decimals );

?>

have fun.....
« Last Edit: June 17, 2010, 07:26:24 PM by DarkViper »
Logged
Der blaue Planet - er ist nicht unser Eigentum - wir haben ihn nur von unseren Nachkommen geliehen

"We need education to cope with digitalization - and NOT the digitalization of education.!"

Tägliches Stoßgebet: Oh Herr, wirf Hirn vom Himmel !

snark

  • Guest
Re: percentage droplet
« Reply #2 on: June 16, 2010, 02:11:39 PM »
looks great but it returns a "0" as percentage to everything I fill in ...

Logged

Offline pcwacht

  • Posts: 2923
  • Gender: Male
    • Dutch ICT info
Re: percentage droplet
« Reply #3 on: June 16, 2010, 02:22:58 PM »
Typo?

Code: [Select]
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue : 0;
  if( !
$basevalue ) return &#39;0&#39;;  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percenvalue : 0;
  if( !
$percenvalue ) return &#39;0&#39;;
  
$decimals = isset($decimals) ? (intval($decimals) % 11) : 0;

  return  
number_format( (( 100 * $percentvalue ) / $basevalue ), $decimals );

?>
Code: [Select]
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue : 0;
  if( !
$basevalue ) return &#39;0&#39;;  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percentvalue : 0;
  if( !
$percentvalue ) return &#39;0&#39;;
  
$decimals = isset($decimals) ? (intval($decimals) % 11) : 0;

  return  
number_format( (( 100 * $percentvalue ) / $basevalue ), $decimals );

?>
twice the missing 't' in percentvalue

Didn't test it...
Have fun,
John
Logged
http://www.ictwacht.nl = Dutch ICT info
http://www.pcwacht.nl = My first
both still work in progress, since years.....

snark

  • Guest
Re: percentage droplet
« Reply #4 on: June 16, 2010, 04:16:53 PM »
helemaal te gek!

dankjewel
Logged

Offline DarkViper

  • Forum administrator
  • *****
  • Posts: 3087
  • Gender: Female
Re: percentage droplet
« Reply #5 on: June 16, 2010, 04:58:04 PM »
ok, ok.... for the next time i've cleaned the keyboard of my laptop...   :roll:
Logged
Der blaue Planet - er ist nicht unser Eigentum - wir haben ihn nur von unseren Nachkommen geliehen

"We need education to cope with digitalization - and NOT the digitalization of education.!"

Tägliches Stoßgebet: Oh Herr, wirf Hirn vom Himmel !

Offline pcwacht

  • Posts: 2923
  • Gender: Male
    • Dutch ICT info
Re: percentage droplet
« Reply #6 on: June 16, 2010, 05:01:40 PM »
no problem, my fingers are dyslexic too ;)
Logged
http://www.ictwacht.nl = Dutch ICT info
http://www.pcwacht.nl = My first
both still work in progress, since years.....

  • Print
Pages: [1]   Go Up
  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • percentage droplet
 

  • SMF 2.0.19 | SMF © 2017, Simple Machines
  • XHTML
  • RSS
  • WAP2