AnAmericanGunner Posted January 1, 2012 Share Posted January 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/ Share on other sites More sharing options...
AyKay47 Posted January 1, 2012 Share Posted January 1, 2012 So you want it to output the total amount of points once? Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303169 Share on other sites More sharing options...
AnAmericanGunner Posted January 2, 2012 Author Share Posted January 2, 2012 Yes. If the records show two 5th places, that should be 60 points, for example. Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303178 Share on other sites More sharing options...
AyKay47 Posted January 2, 2012 Share Posted January 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303195 Share on other sites More sharing options...
AnAmericanGunner Posted January 2, 2012 Author Share Posted January 2, 2012 Thank you that works. As for the function, could you give me an example? I've only ever done points this way. :S Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303202 Share on other sites More sharing options...
AyKay47 Posted January 2, 2012 Share Posted January 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303206 Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2012 Share Posted January 2, 2012 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"; Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303211 Share on other sites More sharing options...
AnAmericanGunner Posted January 2, 2012 Author Share Posted January 2, 2012 Thank you, I'll keep that in mind I will experiment later. Quote Link to comment https://forums.phpfreaks.com/topic/254178-variable-variable-number-script-not-working/#findComment-1303217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.