473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Turning this into a function (Beginner Level)

I need some help writing the following lines of code into a simple
function. I'd like to be able to create a function to do the
calculation, then feed the $a, $b, or $c variable into it.

Thanks,
J

<?php $a = $kerryvotes/$votes;
$kpercentage = $a * 100;
$kpercent = round($kpercentage, 0);
?>

<?php $b = $bushvotes/$votes;
$bpercentage = $b * 100;
$bpercent = round($bpercentage, 0);
?>
<?php $c = $nadervotes/$votes;
$npercentage = $c * 100;
$npercent = round($npercentage, 0);
?>
Jul 17 '05 #1
3 1456
Jason wrote:
[snip]

function votepercent($votes, $totalvotes) {
$total = round(($votes/$totalvotes)*100);
return $total;
}

Use example:
echo 'Kerry has yer '.votepercent($quantityforkerry, $totalofvotes).' %
of the votes';

Aphrael...
--
"La demande mondiale d’ordinateurs n’excédera pas cinq machines."
(Thomas Watson, Fondateur d'IBM, 1945)
Jul 17 '05 #2
Jason wrote:
I need some help writing the following lines of code into a simple
function. I'd like to be able to create a function to do the
calculation, then feed the $a, $b, or $c variable into it.

[snip]

You needn't do it one by one ...
Here's a function to do it all in one swoop :-)
<?php
function calc_percentages($individual_votes) {
$sum = array_sum($individual_votes);
foreach ($individual_votes as $k=>$v) {
$ret[$k] = round($v/$sum*100);
}
return $ret;
}

/* example usage */

/* disclaimer: the numbers are random */
$votes = array('bush'=>555, 'kerry'=>555, 'nader'=>555);

$percs = calc_percentages($votes);
print_r($percs);
?>

--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #3
thanks, this looks good.. i'll try both methods..
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Freddy | last post by:
How do you turn a string into a callable function? I'm writing a program which takes user input and then looks up the associated function to run from a shelved dictionary. So, I have a dictionary...
6
by: Peter Otten | last post by:
It's easy to write a function that wraps a generator and provides a callback. E. g.: import os, sys def walk(path, visit): """ Emulate os.path.walk() (simplified) using os.walk()""" for dir,...
4
by: mercuryprey | last post by:
Hi, I'm pretty new to Python, to programming overall...so how would I make something where the user inputs multiple words in a string - like "connect 123.123.123.123 21 user password" or similar,...
5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
4
by: Rodusa | last post by:
I am having problem to apply updates into this function below. I tried using cursor for updates, etc. but no success. Sql server keeps telling me that I cannot execute insert or update from inside...
5
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume...
7
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
2
tpgames
by: tpgames | last post by:
Me memory game freezes after I click some cards. I seems to be random as to the number of clicks needed before it freezes. I posted the entire code, as I am not sure what is needed for help. I am...
6
by: Rain | last post by:
Hi, I noticed the other day when I was trying to optimise the traffic on a particular web page that it had significant viewstate even though I had turned it off for most of the controls. So in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.