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
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

 

 

 

 

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.