Forum Moderators: coopster
I am trying to create a script to calculate and update user's points based on the total number of players.
Here is my code so far:
<?php
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST[update])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points\">
$points[points]
<input type=\"checkbox\" value=\"played\" name=\"played\">
<select name=\"top10\">
<option value=\"0\">0
<option value=\"1\">1st
<option value=\"2\">2nd
<option value=\"3\">3rd
<option value=\"4\">4th
<option value=\"5\">5th
<option value=\"6\">6th
<option value=\"7\">7th
<option value=\"8\">8th
<option value=\"9\">9th
<option value=\"10\">10th
</select>
<input type=\"hidden\" value=\"$points[username]\" name=\"username\">
<a href=\"members.php?user=$points[username]\">$points[username]</a><br />\n");
}
echo ("<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>");
}
} else {
mysql_query("SELECT * FROM users");
echo ("Updated!");
$numplayers = "$_POST[numplayers]";
$played = "$_POST[played]";
$top10 = "$_POST[top10]"; $username = "$_POST[username]";
$currentpoints = "$_POST[points]";
$totalamount = 0;
$totalamount = $numplayers * 100;
$first = ('$totalamount' * 0.30);
$second = $totalamount * 0.20;
$third = $totalamount * 0.12;
$fourth = $totalamount * 0.10;
$fifth = $totalamount * 0.08;
$sixth = $totalamount * 0.06;
$seventh = $totalamount * 0.05;
$eighth = $totalamount * 0.04;
$ninth = $totalamount * 0.03;
$tenth = $totalamount * 0.02;
$playedpoints = 0;
if ($played == true) {
$playedpoints = 100;
} else {
$playedpoints = NULL;
}
$newpoints = 0;
if ($top10 == 0) {
$totalpoints = NULL;
} elseif ($top10 == 1) {
$newpoints = $first;
} elseif ($top10 == 2) {
$newpoints = $second;
} elseif ($top10 == 3) {
$newpoints = $third;
} elseif ($top10 == 4) {
$newpoints = $fourth;
} elseif ($top10 == 5) {
$newpoints = $fifth;
} elseif ($top10 == 6) {
$newpoints = $sixth;
} elseif ($top10 == 7) {
$newpoints = $seventh;
} elseif ($top10 == 8) {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
} else {
$newpoints = NULL;
}
$newtotal = (('$currentpoints' + '$newpoints') + 'playedpoints');
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update);
}
?>
As you can see, I am trying to have the first input box calculate the total amount of points by multiplying the total amount of players by 100.
I then want a checkbox with a value of 100 points to be checked or left blank that will determine whether or not a player was in the tournament. Then, I have a dropdown with the placement in the final table from 1-10.
If the box is checked, I want only 100 points added to the user's previous points and if the box and a top10 place is present, then I want the points from the player's place and the 100 points to be added...
the problem is, right now nothing is updating.
I cannot seem to figure it out. I am also new to php programming so if anyone has any ideas on ways to make this function better, please let me know!
Thanks in advance!
First thing you need to look at is a little debugging. As nothing is updating you need to make sure the problem isn`t with the database. So, change this:
mysql_query($update);
to this:
mysql_query($update) or die(mysql_error());
Next thing to do is set your error reporting level to E_ALL, so at the top of your script add this:
error_reporting(E_ALL);
See if that throws any errors.
dc
Thanks for the prompt response.
I am not getting any errors- none whatsoever
Here is my updated code:
<?php
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST['update'])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points\">
$points[points]
<input type=\"checkbox\" value=\"played\" name=\"played\">
<select name=\"top10\">
<option value=\"0\">0
<option value=\"1\">1st
<option value=\"2\">2nd
<option value=\"3\">3rd
<option value=\"4\">4th
<option value=\"5\">5th
<option value=\"6\">6th
<option value=\"7\">7th
<option value=\"8\">8th
<option value=\"9\">9th
<option value=\"10\">10th
</select>
<input type=\"hidden\" value=\"$points[username]\" name=\"username\">
<a href=\"members.php?user=$points[username]\">$points[username]</a><br />\n");
}
echo ("<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>");
}
} else {
echo ("Updated!");
$numplayers = $_POST['numplayers'];
$played = $_POST['played'];
$top10 = $_POST['top10'];$username = $_POST['username'];
$currentpoints = $_POST['points'];
$totalamount = 0;
$totalamount = $numplayers * 100;
$first = $totalamount * 0.30;
$second = $totalamount * 0.20;
$third = $totalamount * 0.12;
$fourth = $totalamount * 0.10;
$fifth = $totalamount * 0.08;
$sixth = $totalamount * 0.06;
$seventh = $totalamount * 0.05;
$eigth = $totalamount * 0.04;
$ninth = $totalamount * 0.03;
$tenth = $totalamount * 0.02;
$playedpoints = 0;
if ($played == true) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}
$newpoints = 0;
if ($top10 == 0) {
$newpoints = 0;
} elseif ($top10 == 1) {
$newpoints = $first;
} elseif ($top10 == 2) {
$newpoints = $second;
} elseif ($top10 == 3) {
$newpoints = $third;
} elseif ($top10 == 4) {
$newpoints = $fourth;
} elseif ($top10 == 5) {
$newpoints = $fifth;
} elseif ($top10 == 6) {
$newpoints = $sixth;
} elseif ($top10 == 7) {
$newpoints = $seventh;
} elseif ($top10 == 8) {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
}
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update) or die(mysql_error());
}
?>
The problem I am having now is simply that it is not updating EVERY user, and rather only the LAST user. So if my query returns 15 users ordered by id (1-15), only the user with the id of 15 gets updated.
someone told me that i should use arrays as my input names:
EXAMPLE:
input type=\"hidden\" value=\"$points[points]\" name=\"points[]\">
$points[points]
<input type=\"checkbox\" value=\"played\" name=\"played[]\">
<select name=\"top10[]\" value=\"top10\">
<option value=\"0\">0
<option value=\"1\">1st
<option value=\"2\">2nd
<option value=\"3\">3rd
<option value=\"4\">4th
<option value=\"5\">5th
<option value=\"6\">6th
<option value=\"7\">7th
<option value=\"8\">8th
<option value=\"9\">9th
<option value=\"10\">10th
</select>
<input type=\"hidden\" value=\"$points[username]\" name=\"username[]\">
but i do not know how to do this correctly...
any help?
is this the right direction?
I didn't really think it came from that actual update so I thought it might come from the previous generation
look at the source code for the page you are using to update
it might actually have 'Resource id #3 ' as one of your values
I also noticed some things in your code which may cause issues so I reformatted it to this
<?php
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST['update'])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo "<input type=\"hidden\" value=\"",$points['points'],"\" name=\"points\">",$points[points]," "
?>
<input type="checkbox" value="played" name="played">
<select name="top10">
<option value="0">0
<option value="1">1st
<option value="2">2nd
<option value="3">3rd
<option value="4">4th
<option value="5">5th
<option value="6">6th
<option value="7">7th
<option value="8">8th
<option value="9">9th
<option value="10">10th
</select>
<?php
echo "<input type=\"hidden\" value=\"",$points[username],"\" name=\"username\">";
echo "<a href=\"members.php?user=",$points[username],"\">",$points[username],"</a><br />\n";
}
echo "<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>";
}
} else {
echo ("Updated!");
$numplayers = $_POST['numplayers'];
$played = $_POST['played'];
$top10 = $_POST['top10'];
$username = $_POST['username'];
$currentpoints = $_POST['points'];
$totalamount = 0;
$totalamount = $numplayers * 100;
$first = $totalamount * 0.30;
$second = $totalamount * 0.20;
$third = $totalamount * 0.12;
$fourth = $totalamount * 0.10;
$fifth = $totalamount * 0.08;
$sixth = $totalamount * 0.06;
$seventh = $totalamount * 0.05;
$eigth = $totalamount * 0.04;
$ninth = $totalamount * 0.03;
$tenth = $totalamount * 0.02;
$playedpoints = 0;
if ($played == true) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}
$newpoints = 0;
if ($top10 == 0) {
$newpoints = 0;
} elseif ($top10 == 1) {
$newpoints = $first;
} elseif ($top10 == 2) {
$newpoints = $second;
} elseif ($top10 == 3) {
$newpoints = $third;
} elseif ($top10 == 4) {
$newpoints = $fourth;
} elseif ($top10 == 5) {
$newpoints = $fifth;
} elseif ($top10 == 6) {
$newpoints = $sixth;
} elseif ($top10 == 7) {
$newpoints = $seventh;
} elseif ($top10 == 8) {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
}
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update) or die(mysql_error());
}
?>
this escapes from php parsing to output that chunk of html and uses better echo syntax for your arrays, which may not have been resolving inside double quotes.
give those two things a try and tell us what happens
you should also echo your constructed query before sending it to mysql like so
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
echo '<p>update query:<br>',$update;
mysql_query($update) or die(mysql_error());
then you can actually look at the query and see if it looks right. You could also paste it into the command line or into phpmyadmin to test it
I have several different versions of this code now and none of them will work. I have created loops and used foreach statements and for statements and I simply cannot get this script to update each user individually. I have gotten it to update one user on the list, and update each user with one set of points, but not each user individually.
It is pretty frustrating.
By the way, the formatting you did only had simple errors... $variable[value] instead of $variable['value']
did you check your source to see if the error was being generated previously?
>> the formatting you did only had simple errors
did you notice that some of those were concatenation errors that wouldn't actually resolve in the way they were?
such as the arrays within echo's which will only resolve properly when they are surrounded by braces
echo your query and see what it looks like, don't worry about sending them to the db, just print all of them to the screen on individual lines and see if that shows where the error is.
I could probably write your update script in a minute or two but that wouldn't really help anyone learn anything. Go with what you had above and try to see if you can isolate why it is misbehaving. Have it spit out some variables to the browser and diagnose it, don't try and rewrite it every time. Whenever I do that I usually make things worse unless I go back and start from scratch.
the echo returned the following:
UPDATE users SET points = '2800' WHERE username = 'test1'UPDATE users SET points = '2800' WHERE username = 'test1'
test1 was the last user on the list. 2800 is an accurate calculation of the total points. it is odd that it only repeated it twice- i would have expected it to have repeated 4 times (since there are 4 users in the database)
<?php
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST['update'])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC") or die(mysql_error());
echo ("<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points[]\">
$points[points]
<input type=\"checkbox\" value=\"1\" name=\"played[]\">
<select name=\"top10[]\">
<option value=\"0\">0
<option value=\"1\">1st
<option value=\"2\">2nd
<option value=\"3\">3rd
<option value=\"4\">4th
<option value=\"5\">5th
<option value=\"6\">6th
<option value=\"7\">7th
<option value=\"8\">8th
<option value=\"9\">9th
<option value=\"10\">10th
</select>
<input type=\"hidden\" value=\"$points[username]\" name=\"username[]\">
<a href=\"members.php?user=$points[username]\">$points[username]</a><br />\n");
}
echo ("<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>");
}
} else {
echo ("Updated!<br>");
$numPlayers = $_POST['numplayers'];
$Played = $_POST['played'];
$top10 = $_POST['top10'];
$Username = $_POST['username'];
$Points = $_POST['points'];
$totalamount = $numPlayers * 100;
if ($Played == true) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}
if ($top10 == 1) {
$newpoints = $totalamount * 0.30;
} elseif ($top10 == 2) {
$newpoints = $totalamount * 0.20;
} elseif ($top10 == 3) {
$newpoints = $totalamount * 0.12;
} elseif ($top10 == 4) {
$newpoints = $totalamount * 0.10;
} elseif ($top10 == 5) {
$newpoints = $totalamount * 0.08;
} elseif ($top10 == 6) {
$newpoints = $totalamount * 0.06;
} elseif ($top10 == 7) {
$newpoints = $totalamount * 0.05;
} elseif ($top10 == 8) {
$newpoints = $totalamount * 0.04;
} elseif ($top10 == 9) {
$newpoints = $totalamount * 0.03;
} elseif ($top10 == 10) {
$newpoints = $totalamount * 0.02;
} else {
$newpoints = 0;
}
foreach($Username as $i=>$User)
{
$npoints = $newpoints;
$cpoints = $Points[$i];
$newtotal = ($cpoints + $npoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$User'";
mysql_query($update) or die(mysql_error());
echo "<br><br>$update<br>";
}
}
?>