Jump to content

[SOLVED] PHP Array


TOMMURRAY

Recommended Posts

Hi all, I have a script (below) that is used to calculate the points on a golf scoring system called stableford. I would like to amend the code so that it runs through the results of a form submitted for all 18 holes. The script below only calculates for one hole, how could i go about doing this?

 

For each hole the following will have different unique values:

 

$score

$si

$par

 

What would be the best way of doing this and is there any good resources that you could point me in the direction of?

 

Thank you in advance

 

Tom

 

 

-----------------------------------------------

 

<?php

$count = 0;

$score = 5;

$si = 1;

$handicap = 28;

$par = 4;

$stablefordpoints =0;

 

if ($handicap >= 0) {

if ($score > 0) {

$sp = 0;

if ($handicap == 0) {

$sp = $score;

} else if ($handicap <= 18){

For ($count = 1; $count <= $handicap; $count++){

if ($si == $count) {

$sp = $score - 1;

} else {

$sp = $score;

}

}

} else if ($handicap >= 19 && $handicap <= 36){

$handicap = $handicap - 18;

For ($count = 1; $count <= $handicap; $count++){

if ($si <= $count) {

$sp = $score - 2;

} else {

$sp = $score -1;

}

}

}

 

if ($sp - 1 == $par){

  $stablefordpoints = 1;

} elseif ($sp == $par){

  $stablefordpoints = 2;

} elseif ($sp + 1 == $par){

$stablefordpoints = 3;

} elseif ($sp + 2 == $par){

  $stablefordpoints = 4;

} elseif ($sp + 3 == $par){

  $stablefordpoints = 5;

} elseif ($sp + 4 == $par){

  $stablefordpoints = 6;

}                               

if (!empty($stablefordpoints)) echo $stablefordpoints; 

 

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/48607-solved-php-array/
Share on other sites

Try something like this. Keep in mind that I don't fully understand what your loops are doing, so I'm simply reworking your script into pieces:

<?php
function calcHole($hc, $score, $si, $par)
{
if ($score >0)
{
	$sp = 0;
	if ($hc == 0)
	{
		$sp = $score;
	} elseif ($hc <= 18) {
		for ($count = 1; $count <= $hc; $count++)
		{
			if ($si == $count) $sp = $score - 1;
			else $sp = $score;
		}
	}
} elseif ($hc >= 19 && $hc <= 36) {
	$hc -= 18;
	for ($count = 1; $count <= $hc; $count++)
	{
		if ($si <= $count) $sp = $score - 2;
		else $sp = $score - 1;
	}
}

for ($i = -1; $i < 5; $i++)
{
	if ($sp + $i == $par) $points = $i + 2;
}

return $points;
}

function calcAllSubmitted($arr, $handicap)
{
$total = 0;
foreach ($arr['score'] as $k => $v)
{
	if (!empty($v) && !empty($arr['si'][$k]) && !empty( $arr['par'][$k]))
	{
		$total += calcHole($handicap, $v, $arr['si'][$k], $arr['par'][$k]);
	}
}
return $total;
}

if (isset($_POST['submit']))
{
echo "Form submitted.<br />Total score: " . calcAllSubmitted($_POST, 10) . "<br />\n";
}
?>

<form name="calc" action="" method="post">
<table>
<tr>
<th>Hole</th><th>Score</th><th>SI</th><th>Par</th>
</tr>
<?php
for ($i = 1; $i < 19; $i++)
{
echo "<tr>\n";
echo "<td>Hole #{$i}</td>\n";
echo "<td><input type=\"text\" size=\"4\" name=\"score[]\" /></td>";
echo "<td><input type=\"text\" size=\"4\" name=\"si[]\" /></td>";
echo "<td><input type=\"text\" size=\"4\" name=\"par[]\" /></td>";
echo "</tr>\n";
}
?>
<tr>
<td colspan="4"><input type="submit" name="submit" value="Calculate" /></td>
</tr>
</table>
</form>

 

This does two things:

 

First, it auto generates an 18 hole form for you.

Second, it calculates your score based on what has been submitted only. If you leave some holes out, it calculates only those fields that have been filled.

Link to comment
https://forums.phpfreaks.com/topic/48607-solved-php-array/#findComment-238064
Share on other sites

I have just noticed a discrepancy in the calculations, the calculations are fine if the person has "0" handicap. But if the person has a handicap of say "1" then the whole that has a stroke index (si) of "1" the person gets a stroke deducted.

 

E.G. if the par for the hole is 4 and the person takes 4 shots, they get a stroke deducted so the total shots for that hole would be 3. Which would then get a total of 3 points. But in the reply above the total points would be 2.

 

Does anyone have any ideas were the problem lies?

 

Thank You

 

obsidian  I can't thank you enough for the contribution you have mad.

Link to comment
https://forums.phpfreaks.com/topic/48607-solved-php-array/#findComment-238601
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.