Page 1 of 2
Need some help with this code/database!
Posted: Thu Sep 07, 2006 6:37 am
by lecram
right now i have this code and i know there are issues because i am new to php
Code: Select all
if (!$_POST[update]) {
$query = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form action=\"totalpoints.php\" method=\"post\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
$numusers = mysql_num_rows($query);
if ($numusers == 0) {
echo ("There are no users!");
} else {
while ($get = mysql_fetch_array($query)) {
echo ("
<input type=\"hidden\" value=\"$get[username]\" name=\"username\">
<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>
<a href=\"members.php?user=$get[username]\">$get[username]</a><br />\n");
}
}
echo ("<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>");
} else {
$numplayers = htmlspecialchars($_POST[numplayers]);
}
and then this:
Code: Select all
$numplayers = $_POST['numplayers'];
$top10 = $_POST['top10'];
$username = $_POST['username'];
echo "<strong>Result</strong>";
echo "<br>";
echo $numplayers . " players<BR>";
$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;
if ($top10 == 0) {
$points = 100;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 1) {
$points = $first + 100;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 2) {
$points = $second + 100;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 3) {
$points = $third + 100;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 4) {
$points = $fourth + 100;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 5) {
$points = $fifth + PLAYERPOINTS;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 6) {
$points = $sixth + PLAYERPOINTS;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 7) {
$points = $seventh + PLAYERPOINTS;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == {
$points = $eighth + PLAYERPOINTS;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 9) {
$points = $ninth + PLAYERPOINTS;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} elseif ($top10 == 10) {
$points = $tenth + PLAYERPOINTS;
$update = mysql_query("Update users set points = '$points' where username = '$username'");
} else {
echo ("Updated!");
}
however, it is only updating the last user on the list, not all of them. also, how would i go about adding the points to the database and not overwriting them each time i hit the submit button?
Posted: Fri Sep 08, 2006 1:07 am
by RobertGonzalez
Your code will only update the record for $username. But even that is not going to work unless you created an array called $get and used that in your form.
Is this:
supposed to be?:
Posted: Sun Sep 10, 2006 2:05 am
by lecram
Here is my updated code...
I am still having the same problem though... It is only updating the last person... Can someone please help?
Code: Select all
<?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 = 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 == {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
} else {
$newpoints = 0;
}
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update);
}
?>
Posted: Sun Sep 10, 2006 2:23 am
by n00b Saibot
why this
Code: Select all
$numplayers = "$_POST[numplayers]";
$played = "$_POST[played]";
$top10 = "$_POST[top10]";
$username = "$_POST[username]";
$currentpoints = "$_POST[points]";
instead of this
Code: Select all
$numplayers = $_POST['numplayers'];
$played = $_POST['played'];
$top10 = $_POST['top10'];
$username = $_POST['username'];
$currentpoints = $_POST['points'];
you store number of players in $numplayers variable... but are you returning all the usernames in the array.. I suppose only last ne gets passed and is updated in the end.
Posted: Sun Sep 10, 2006 2:36 am
by lecram
The $numplayers variable is just to calculate the total amount of points...
Any solution?
Posted: Sun Sep 10, 2006 2:55 am
by n00b Saibot
can you shoe me how you get the list of usenames?
Posted: Sun Sep 10, 2006 3:28 am
by lecram
yeah its just a simple query
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
while ($points = mysql_fetch_array($result)) {
and then i am displaying the username as a link to the userprofile
<a href=\"members.php?user=$points[username]\">$points[username]</a><br />
Posted: Sun Sep 10, 2006 3:35 am
by lecram
That has to be where my problem is...
I just don't know how it should be!
Posted: Sun Sep 10, 2006 4:46 am
by n00b Saibot
ah, I see it.. you are outputting multiple players along wth their attributes for updattion? in this case you should use arrays as field names... like f.e. take username fld...
Code: Select all
<input type=\"hidden\" value=\"$points[username]\" name=\"username\">
should be
Code: Select all
<input type=\"hidden\" value=\"$points[username]\" name=\"username[]\">
also noticed this...
Code: Select all
input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\">
no value in the numplayers fld...
Posted: Sun Sep 10, 2006 5:11 am
by lecram
if i do that, i need to change a lot of my variables though, right?
and how would i process the info?
Posted: Mon Sep 11, 2006 1:58 am
by n00b Saibot
lecram wrote:and how would i process the info?
like you would process any array...
Code: Select all
$Users = $_POST['username'];
$Points = $_POST['points'];
$totPlayers = count($Users);
foreach($Users as $idx=>$aUser)
{
$currentpoints = $Points[$idx];
$totalamount = 0;
$totalamount = $totPlayers * 100;
//calculation...
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$aUser'";
}
Posted: Mon Sep 11, 2006 11:40 pm
by lecram
thanks for the help saibot
i unfortunately cannot get it to work. i keep running into the same issues- i can get it to loop, i just cant get it to loop one instance, so if i enter a value for one user, it applys the value to every user. i am going to settle with a setup that is a little more simple but less appealing... pretty dissapointing...
thanks anyway though. if anyone has any ideas or pointers, please feel free to post them. i cannot let this beat me so i will still try to make it happen, but for now i need to get this site up.
Posted: Tue Sep 12, 2006 10:35 am
by RobertGonzalez
Here is the second bit of code you posted, with some comments. See if they are helpful...
Code: Select all
<?php
// Why are you using output buffering?
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
/*
This is checking to see if the POST array var 'update'
is NOT set. I suppose this assumes a posted form?
*/
if (!isset($_POST['update'])) {
// There is no error checking on this query
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
// There is no action here
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
// Check to see if there was no returned result
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
/*
This is now looping to display records from the query
This will create 2 hidden fields, a checkbox and a
select element for each record in the `users` table
*/
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 {
/*
If we are here then the form field called 'update' was
posted. But you are not checking for values in any other
form field that was posted.
*/
echo ("Updated!");
/*
Your syntax for each of these is wrong
You may want to change them to:
$numplayers = $_POST['numplayers'];
and so on throughout the script
*/
$numplayers = "$_POST[numplayers]";
$played = "$_POST[played]";
$top10 = "$_POST[top10]";
$username = "$_POST[username]";
$currentpoints = "$_POST[points]";
$totalamount = 0;
// You realize that if $totalamount is 0 then this will be 0 also?
$totalamount = $numplayers * 100;
// And if total amount was 0 above, then it will 0 in all of these?
$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;
// This may not evaluate the way you think based on your assingment syntax above
if ($played == true) {
$playedpoints = 100;
} else {
// You already set this a few lines up, you don't need this 'else'
$playedpoints = 0;
}
$newpoints = 0;
/*
With the exception of the first conditional in this list
every other conditional will result in 0 based on the 0
multiplier above.
You may also want to consider a switch statement here
*/
if ($top10 == 0) {
$newpoints = 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 == {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
} else {
$newpoints = 0;
}
// This will more than likely be $currentpoints + 100 (or + 0)
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
// this will update the one user for whom the form was posted
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
/*
Again, you are not using any error checking.
How are you going to know if something went wrong?
*/
mysql_query($update);
}
?>
Posted: Tue Sep 12, 2006 10:07 pm
by lecram
everah- your post was extremely helpful!
i will analyze my code once more and post the result!
thanks again
Posted: Wed Sep 13, 2006 1:28 am
by lecram
In one of your comments you wrote "// this will update the one user for whom the form was posted "
With saibot's help, I have gotten the script to update every user, however I still have issues.
Code: Select all
<?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!");
$numplayers = $_POST['numplayers'];
$played = $_POST['played'];
$top10 = $_POST['top10'];
$User = $_POST['username'];
$Points = $_POST['points'];
foreach($User as $idx=>$aUser)
{
$totalamount = $numplayers * 100;
if ($played == 1) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}
if ($top10 == 0) {
$newpoints = 0;
} elseif ($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 == {
$newpoints = $totalamount * 0.04;
} elseif ($top10 == 9) {
$newpoints = $totalamount * 0.03;
} elseif ($top10 == 10) {
$newpoints = $totalamount * 0.02;
} else {
$newpoints = 0;
}
$cpoints = $Points[$idx];
$newtotal = ($cpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$aUser'";
mysql_query($update) or die(mysql_error());
echo "<br><br>$update<br>";
}
}
?>
As of now my querys are much better than before. I still cannot get the checkbox to effect each user individually. If one is checked, it will effect every user. I also
still cant get the top10 to work...
Code: Select all
<?
switch($top10) {
case 0;
$newPoints = 0;
break;
case 1;
$newPoints = $totalamount * 0.30;
break;
case 2;
$newPoints = $totalamount * 0.20;
break;
case 3;
$newPoints = $totalamount * 0.12;
break;
case 4;
$newPoints = $totalamount * 0.10;
break;
case 5;
$newPoints = $totalamount * 0.08;
break;
case 6;
$newPoints = $totalamount * 0.06;
break;
case 7;
$newPoints = $totalamount * 0.05;
break;
case 8;
$newPoints = $totalamount * 0.04;
break;
case 9;
$newPoints = $totalamount * 0.03;
break;
case 10;
$newPoints = $totalamount * 0.02;
break;
}
?>
Should I go with the switch method? I thought that the if/elseif statements are better suited for this situation?