Page 1 of 1

Help Needed With a Script for Calculations [SOLVED]

Posted: Tue Apr 24, 2007 2:25 am
by tommurray
I am attempting to create a php script for a golf website, the problem is that I am new to PHP and I have become stuck as I can't see were the error lies in the below code. The script calculates based on the variables but I thought I would have a number returned at the end of the script but nothing is displayed can anyone see any errors?

I have automatically set the variables to 4 so I can see if the script is calucalting correctly, once the script is working the values will be determined from an entry in a form.

if you want to know what the script is supposed to add up like let me know an I will do my best to explain or look up stableford points in golf.

Thank you in advance.

Tom

*** PLEASE USE THE PHP TAG WHEN POSTING CODE ***

Code: Select all

<?php

$count = 0;
$score = 4;
$si = 4;
$handicap = 4;
$par = 4;

   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;
            echo "$stablefordpoints";

         } else if ($sp - 2 = $par){
            $stablefordpoints = 2;
            echo "$stablefordpoints";

         } else if ($sp - 3 = $par){
            $stablefordpoints = 3;
            echo "$stablefordpoints";

         } else if ($sp - 4 = $par){
            $stablefordpoints = 4;
            echo "$stablefordpoints";

         } else if ($sp - 5 = $par){
            $stablefordpoints = 5;
            echo "$stablefordpoints";
         }   
      }
   }

?>

Posted: Tue Apr 24, 2007 3:18 am
by tommurray
I have amended the code to the below adding in "==" to the if statements at the end to set as equals but nothing is returned no errors, no responses. Please help.

Thank You

*** PLEASE USE THE PHP TAG WHEN POSTING CODE ***

*** ALSO, YOU CAN EDIT YOUR PREVIOUS POST RATHER THAN ADDING POSTS ***

Code: Select all

<?php
	$count = 0; 
	$score = 4; 
	$si = 4; 
	$handicap = 4; 
	$par = 4;
	$stablefordpoints; 

		if ($handicap >= 0) { 
			if ($score > 0) { 
				$sp = 0; 
				if ($handicap == 0) { 
					$sp = $score; 
				} else if ($handicap <= 1){ 
					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; 
					echo "$stablefordpoints"; 
				} else if ($sp - 2 == $par){ 
					$stablefordpoints = 2; 
					echo "$stablefordpoints"; 
				} else if ($sp - 3 == $par){ 
					$stablefordpoints = 3; 
					echo "$stablefordpoints"; 
				} else if ($sp - 4 == $par){ 
					$stablefordpoints = 4; 
					echo "$stablefordpoints"; 
				} else if ($sp - 5 == $par){ 
					$stablefordpoints = 5; 
					echo "$stablefordpoints"; 
				} 
			} 
		} 
		
		echo "$stablefordpoints"; 

?>

Posted: Tue Apr 24, 2007 4:17 am
by CoderGoblin
OK... No wonder I never like Golf.. :D

I would use...

Code: Select all

if ($sp - 1 == $par){
  $stablefordpoints = 1;
} elseif ($sp - 2 == $par){
  $stablefordpoints = 2;
} elseif ($sp - 3 == $par){
  $stablefordpoints = 3;
} elseif ($sp - 4 == $par){
  $stablefordpoints = 4;
} elseif ($sp - 5 == $par){
  $stablefordpoints = 5;
}                                }  
if (!empty($stablefordpoints)) echo $stablefordpoints;
as your last block. I cannot see an error but you may want to add "debug" checks in your code. These can be simply an echo...

Code: Select all

echo "DEBUG: In block handicap 2-18<br />";
would do the job. When inserting this you should start to realise that you don't have an elseif for when handicap is 4.

Code: Select all

if ($handicap == 0) { 
} elseif ($handicap <= 1) {
} elseif ($handicap >= 19 && $handicap <= 36){

Posted: Tue Apr 24, 2007 5:42 am
by tommurray
Thank you for your assistance I have made the changes to the IF statements at the bottom of the script as you suggested, the else if statement in the script should have been a range of "18" as opossed to the "1" my mistake.

After changing the script to include 18 in the For statement I now get the following message any suggestions?


Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\stableford-point-script.php on line 15

Posted: Tue Apr 24, 2007 6:19 am
by CoderGoblin
All conditionals need to be == not = (for loops <=)

Code: Select all

for ($count = 1; $count <= $handicap; $count++) {
  if ($si == $count) {
You are never breaking out of the loop.

SOLVED

Posted: Tue Apr 24, 2007 7:53 am
by tommurray
Thank You so much for all your help I do have issues out standing but they are down to my false calculations at the bottom of the script. Once again thank you so much.

Cheers

Tom