Jump to content

variable = variable + number - script not working


AnAmericanGunner

Recommended Posts

I have a script that adds points together based upon the placing. This is the actual script:

 

<? $points = 0; 

if($place === '1st') {$points = $points + 50;}
elseif($place === '2nd') {$points = $points + 45;}
elseif($place === '3rd') {$points = $points + 40;}
elseif($place === '4th') {$points = $points + 35;}
elseif($place === '5th') {$points = $points + 30;}
elseif($place === '6th') {$points = $points + 25;}
elseif($place === '7th') {$points = $points + 20;}
elseif($place === '8th') {$points = $points + 10;}
elseif($place === '9th') {$points = $points + 10;}
elseif($place === '10th') {$points = $points + 10;}
elseif($place === 'CH') {$points = $points + 50;}
elseif($place === 'RCH') {$points = $points + 40;}
elseif($place === 'TT') {$points = $points + 30;}
elseif($place === 'T5') {$points = $points + 30;}
elseif($place === 'Champion') {$points = $points + 50;}
elseif($place === 'Reserve Champion') {$points = $points + 40;} 

echo "Total HF Points: $points";
?>

What it *should* do (my friend's script works the same way and it works) it starts at points = 0, than if there is a first place, it adds 50, and so forth until it reaches the end.

 

It is included into a file, in this area:

 

<div class="tabbertab">
<h2>Records</h2>
<? $query92 = "SELECT * FROM THISTABLE WHERE VARIABLE='$id' OR VARIABLE = '$name' ORDER BY ABS(VARIABLE), VARIABLE";
$result92 = mysql_query($query92) or die (mysql_error());

echo "<table class='record'>
<tr><th>Show</th> <th>Class</th> <th>Place</th></tr>
";

while($row92 = mysql_fetch_array($result92)) {
$class = $row92['class'];
$place = $row92['place'];
$entries = $row92['entries'];
$race = $row92['show'];
$purse = number_format($row92['purse'],2);

echo "<tr><td>$race</td> <td>$class</td> <td>$place</td></tr>";
} 
?>
<tr><td colspan='3'><div align='right'><? include('includes/points.php'); ?></div></td></tr>
</table>
</div>

 

This is the code that is relevant. When ended here, it echoes the last place that appears in the results (such as a 5th place echoing 30 points). When I move it to be included in the while loop, it shows Total Points: 50 Total Points: 25 Total Points: 10 (depending on the results displayed on that page). What am I doing wrong?

really, i would make this into a function and include the file in the head of the page, but just using the code you have now, you will want to do a few things.

 

change your points.php file to something like this,

 

<?php

if($place === '1st') {$points += 50;}
elseif($place === '2nd') {$points += 45;}
elseif($place === '3rd') {$points += 40;}
elseif($place === '4th') {$points +=  35;}
elseif($place === '5th') {$points += 30;}
elseif($place === '6th') {$points += 25;}
elseif($place === '7th') {$points += 20;}
elseif($place === '8th') {$points += 10;}
elseif($place === '9th') {$points += 10;}
elseif($place === '10th') {$points += 10;}
elseif($place === 'CH') {$points += 50;}
elseif($place === 'RCH') {$points += 40;}
elseif($place === 'TT') {$points += 30;}
elseif($place === 'T5') {$points += 30;}
elseif($place === 'Champion') {$points += 50;}
elseif($place === 'Reserve Champion') {$points +=  40;} 

?>

 

and your main page to,

 

<div class="tabbertab">
<h2>Records</h2>
<?php
         $points = 0; //set before loop
         $query92 = "SELECT * FROM THISTABLE WHERE VARIABLE='$id' OR VARIABLE = '$name' ORDER BY ABS(VARIABLE), VARIABLE";
 $result92 = mysql_query($query92) or die (mysql_error());

echo "<table class='record'>
<tr><th>Show</th> <th>Class</th> <th>Place</th></tr>
";

while($row92 = mysql_fetch_array($result92)) {
$class = $row92['class'];
$place = $row92['place'];
        include("include/points.php"); //will add correct $points value every iteration while maintaining previous value
$entries = $row92['entries'];
$race = $row92['show'];
$purse = number_format($row92['purse'],2);

echo "<tr><td>$race</td> <td>$class</td> <td>$place</td></tr>";
} 
?>
<tr><td colspan='3'><div align='right'>Total Points: <?php echo $points; ?></div></td></tr>
</table>
</div>

so in your points.php page I removed where you defined $points = 0; to make it more robust so it would not overwrite the variable every iteration of the while loop, and I cleaned up the syntax a bit, you could use a switch statement here as well, but its really whatever you are more comfortable using.

For the main page, I set $points = 0; towards the top before the loop so it remained a static value if the script were to be run again for another person, I included the points.php page into the while loop so it would add the correct value to the $points variable each iteration, and after the while loop has completed its iterations, you will be left with the total number of points contained in the $points variable, which i output in your table column.

Again, i would set this up in a function rather then the format that you have it in now, but that's just me, also, get into the habit of using full <?php opening tags, as not all servers are configured to accept short tags.

You really want to avoid including a file in a loop, very inefficient.

The function might look something like this, well will say that the file that it is in is called func.php

 

function findPoints($points) {
      // if {} else if {} conditionals go here
      return $points; //return points value
}

 

Then in your main page you will include func.php at the top, and in your while loop after the $place variable is defined you will add this line

 

$points = findPoints($place); // will contain the return value of the function.

 

Then output $points like I have already done in the previous post.

 

 

You may find it cleaner to simply use a lookup array for this, instead of that mess of elseif()s. Untested, since I don't have your table structure, but you get the idea.

 

<?php
$points = 0;
$lookup = array( '1st' => 50, '2nd' => 45, '3rd' => 40, '4th' =>  35,
'5th' => 30, '6th' => 25, '7th' => 20, '8th' => 10,
'9th' => 10, '10th' => 10, 'CH' => 50, 'RCH' => 40,
'TT' => 30, 'T5' => 30, 'Champion' => 50, 'Reserve Champion' =>  40 );

$query92 = "SELECT class, place, entries, `show`, FORMAT(purse, 2) FROM THISTABLE WHERE VARIABLE='$id' OR VARIABLE = '$name' ORDER BY ABS(VARIABLE), VARIABLE";
$result92 = mysql_query($query92) or die (mysql_error());
echo "<table class='record'>\n
<tr><th>Show</th> <th>Class</th> <th>Place</th></tr>\n";
while($row92 = mysql_fetch_array($result92)) {
echo "<tr><td>{$row92['race']}</td> <td>{$row92['class']}</td> <td>{$row92['place']}</td></tr>";
$points += $lookup[$row92['place']];
}
echo "<tr><td colspan='3'><div align='right'>Total Points: $points</div></td></tr>\n
</table>\n
</div>\n";

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.