Need help calculating wins and losses from points scored - php

I am creating a website in php to log ping pong scores for my school, and currently the player who wins will log the WinnerID, LoserID, PointsFor, PointsAgainst. I have two tables with the following relationships.
Table: users
user_ID (PK)
username
Elo
Table: games
game_id (PK)
WinnerID (FK)
LoserID (FK)
PointsFor
PointsAgainst
My insert statement in the php file is:
INSERT INTO games(WinnerID,LoserID,PointsFor,PointsAgainst) VALUES('$Winner_ID','$Loser_ID','$userscore','$oppscore')"
Here is what I have tried, but it doesn't display the scores correctly.
SELECT min(u.username) 'Username', COUNT(g.WinnerID) 'Wins', sum(g.PointsFor) 'Points For', sum(g.PointsAgainst) 'Points Against', u.Elo 'Ranking'
from games g
LEFT JOIN users u
on g.WinnerID = u.user_id
Group by g.WinnerID
As you can see by the image above, the points for and points against totals don't add up. Currently, it only displays the stats for whoever was winner. Meaning if PlayerA wins 21-5, it will show up from the select statement, but PlayerB will not show a score of 5-21. Any help is appreciated.
PHP code for page to enter scores:
if(isset($_POST['btn-post']))
{
$opponent = $_POST["opponent"];
//$opponent = array_key_exists('opponent', $_POST) ? $_POST['opponent'] : false;
$userscore = mysql_real_escape_string($_POST['userscore']);
$oppscore = mysql_real_escape_string($_POST['oppscore']);
if($userscore != $oppscore)
{
if($userscore > $oppscore)
{
$Winner_ID = $_SESSION['user'];
$query = mysql_query("SELECT user_id FROM users WHERE username = '".$opponent."'");
$result = mysql_fetch_array($query) or die(mysql_error());
$Loser_ID = $result['user_id'];
$query1 = mysql_query("SELECT Elo FROM users WHERE user_id=".$_SESSION['user']);
$result1 = mysql_fetch_array($query1) or die(mysql_error());
$winnerRating = $result1['Elo'];
$query2 = mysql_query("SELECT Elo FROM users WHERE user_id=".$Loser_ID);
$result2 = mysql_fetch_array($query2) or die(mysql_error());
$loserRating = $result1['Elo'];
$rating = new Rating($winnerRating, $loserRating, 1, 0);
$results = $rating->getNewRatings();
if(mysql_query("UPDATE users SET Elo = " . $results['a'] . " WHERE user_id=".$_SESSION['user']))
{
}
else
{
?>
<script>alert('There was an error while entering winners(user) ranking...');</script>
<?php
}
if(mysql_query("UPDATE users SET Elo = " . $results['b'] . " WHERE user_id=".$Loser_ID))
{
}
else
{
?>
<script>alert('There was an error while entering losers(opp) ranking..');</script>
<?php
}
}
elseif($oppscore > $userscore)
{
$Loser_ID = $_SESSION['user'];
$query = mysql_query("SELECT user_id FROM users WHERE username = '".$opponent."'");
$result = mysql_fetch_array($query) or die(mysql_error());
$Winner_ID = $result['user_id'];
//get rating from user table in database
$query1 = mysql_query("SELECT Elo FROM users WHERE user_id=".$_SESSION['user']);
$result1 = mysql_fetch_array($query1) or die(mysql_error());
$loserRating = $result1['Elo'];
$query2 = mysql_query("SELECT Elo FROM users WHERE user_id=".$Loser_ID);
$result2 = mysql_fetch_array($query2) or die(mysql_error());
$winnerRating = $result1['Elo'];
$rating = new Rating($winnerRating, $loserRating, 1, 0);
$results = $rating->getNewRatings();
$results = $rating->getNewRatings();
if(mysql_query("UPDATE users SET Elo = " . $results['b'] . " WHERE user_id=".$_SESSION['user']))
{
}
else
{
?>
<script>alert('There was an error while entering losers(user) ranking...');</script>
<?php
}
if(mysql_query("UPDATE users SET Elo = " . $results['a'] . " WHERE user_id=".$Winner_ID))
{
}
else
{
?>
<script>alert('There was an error while entering winners(opp) ranking...');</script>
<?php
}
}
if(mysql_query("INSERT INTO games(WinnerID,LoserID,PointsFor,PointsAgainst) VALUES('$Winner_ID','$Loser_ID','$userscore','$oppscore')"))
{
?>
<script>alert('Your scores were successfully entered');</script>
<?php
}
else
{
?>
<script>alert('There was an error while entering your score...');</script>
<?php
}
}
else
{
?>
<script>alert('There cannot be a tie in ping pong, please re-enter your scores...');</script>
<?php
}
}
?>

Your query fails because it doesn't take into account the rows where the player loses. You can fix that by using unions. The following query should do what you want:
SELECT username AS "Username",
SUM(wins) AS "Wins",
SUM(PF) AS "Points For",
SUM(PA) AS "Points Against",
elo AS "Ranking"
FROM (
(SELECT users.user_ID,
users.username AS username,
COUNT(games.WinnerID) AS wins,
SUM(games.PointsFor) AS PF,
SUM(games.PointsAgainst) AS PA,
users.Elo AS elo
FROM users, games
WHERE games.WinnerID = users.user_ID
GROUP BY users.user_ID)
UNION ALL
(SELECT users.user_ID,
users.username AS username,
0 AS wins,
SUM(games.PointsAgainst) AS PF,
SUM(games.PointsFor) AS PA,
users.Elo AS elo
FROM users, games
WHERE games.LoserID = users.user_ID
GROUP BY users.user_ID)
) AS t
GROUP BY username
ORDER BY user_ID;
Note that in the "losing query" the field PointsAgainst should be counter as the player's "points for" and vice-versa.

Try it as an inner join, and get rid of the MIN() on the username column:
SELECT u.username, COUNT(g.WinnerID),
SUM(g.PointsFor), SUM(g.PointsAgainst), u.Elo
FROM users u, games g
WHERE u.user_id = g.WinnerID
GROUP BY u.username, u.Elo;
Also, before anyone else takes you to task, you should be using mysqli instead of mysql (or better yet, PDO) and you should be using prepared statements instead of dynamic SQL.

Your
INSERT INTO games(WinnerID,LoserID,PointsFor,PointsAgainst) VALUES('$Winner_ID','$Loser_ID','$userscore','$oppscore')
query contains the $userscore and $oppscore values in a potentially wrong order. The $Winner_ID and $Loser_ID may change in your conditional processing, but $userscore and $oppscore are not similarly flipped at that time.
Also your conditional structure is in general unnecessarily verbose. It seems like you should be able to just determine the winner and loser ID and score first, and then do all your processing once instead of resorting to error-prone copy-paste duplication of near-identical code.

Related

Sort MySQL query in PHP

I'm trying to sort different users 'points' by descending order (highest first). But at the moment the query is being returned in order of the user's ID (order they appear in the database). I'm not sure what is wrong with my code?
The user can be in multiple leagues, so it first queries to see what leagues that the particular user is in. With the league IDs, I query to see what users are in each of the leagues. Then I query what each users total points are within that league. Ultimately, I want to get the rank of the user for each league but at the moment the query to order by the points is not working.
The image shows how the points are coming out. '1635' is the users points that is logged in. For the first league, I'm trying to get 'rank 2' displayed.
// SQL query to see what leagues user is in
$query = mysqli_query($con, "SELECT * FROM UserLeague WHERE UserID='$userid'");
$num = mysqli_num_rows($query);
if($num == 0) {
echo 'You are not in any leagues';
return;
} else {
echo '<div class="pleague-table">';
echo '<div class="pleague-table-header">';
echo '<p>PRIVATE LEAGUE</p>';
echo '<p>CURRENT RANK</p>';
echo '</div>';
}
while($leagueid = mysqli_fetch_assoc($query)) {
$lid = $leagueid['LeagueID'];
// Get all league info that user is in
$query2 = mysqli_query($con, "SELECT * FROM League WHERE LeagueID='$lid'");
// Get all users that is in each league
$queryposition = mysqli_query($con, "SELECT UserID FROM UserLeague WHERE LeagueID='$lid'");
while($getpoints = mysqli_fetch_assoc($queryposition)) {
$uid = $getpoints['UserID'];
// Get each users points in each league
$querypoints = mysqli_query($con, "SELECT * FROM Points WHERE UserID='$uid' ORDER BY total DESC");
while($row = mysqli_fetch_assoc($querypoints)) {
echo $row['total']. '</br>';
}
}
while($leaguename = mysqli_fetch_assoc($query2)) {
echo '<div class="league-link">';
echo $leaguename['Name'];
echo 'Options';
echo '</div>';
}
}
'''
You are trying to combine 2 tables: UserLeague and Points to select users from UserLeague and order them by Points. For such cases there is JOIN syntax in SQL:
SELECT Points.*
FROM Points
RIGHT JOIN UserLeague ON Points.UserID=UserLeague.UserID
WHERE UserLeague.LeagueID=?
ORDER BY Points.total DESC

Do I need a left, natural or simple join in SQL?

I am new to PHP coding and just trying to fix some functionality on my site that was left over from the lead developer.
The site, [Vloggi], is a marketplace. So I need to show the name of the job poster in the assignments page . The table I have the jobs in only has the ID, not the name.
So I need a join, but I've tried and it breaks the entire site.
The SQL has 17 tables, I need to display the User Name (usr_name) contained in table 3, the organisation contained in table 7 (usrg_orgname) with the job posting user (vlop_usr_id) details in table 14.
The primary key is users.usr_id, which is linked to users_gor.usrg_usr_id and vlog-ops.vlog_usr_id.
Table 3: users
usr_id, usr_email, usr_password, usr_fbuser, usr_fbtoken, usr_name, usr_loc_name, usr_loc_lat1, usr_loc_lon1, usr_loc_lat2, usr_loc_lon2, usr_status, usr_gor, usr_vgr, usr_token, usr_regtoken,
table 7: users_gor
usrg_usr_id, usrg_creditops, usrg_creditvlog, usrg_creditvlogette, usrg_destination, usrg_orgname, usrg_orgtype, usrg_location, usrg_website, usrg_jobtitle, usrg_phone, usrg_address1, usrg_address2, usrg_state, usrg_postcode, usrg_country
Table 14: vlog-ops
vlop_id, vlop_title, vlop_description, vlop_tags, vlop_deadline, vlop_quantity, vlop_quantityposted, vlop_vser_id, vlop_usr_id,vlop_loc_name, vlop_loc_lat1, vlop_loc_lon1, vlop_loc_lat2, vlop_loc_lon2, vlop_campaign, vlop_rules, vlop_tips, vlop_status
So in main.php i have written the following Sql lookup
in main.php, I have the following SQL lookups:
$sql = "SELECT * FROM users_gor WHERE usrg_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_gor = $rows[0];
$sql = "SELECT * FROM users_vgr WHERE usrv_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_vgr = $rows[0];
$sql = "SELECT * FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT * FROM vlog-ops WHERE vlop_usr_id ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT usr_name AS vlop_usr_name FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
And then in the page template itself, I have written
<?php echo $vlop['vlop_vser_id'] ?>
<?php echo $vlop['vlop_usr_name'] ?>
The first one works, the second doesn’t. What I want eventually is to display the user name and the organisation name in a table.
Whenever I try a JOIN or a NATURAL JOIN or a LEFT JOIN it breaks and the entire site goes blank.
Any help for a newbie would be appreciated with a million thanks.
When you use JOIN you need to specify how you're joining them.
In the query below I'm assuming you're looking for the fields in bold from your question.
$query='SELECT u.usr_name, g.usrg_orgname, v.vlop_usr_id FROM users u
JOIN vlog-ops v on u.usr_id = v.vlop_usr_id
JOIN users_gor g on u.usr_id = g.usrg_usr_id';
I believe I got the name of the fields right but if not just replace them with the correct ones.
Once you have the data fetched, you just loop through the results:
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
echo 'User name = ' . $row['u.usr_name'];
echo 'Org name = ' . $row['g.usrg_orgname'];
echo 'Job posting user id = ' . $row['v.vlop_usr_id'];
}

How to output a value of an other table from an indexed field in mySQL?

I have two MySQL tables
users with id, user_formOfAdress and serveral additional fields. Field user_formOfAdress contains the id of table formofadress
formofadress with id, formOfAdress_german and serveral additional fields, for example id 1 = Mr., id 2 = Mrs.
The record of the users table is identified by a Session variable.
To output not the id of the field user_formOfAdress but the value of the table formofadress.formOfAdress_german (for example Mr. or Mrs.) I have written this:
if(array_key_exists("id", $_SESSION) && $_SESSION['id']){
$uid = $_SESSION['id'];
$link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
$query = "
SELECT formofadress.ID AS formofadress_ID, formofadress.formOfAdress_german, users.ID, users.user_formOfAdress
FROM `formofadress`
LEFT JOIN users
ON formofadress.formofadress_ID = users.user_formOfAdress
WHERE `users.ID` = '".$uid."'
LIMIT 1
";
$result = mysqli_query($link, $query);
$record = mysqli_fetch_assoc($result);
$user_formOfAdress = $record['formOfAdress_german'];
}
"FROM formofadress" because I want to output the Mr. or Mrs. of this table, but I have to use also the users table because of the Session ID which is also the id of the users record ...
Not every record in the users table has a value in user_formOfdAdress (value 1, 2 or NULL) but every record in the formofadress table has a fixed value.
Error is:
Undefined variable: user_formOfAdress located in the last row
It's my first time to use JOINs and I'm unfortunately not able to solve this issue even after a long time of searching.
Correct code:
if(array_key_exists("id", $_SESSION) && $_SESSION['id']){
$uid = $_SESSION['id'];
$link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
$query = "
SELECT formofadress.ID, formofadress.formOfAdress_german, users.ID, users.user_formOfAdress
FROM formofadress
LEFT JOIN users
ON formofadress.ID = users.user_formOfAdress
WHERE users.ID = '".$uid."'
";
$result = mysqli_query($link, $query);
$record = mysqli_fetch_assoc($result);
$user_formOfAdress = $record['formOfAdress_german'];
}
There were problems with your SQL syntax. You used LEFT JOIN to join users to formofadress while users sometimes can't follow and you will possibly get a NULL value. I also cleaned up your other query syntax so it is more readable.
Also, check to see if the database is expecting an INT for $uid. If not then return the apostrophes ''.
if(array_key_exists("id", $_SESSION) && $_SESSION['id'])
{
$uid = $_SESSION['id'];
$link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
$query = "SELECT u.ID, u.user_formOfAdress, f.ID, f.formOfAdress_german,
FROM users u LEFT JOIN formofadress f ON f.formofadress_ID = u.user_formOfAdress
WHERE u.ID = ".$uid." LIMIT 1";
// if database is not expecting `INT` for `u.ID` then return the apostrophes ' '
$result = mysqli_query($link, $query);
// mysqli_fetch_assoc returns case sensitive keys
$record = mysqli_fetch_assoc($result);
$user_formOfAdress = $record['formOfAdress_german'];
}

SQL Inner Join fetch different results every time

my project stack in this issue bellow ,
i have 2 tables quiziz(filled by teacher) have 10 cols
id,dept,subject,question,ans1,ans2,ans3,ans4,correct_ans,techername
and students have 7 cols
id,username,password,usertype,dept,subject,answers
if the user logged in (successfully) in dborad.php he will see links every link will take him to different quiz in quiz.php page (using get method to get subject and dept for student) , in this quiz.php page he will see his quiz with 4 answers, my problem is the inner join not working , it suppose to fetch quiz for specific subject that he choose from dborad.php with some questions that is different from other students , but its not
dboard.php
if (isset($_SESSION['username'])) {
if ($_SESSION['usertype'] === "isstudent") {
$SQLgetParea = "SELECT * From students WHERE username = '$_SESSION[username]' ";
$SQLgetPareaResult = mysqli_query($conn,$SQLgetParea);
$SQLgetPareaRow = mysqli_fetch_array($SQLgetPareaResult,MYSQLI_NUM);
$getDept = $SQLgetPareaRow[4];
$sql2 = "SELECT students.*, quiziz.*
FROM students
INNER JOIN quiziz
ON students.dept=quiziz.dept
AND students.subject = quiziz.subject
WHERE students.dept = '".$getDept."'
";
$result2 = mysqli_query($conn,$sql2);
while ($row2 = mysqli_fetch_array($result2,MYSQLI_NUM)) {
echo "
<a href='quiz.php?subject=".$row2[5]."?dept=".$row2[4]."'>Enter Exam for ".$row2[5]."</a>
<br><br>";
}
}
}
quiz.php
if(isset($_SESSION['username'])){
if ($_SESSION['usertype'] === "isstudent") {
$conn->query("SET NAMES utf8");
$conn->query("SET CHARACTER SET utf8");
$SQLgetParea = "SELECT * From students ";
$SQLgetPareaResult = mysqli_query($conn,$SQLgetParea);
$SQLgetPareaRow = mysqli_fetch_array($SQLgetPareaResult,MYSQLI_ASSOC);
$getSub = $SQLgetPareaRow['subject'];
$sql2 = "SELECT quiziz.*, students.*
FROM quiziz
INNER JOIN students
ON quiziz.dept=students.dept
AND quiziz.subject = '".$getSub."' ";
$result2 = mysqli_query($conn,$sql2);
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "
<li>".$row2['question']."</li>
<li><div class='ruler'></div></li>
<li>".$row2['ans1']."</li>
<li>".$row2['ans2']."</li>
<li>".$row2['ans3']."</li>
<li>".$row2['ans4']."</li>
";
}
}
}
To fetch diferent question for every user, create a question table and assign an id for each question you want to show your users.
After that, you just need to create a vector with a random function to fetch those questions by id from your database. Change your column question from quizes to question_id which is a foreign key to your recently created table.

Running if statement based on the results of multiple selections

this is probably going to require something very basic but I can't find an answer to do this in the way I want it to work. What I have is a mysql table of articles and a table of favourites where if you like an article the information is logged. So I need to select all of the articles that I like from one table and then all of the articles from another and display them all as a feed, which I have done. The part I need help with is saying, if I like one of the posts, do not display the like button (Alternatively if I haven't yet liked it, show the button). Can anyone please point me in the right direction with this? Here's the code I have but it only shows the last one I Liked:
<?php
$check_like_sql = "SELECT * FROM posts WHERE type = 'Like' && poster = '$yourid'";
$check_like_res = mysqli_query($con, $check_like_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
while($likes = mysqli_fetch_array($check_like_res)){
$yourlike = $likes['media'];
}
}
?>
<?php
$get_posts_sql = "SELECT posts.*, members.username, members.avatar, fans.connection FROM posts LEFT JOIN members ON members.id = posts.poster LEFT JOIN fans ON fans.fan = '$yourid' WHERE posts.poster = fans.connection ORDER BY date DESC";
$get_posts_res = mysqli_query($con, $get_posts_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
while($posts = mysqli_fetch_assoc($get_posts_res)){
$postid = $posts['id'];
etc
etc
if($yourlike == $postid){
$likethis = "Unlike . ";
}
else if($posttype == "Like"){
$likethis = "";
}
else{
$likethis = "Like . ";
}
$post .= "";
}
}
?>
You can left outer join from your articles table to your liked_articles table.
select a.*,
case when la.liked_article_id is null then 'Not Liked Yet' else 'Already Liked' as LikedStatus
from articles a
left outer join liked_articles la
on a.id = la.article_id
and #userId = la.user_id;
This may not be syntactically correct for MYSQL but you get the gist.

Categories