PDA

View Full Version : HELP REQUIRED! converting repetitive code into a function


martincrumlish
10-23-2002, 11:20 AM
Hi,

I am writing a betting calculation script. This will involve multiple combinations of odds and stakes etc which are entered via a form.

A user selects which type of bet they want and when they clickm submit the calculation for this script is run. i.e: a single bet is: odds * stake + stake = return1

a double bet is:
odds * stake + stake = return1
odds1 * return1 + return1 = return

There are some advanced betting selections that have 8 selections and involve a number of combinations of the different selections. What I need is a function which i can pass these values through to get a result.

Here is an example of the calculation I have for a double bet:

if($calc == "Double")
{
if($rf1){$odds1 = $odds1 * (1 - $rf1);}
if($rf2){$odds2 = $odds2 * (1 - $rf2);}
$total1 = $odds1/$odds1a * $stake + $stake;
if($eachway1){$total1 = ($odds1 / ($odds1a * $eachway1) * $stake) + $stake;}
if($deadheat1){$total1 = $total1 / $deadheat1;}
$total = $total1 * $odds2/$odds2a + $total1;
if($eachway2){$total = ($odds2 / ($odds2a * $eachway2) * $total1) + $total1;}
if($deadheat2){$total = $total / $deadheat2;}
if(($status1 == "lose") || ($status2 == "lose")){$total = 0;}
}

The variables odds1, odds1a etc are the odds entered in a web form
the variable stake is the entered stake from the form
rf1, status1, deadheat and eachway are all passed from the web form as well and are betting terminologies that i dont need to go into.

What i need is a function which i can call like this:

say we have a form with 2 bets:
If the odds for a bet are 2/1 then $odds1 = 2 and $odds1a = 1

function doublecalc ($odds1,$odds1a,$odds2,$odds2a,rf1,rf2,deadheat1,deadheat2,eachway1,eachway2,staus1,status2)

This function should return the value $total

Can someone help me with this please? if I can get 1 function working i can work out all the other bets types from there.

Thanks

firepages
10-23-2002, 01:56 PM
well since you have already done all the work!

turning an existing routine into a function is as simple as

i.e.

$res=$x+$y;
.......could be

function add($x,$y){/*the variables we are passing to the function*/
$total=$x+$y;
return $total;
}

echo add(5,9);

and yours whilst longer (and should probably be split up into smaller functions? (hard to say without seeing the rest of your requirements)
follows exactly the same logic...


<?
function doublecalc($odds1,$odds1a,$odds2,$odds2a,$rf1,$rf2,$deadheat1,$deadheat2,$eachway1,$eachway2,$staus1 ,$status2){
if($rf1){$odds1 = $odds1 * (1 - $rf1);}
if($rf2){$odds2 = $odds2 * (1 - $rf2);}
$total1 = $odds1/$odds1a * $stake + $stake;
if($eachway1){$total1 = ($odds1 / ($odds1a * $eachway1) * $stake) + $stake;}
if($deadheat1){$total1 = $total1 / $deadheat1;}
$total = $total1 * $odds2/$odds2a + $total1;
if($eachway2){$total = ($odds2 / ($odds2a * $eachway2) * $total1) + $total1;}
if($deadheat2){$total = $total / $deadheat2;}
if(($status1 == "lose") || ($status2 == "lose")){$total = 0;}
return $total;
}

if($calc == "Double")
{
$result=doublecalc ($odds1,$odds1a,$odds2,$odds2a,$rf1,$rf2,$deadheat1,$deadheat2,$eachway1,$eachway2,$staus1,$status2) ;
}
?>

martincrumlish
10-23-2002, 02:04 PM
Thanks for your help.

I have got this working now :)