Jump to content

[SOLVED] Not doin' the math right


TimUSA

Recommended Posts

When this submits to the database, it is not calculating $points correctly???

 

// CONFIGURATION SECTION
$times = $_POST['boats'];
$x = 0;

///POST TO DATABASE
if (isset($_POST['submitted'])) {

if ($_POST['points'][$x] == "DNF")
$points = ($_POST['boats'] +1) * $_POST['factor'];

else if ($_POST['points'][$x] == "DNS")
$points = ($_POST['boats'] +1) * $_POST['factor'];

else
$points = $_POST['points'][$x] * $_POST['factor'];

if ($_POST['factor'] == "3") {
while ($x < $times) {
mysql_query("INSERT INTO `ladder_points` (`memberName`, `raceDate`, `factor`, `racePoints`, `matchPoints`)
              VALUES
             ('{$_POST['name'][$x]}','{$_POST['date']}','{$_POST['factor']}','{$_POST['points'][$x]}','{$points}')
             ");
$x++;
}
} else {
while ($x < $times) {
mysql_query("INSERT INTO `ladder_points` (`memberName`, `raceDate`, `factor`, `racePoints`, `fleetPoints`)
              VALUES
             ('{$_POST['name'][$x]}','{$_POST['date']}','{$_POST['factor']}','{$_POST['points'][$x]}','{$points}')
             ");
$x++;
}
}
}

Link to comment
https://forums.phpfreaks.com/topic/83800-solved-not-doin-the-math-right/
Share on other sites

the first part gets data from a previously submitted form

$_POST['boats'] = the number of boats entered into a race

$_POST['factor'] = a weighting factor for calculating points for a ladder

$_POST['points'] = equals the actual points scored in the race

 

so:

if ($_POST['points'][$x] == "DNF")
$points = ($_POST['boats'] +1) * $_POST['factor'];

else if ($_POST['points'][$x] == "DNS")
$points = ($_POST['boats'] +1) * $_POST['factor'];

 

if points scored = DNF (Did Not Finish) or DNS (Did Not Start)

(ladder points = (number of boats + 1) * weighting factor)

 

else
$points = $_POST['points'][$x] * $_POST['factor'];

otherwise (ladder points = actual points * weighting factor)

 

the above parts work because I have echoed the values to test them, what is not working is when I am posting them to the database:

if ($_POST['factor'] == "3") {
while ($x < $times) {
mysql_query("INSERT INTO `ladder_points` (`memberName`, `raceDate`, `factor`, `racePoints`, `matchPoints`)
              VALUES
             ('{$_POST['name'][$x]}','{$_POST['date']}','{$_POST['factor']}','{$_POST['points'][$x]}','{$points}')
             ");
$x++;

So if the factor is 3 then assign points to the matchPoints column:

 

or

 

else {
while ($x < $times) {
mysql_query("INSERT INTO `ladder_points` (`memberName`, `raceDate`, `factor`, `racePoints`, `fleetPoints`)
              VALUES
             ('{$_POST['name'][$x]}','{$_POST['date']}','{$_POST['factor']}','{$_POST['points'][$x]}','{$points}')
             ");
$x++;

basically all other factors, assign to the fleet racing column

 

all seems to be inserting correctly except the above points calculation

'{$points}'

 

this is what a sample looks like in the database:

raceIDmemberNameDatefactorracePointsmatchPointsfleetPoints

39ARK Racing2007-12-2843.00NULL3.00

 

so if

 

fleetPoints = racePoints * factor

 

in this example this should be 12 not 3

 

 

 

 

 

 

 

if ($_POST['points'][$x] == "DNF")
$points = ($_POST['boats'] +1) * $_POST['factor'];

else if ($_POST['points'][$x] == "DNS")
$points = ($_POST['boats'] +1) * $_POST['factor'];

That's redundant.

 

Same as:

if ($_POST['points'][$x] == "DNF" || $_POST['points'][$x] == "DNS")
$points = ($_POST['boats'] +1) * $_POST['factor'];

 

You don't need  {} around $points.

 

Did you try echoing out what $points is before you insert it into the database?

the above parts work because I have echoed the values to test them, what is not working is when I am posting them to the database:

 

I think it's likely that they query is not succeeding.  Can you do your queries like this:

 

$result = mysql_query("...");
if ($result === false) die("Error in query: " . mysql_error());

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.