How to make ranking system in PHP - php

I have problem. .
In my case i make championship. . You know, the winner is determined by the highest value. .
Example :
$sql = "SELECT bla bla bla FROM `user` ORDER BY `point` DESC";
$result = mysql_query($sql);
$i=0;
while($row = mysql_fetch_array($result)) {
$1++;
echo "rank ".$i." is ".$row['name']." with point ".$row['point'].";
}
It's will show
rank 1 is abc with point 10
rank 2 is def with point 9
rank 3 is ghi with point 8
rank 4 is jkl with point 7
rank 5 is mno with point 7
rank 6 is pqr with point 3
rank 7 is stu with point 1
The question. .
look the result. . rank 4 and rank 5 have same point. . how to make
they in same possition ??
how to auto detect title of champions like rank 1 is the big boss, rank 2 is a boss, rank 7 is worker ??
how to tell ex: you in possition 6 ??

$sql = "SELECT bla bla bla FROM `user` ORDER BY `point` DESC";
$result = mysql_query($sql);
if( !$result ){
echo 'SQL Query Failed';
}else{
$rank = 0;
$last_score = false;
$rows = 0;
while( $row = mysql_fetch_array( $result ) ){
$rows++;
if( $last_score!= $row['point'] ){
$last_score = $row['point'];
$rank = $rows;
}
echo "rank ".$rank." is ".$row['name']." with point ".$row['point'].";
}
}
This code will also adjust the rankings properly - such as:
rank 1 is Adam Aarons with point 100
rank 2 is Barry Blue with point 90
rank 2 is Betty Boop with point 90
rank 4 is Charlie Chaplin with point 80
Note that there is no "rank 3", as "Charlie Chaplin" is actually the fourth-highest scorer.
If you do not want this behaviour, then simply replace $rank = $rows; with $rank++;

ad 1. group by point, add #curRow := \#curRow + 1 AS row_numbe to select the current row number which will be the rank position
ad 2. left join title_dict on row_number = title_dict.id
where title dictionary is table with title_id and title name dictionary
ad 3. select from your select on id = your id

Actually, a very easy way to order players with same amount of points is to add a column "points_last_update" containing the timestamp of the last update to the "points" column. Then you just add the "points_last_update" to the ORDER BY in your sql statement. That way, the player who has reached the points the first gets the higher ranking, wich is logic.

Save the last score and make sure it does not equal the current score before you increment.
$sql = "SELECT bla bla bla FROM `user` ORDER BY `point` DESC";
$result = mysql_query($sql);
$i=0;
$lastscore = NULL;
while($row = mysql_fetch_array($result)) {
if ($lastscore != $row['point']){
$i++;
}
echo "rank ".$i." is ".$row['name']." with point ".$row['point'];
$lastscore = $row['point'];
}

Related

Mysql count column issue

I can't figure-it out how to count all these dancers columns and echo with total of all dancers
id | dancer1 | dancer2 | dancer3 | dancer4, and so on..
---------------------------------------
1 alex michael dalm name
2 clare rose test
I have this for the start but is not working:
$counter = mysql_query("SELECT COUNT(*) AS id FROM table");
$num = mysql_fetch_array($counter);
$dancers = $num["id"];
echo "Total dancers: $dancers";
Any help is appreciated. Thanks!
Try this:
$counter = mysql_query("SELECT * FROM table");
$dancers = 0;
while($rows = mysql_fetch_array($counter)){
for($i = 1; $i <= 24; $i++){
$dan_id = 'dancer'.$i;
if($rows[$dan_id] != "" || $rows[$dan_id] != null )
$dancers++;
}
}
echo "Total dancers:". $dancers;
Note: Never design your database table like this.
I would actually save your dancers in a different (easier) way... For examle:
ID NAME SURNAME PHONE ....
1 Anna Brickstone 0975 ...
2 Jef Damen 0754 ...
That way you could use the following code to count tables:
$dancersCount="0";
$sql = "SELECT * FROM dancers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dancersCount++;
}
} else {
echo "No dancers..";
}
echo"$dancersCount";
The counter will count +1 each time it finds a row (dancer)
If you really want to do it that way...
Then I don't really think there's an easy way to fix this... You will probably need to check how many columns you have in your database but that's not something i can help you with...
You need to change your table structure:
id | dancerNumber | name
1 1 alex
2 1 clare
3 2 michael
4 2 rose
5 3 dalm
6 3 test
7 4 name
8 4 dana
SELECT COUNT(*) AS id FROM table will return 8 dancers. If this is what you were looking for?
if you want to keep your structure then you need to do the following sql query
SELECT dancer1,
dancer2,
dancer3,
(CASE WHEN (dancer1 <> "" AND dancer1 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer2 <> "" AND dancer2 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer3 <> "" AND dancer3 IS NOT NULL) THEN 1 ELSE 0 END) AS COUNTER
FROM table
This will count all the non empty and non null columns and add a counter at the end of the table. this counter will then contain the number of dancers with your structure.
Full answer with your php code
$query = 'SELECT (CASE WHEN (dancer1 <> "" AND dancer1 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer2 <> "" AND dancer2 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer3 <> "" AND dancer3 IS NOT NULL) THEN 1 ELSE 0 END) AS COUNTER
FROM table'
$counter = mysql_query($query);
$num = mysql_fetch_array($counter);
$dancers = $num["COUNTER"];
echo "Total dancers: $dancers";

How get all two array values using select query at once

I want to get Name and corresponding Score at the latest time. So I tried:
$queryObj = mysql_query("SELECT `Name`,`Score` from `Table`.`Score` where `Date` = ( SELECT max(`Date`) from `Table`.`Score`) and `Name`<>'' ");
then get value from it by:
while( $obj = mysql_fetch_object( $queryObj ) ) {
$data = array();
$data['Name'] = $obj->Name;
$data['Score'] = $obj->Score;
$searches[] = $data;
}
But when I print :
print_r(array_values($searches));
the first value is missing in the array, so that won't be the right way.
I also tried:
$row = mysql_fetch_assoc($queryObj);
for ($i = 0; $i <3; $i++)
print( $row['Name'][$i]." Score: ".$row['Score'][$i]."<br />\n");
But it won't give me the right results also. How do I get the value from that query? (the query is correct, I tested it). Any body has suggestion ?
Edit: I add my sample data here:
Name Score Date
abc 3 2013-08-29 10:11:47
abc 2 2013-08-29 09:39:23
abc 1 2013-08-28 10:22:28
jane 2 2013-08-29 09:39:23
2013-08-29 10:08:36
jane 1 2013-08-29 10:11:47
tarzan 1 2013-08-29 10:11:47
Note: Yes, there is some blank values.
My expected result would be:
abc score 3
jane score 1
tarzan score 1
Ok, so after you have updated your question and provided what you expect, your query should look like this:
SELECT t1.Name, t1.Score
FROM Table.Score t1
INNER JOIN
(
SELECT max(Date) MaxDate, Name, Score
FROM Table.Score
WHERE Name <> ''
GROUP BY Name
) t2
ON t1.Name = t2.Name AND t1.Date = t2.MaxDate
This will give you pairs of Name and Score for each Name with Score based on his latest Date (1 row per Name).
So replace your original query with mine in this line:
$queryObj = mysql_query(" ... ");
Then:
$rows = array();
while($row = mysql_fetch_assoc($queryObj)) {
$rows[$row['Name']] = $row['Score'];
}
And you can nicely foreach it in the exact way you wanted in your last comment:
foreach($rows as $name => $score) {
echo $name . ' - ' . $score . "\n";
}

Displaying 11 consecutive rows from a query where middle row contains a variable

On my web page, there is a variable called $submission. I would like to display exactly 11 rows from the query below: the row where $submission equals $row["title"], 5 rows above it, and 5 rows below it. All ranked by points descending.
How can I do this?
$sqlStr = "SELECT title, points, submissionid
FROM submission
ORDER BY points DESC";
$result = mysql_query($sqlStr);
$arr = array();
$count=1;
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr >';
echo '<td>'.$count++.'.</td>';
echo '<td class="sitename1">'.$row["title"].'</td>';
echo '<td class="sitename2"><div class="pointlink2">'.number_format($row["points"]).'</div></td>';
echo '</tr>';
}
echo "</table>";
This is a little tricky if several rows share the same value for "points":
points | title | submissionid
------ + ----- + ------------
...
50 | 'foo' | ABCD01234 <-- If (50, 'foo') is the "midpoint" record,
50 | 'bar' | EF7654321 <-- does (50, 'bar') come before or after?
...
In this case, we need to impose an order. For convenience we're going to order by "points" descending and then "title" descending.
Assuming your "midpoint record" has a points value of '#points' and a title '#title', we'll say the records that come "before" the midpoint are those records whose (points, title) > (#points, #title). Similarly, those records "after" the midpoint have their (points, title) < (#points, #title).
Putting it together, we have:
-- First, initialize user variables with the points and
-- title of our midpoint (including midpoint)
--
SELECT #title := title,
#points := points
FROM submission
WHERE title = ? -- bind your $submission variable bere
LIMIT 1;
-- Now, select six records greater than or equal to our
-- midpoint.
--
SELECT title, points, submissionid
FROM ( SELECT title, points, submissionid
FROM submission
WHERE (points, title) >= (#points, #title)
ORDER BY points ASC, title ASC
LIMIT 6) gte
-- and UNION those records with five records less than
-- our midpoint
--
UNION
SELECT title, points, submissionid
FROM ( SELECT title, points, submissionid
FROM submission
WHERE (points, title) < (#points, #title)
ORDER BY points DESC, title DESC
LIMIT 5) lt
-- Finally sort the result set
--
ORDER BY points DESC, title DESC
You need to use a UNION
(
SELECT title, points, submissionid
FROM submission
WHERE points < (SELECT points FROM submission WHERE title = <row_title> LIMIT 1)
ORDER BY points DESC LIMIT 5
)UNION(
SELECT title, points, submissionid
FROM submission
WHERE points > (SELECT points FROM submission WHERE title = <row_title> LIMIT 1)
ORDER BY points ASC LIMIT 5
) ORDER BY points DESC
I haven't tested it, but this is the gist of it. You will get 10 records (or less), you'll have to figure out in PHP which records should go above your $submission and which below, since if you get 9 records you wont know if 4 are higher, or 5.
Alternatively you could just do it with 2 queries of course >_>
SELECT
title, points, submissionid
FROM
submission
WHERE
ROWID >= (SELECT ROWID FROM submission WHERE ..... ORDER BY points DESC) - 5
AND
ROWID <= (SELECT ROWID FROM submission WHERE ..... ORDER BY points DESC) + 5
ORDER BY
points DESC
I think that the following might do what you are asking. It will run the query and start reading records from the result set up until it finds the first occurrence of a record with a title that equals the $submission variable that you have (if I understand it correctly the primary key of your table is the submissionid and the title is a simple field - i.e. you do not have a unique key on it, so there may be more than one records with the same title).
After it finds that first record it will read 5 more records and stop. Then it will provide you with the part of the array of records that you wish to print and finally it will print it.
$sqlStr = "SELECT title, points, submissionid FROM submission ORDER BY points DESC";
$result = mysql_query($sqlStr);
$count = 1;
$found = false;
$continue = true;
$records = array();
$row = mysql_fetch_array($result);
while ($row !== false && $continue === true)
{
if($found == false && $row['title'] == $submission)
{
$found = true;
}
elseif($found == true && $count < 6)
{
$count++;
}
elseif($found == true && $count >= 6)
{
$continue = false;
}
$records[] = $row;
$row = mysql_fetch_array($result);
}
if($found === true)
{
if(array_count($records) > 11)
$records = array_splice($records, -11);
}
else
{
$records = array();
}
echo "<table class=\"samplesrec\">";
for($i = 1; $i <= count($records); $i++)
{
echo '<tr >';
echo '<td>'.$i.'.</td>';
echo '<td class="sitename1">'.$records[$i]["title"].'</td>';
echo '<td class="sitename2"><div class="pointlink2">'.number_format($records[$i]["points"]).'</div></td>';
echo '</tr>';
}
echo "</table>";
If there are no duplicate "title" values and if there are no duplicate "points"
SELECT title, points, submissionid
FROM submission
WHERE points <=
( SELECT points
FROM submission
WHERE points >=
( SELECT points
FROM submission
WHERE title = #row_title
)
ORDER BY points ASC
LIMIT 1 OFFSET 5
)
ORDER BY points DESC
LIMIT 11 OFFSET 0
I think its simplest to just do 2 queries against the database, but you could do it in PHP:
$entryFound = false;
$counter = 0;
$priorEntries = array();
while ($row = mysql_fetch_array($result)) {
$rowHtml = '<tr >';
$rowHtml .= '<td>'.$count++.'.</td>';
$rowHtml .= '<td class="sitename1">'.$row["title"].'</td>';
$rowHtml .= '<td class="sitename2"><div class="pointlink2">'.number_format($row["points"]).'</div></td>';
$rowHtml .= '</tr>';
if ($entryFound) {
if ($counter < 5) {
$counter++;
echo $rowHtml;
}
} else {
array_unshift($priorEntries, $rowHtml);
if (strcmp($row["title"], $submission) == 0) {
echo implode(array_reverse($priorEntries));
$entryFound = true;
}
array_splice($priorEntries, 5);
}
}
If the intention is to use single query and table is small and performance doesn't matters then use
SELECT b.title, b.points FROM (
SELECT #rank1 := #rank1 + 1 as slno, temp1.* FROM (
SELECT s1.title, s1.points, COUNT(s2.title) rank
FROM submission s1
JOIN submission s2 ON s1.points <= s2.points
GROUP BY s1.title, s1.points
ORDER BY rank, s1.title ASC
) as temp1
JOIN( SELECT #rank1 := 0 ) AS init
ORDER BY slno
) a
LEFT JOIN (
SELECT #rank2 := #rank2 + 1 as slno, temp1.* FROM (
SELECT s1.title, s1.points, COUNT(s2.title) rank
FROM submission s1
JOIN submission s2 ON s1.points <= s2.points
GROUP BY s1.title, s1.points
ORDER BY rank, s1.title ASC
) as temp1
JOIN( SELECT #rank2 := 0 ) AS init
ORDER BY slno
) b ON a.slno BETWEEN b.slno - 5 AND b.slno + 5
WHERE a.title = <row_title>;
This will return the row selected by and upto 5 above(if present) and upto 5 below(if present).
However, it is highly suggested to use temporary table to store ranks and the use it to display ranks.
More details about above query #
http://www.artfulsoftware.com/infotree/queries.php?&bw=1280#460
More details about optimized method #
http://onlamp.com/pub/a/mysql/2007/03/01/optimize-mysql-rank-data.html

Drupal-based SQL query through PHP: count t1 join t2

I have two tables like so:
table {node}
`nid`, `uid`, `type`
1 1 basketball
2 1 basketball
3 1 football
4 2 football
5 2 basketball
table {strato_ticket}
`tid`, `author_uid`, `purpose`, `active`
1 1 'Ticket to a basketball game' TRUE
2 1 'Ticket to a football game' TRUE
3 2 'Ticket to a football game' FALSE
I'd like to generate a report that counts the number of each kind of node, and then counts the number of active tickets that each user has associated with that kind of node.
My solution uses a combination of SQL and PHP: I have a PHP loop for each kind of node that I'm interested in, which simplifies the SQL query, and translates from 'type' to 'purpose', eg
$node_types = array('basketball', 'football');
foreach($node_types as $node){
switch($type){
case 'basketball':
$purpose = array('Ticket to a basketball node');
break;
case 'football':
$purpose = array('Ticket to a football game');
break;
}
$where = " WHERE ({strato_ticket}.`purpose` = '"
.implode("' OR {strato_ticket}.`purpose` = '",$purpose)."')";
Finally I have the trouble spot, the SQL query. When I was just counting nodes owned by each user, it worked fine:
$query = "
SELECT uid, count( * ) AS nodes_owned
FROM {node} WHERE `type` = '$type'
GROUP BY uid ORDER BY nodes_owned DESC
";
$query = db_query($query);
output:
Now displaying info for basketball.
uid nodes_owned
1 2
2 1
Now displaying info for football.
uid nodes_owned
1 1
2 1
But now that I need to query against another table, strato_ticket, things get complicated, and my query is returning FALSE without throwing an error (I think).
$query = "
SELECT count(*) as tickets
FROM {strato_ticket} INNER JOIN (
SELECT node.uid, count( * ) AS nodes_owned
FROM {node} WHERE `type` = '$type'
GROUP BY uid
) AS {nodecount}
ON {strato_ticket}.`author_uid` = {nodecount}.`uid`
$where
GROUP BY nodecount.uid ORDER BY nodecount.nodes_owned DESC
";
$query = db_query($query);
I'm not very good with SQL and I'm not quite sure how it's broken. Could use a little help?
Ideally would like to see
uid nodes_owned tickets
//basketball
1 2 1
2 1 0
//football
1 1 1
2 1 0
Aside from the placeholders, which I can get to later, I think this solves it.
$form = array();
$node_types = array('basketball','football');
// if($user->uid == 1){
$form[$type][] = array('#value'=>"Showing how many of each node type each user owns.".'<br/>');
foreach($node_types as $type){
// Count the number of nodes each user owns of $type.
$form[$type][] = array('#value'=>"Now displaying info for $type".'s. <br/>');
switch($type){
case 'basketball':
$purpose = array('ticket to a basketball game', 'basketball');
break;
case 'football':
$purpose = array('ticket to a football game');
break;
}
$purpose = implode("', '", $purpose);
//#todo : Make a temporary table to query against so I'm not hitting node table multiple times.
$ticketquery = "
SELECT author_uid, purpose, COUNT( * ) AS invitees_accepted
FROM {strato_ticket}
WHERE purpose IN ('$purpose')
GROUP BY author_uid, `purpose`
";
$nodequery = "
SELECT node.uid, count( * ) AS nodes_owned, type
FROM {node}
WHERE `type` IN ('$type')
GROUP BY uid, type";
$query = "
SELECT * FROM
($nodequery) AS nt
JOIN
($ticketquery) AS tt
ON nt.uid = tt.author_uid
GROUP BY nt.uid ORDER BY nt.nodes_owned DESC
";
drupal_set_message('Query is <br/>'.$query);
//return;
$query = db_query($query);
$first = true;
while ($rec = db_fetch_object($query)){
if($first){
$form[$type][] = array('#value'=>"And the winner is: ".print_r($rec, true).'<br/>');
$first = false;
}
else {
$form[$type][] = array('#value'=>print_r($rec, true).'<br/>');
}
}
// }
}

mysql select and where over several tables (very tricky)

I have 4 Tables (listed bellow) and need:
get last 10 Chats from Room 3 without banned users
show nickname for fromuserid
HIDE Users $userid dont like to see table "HIDE"
Table 1 "chats"
ID(autoinc) fromuserid roomid text
1 23 3 bla
2 14 1 bla
3 11 3 bal
Table 2 "user" /shorted/
ID(autoinc) nickname banned
1 chris 0
2 paul 1 // 1 = banned
Table 3 "hide"
ID(autoinc) orguser hideuser
1 12 3
2 33 12
Right now i solved it with PHP Routine, but I have to go through EACH result and make always a new query, that needs too long;
$userid = 1; // actual user
// List all chats and show userid as nickname
$sql_com = "SELECT user.id, user.nickname, chats.text, chats.id ".
" FROM chats, user".
" WHERE ".
" chats.fromuserid = user.id ".
" AND chats.roomid = 3 ".
" AND user.banned != 1 ".
" ORDER BY chats.id DESC";
$result = mysql_query ($sql_com);
$count = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$dontshow = false;
// Filter : dont show users $userid dont like to see (table "hide")
$sql_com2 = "SELECT id from hide WHERE ( (orguser = ".$userid.") AND (hideuser = ".$row[0].") ) ";
if ($result2 = mysql_query ($sql_com2))
{
if (mysql_num_rows($result2) > 0) $dontshow = true;
}
// Output
if ($dontshow == false)
{
$count++;
echo "Nickname: ".$row[1]." Text: ".$row[2];
}
if ($count > 10) break;
}
Btw. I made already some improvments, so the actual question may not fit with all answers (thanks for your help till now)
Finaly its now just about to integrate the filter "dont show people listed in table "hide" for my actual user".
I think you need something along these general lines. I've done it slightly different from your question. Instead of getting the top 10 then removing records. It gets the top 10 records which would not be hidden.
SELECT c.ID, c.fromuserid, c.roomid, c.text, u.nickname
FROM chats c
JOIN user u ON c.fromuserid = u.id
where c.roomid = 3 AND user.banned = 0
AND NOT EXISTS(
SELECT * FROM hide h
WHERE h.hideuser = c.fromuserid
AND orguser = $userid)
ORDER BY c.ID DESC
LIMIT 0,10
Not tested but it would be something like:
$sql_com = "SELECT us.id, us.nickname, ch.text, ch.id ".
" FROM chats ch, ".
" user us, ".
" hide hi, ".
" banned ba, ".
" WHERE ".
" us.id != hi.hideuser ".
" us.id != ba.user ".
" us.id = ch.fromuserid ".
" AND ch.roomid = 3 ".
" ORDER BY ch.id DESC LIMIT 0,10";
Although I can't immediately find a simple way to answer your question as-is, I can point you in the right direction:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
Using subqueries should enable you to go and select from both blocked and hidden tables, and using those in your original query.

Categories