Page 1 of 1

HELP REQUIRED! converting repetitive calculation code into a

Posted: Wed Oct 23, 2002 5:18 am
by martincrumlish
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:

Code: Select all

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

Posted: Wed Oct 23, 2002 5:53 am
by smesquita
See if is that what you need :wink: :

function doublecalc ($odds1, $odds1a, $odds2, $odds2a, $rf1, $rf2, $deadheat1, $deadheat2, $eachway1, $eachway2, $status1, $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;
}

Posted: Wed Oct 23, 2002 6:22 am
by twigletmac
Are those variable names - $odds1, $odds2 etc - always going to be the same and will they already exist when you call the function? If so you needn't pass everything as parameters you can global them instead - obviously this is down to the way you intend to use the function. You can also rearrange the if statements slightly because you know that if $status1 or $status2 are equal to lose then $total will be zero no matter what so you don't need to do the other calculations.

Code: Select all

function doublecalc()
{ 
	global $odds1, $odds1a, $odds2, $odds2a;
	global $rf1, $rf2;
	global $deadheat1, $deadheat2;
	global $eachway1, $eachway2;
	global $status1, $status2;

	if ($status1 == 'lose' || $status2 == 'lose') {
		$total = 0;
	} else {
		if (!empty($rf1)) {
			$odds1 = $odds1 * (1 - $rf1);
		}
		if (!empty($rf2)) {
			$odds2 = $odds2 * (1 - $rf2);
		} 
		$total1 = $odds1/$odds1a * $stake + $stake; 
		if (!empty($eachway1)) {
			$total1 = ($odds1 / ($odds1a * $eachway1) * $stake) + $stake;
		}
		if (!empty($deadheat1)) {
			$total1 = $total1 / $deadheat1;
		}
		$total = $total1 * $odds2/$odds2a + $total1; 
		if(!empty($eachway2)) {
			$total = ($odds2 / ($odds2a * $eachway2) * $total1) + $total1;
		}
		if (!empty($deadheat2)) {
			$total = $total / $deadheat2;
		}
	}
	return $total;
}
You would then just be able to do:

Code: Select all

$total = doublecalc();
Mac