I'm making a ranking list based on user earned,
I made the top 10 ranking when it was displayed
but i need to show the ranking from the user but he doesn't have the top 10 ranking
example:
rank1: Jhon
rank2: Jhon 2
rank3: Jhon 3
rank4: Jhon 4
rank5: Jhon 5
rank6: Jhon 6
rank7: Jhon 7
rank8: Jhon 8
rank9: Jhon 9
rank10: Jhon 10
rank465: Jhon 465 (based on user login)
I need Jhon 465 and his rank displayed in the ranking list
here the php sql code
i use smarty framework to show myphp
/** PHP SQL CODE **/
$q = $db->query("SELECT user_id,earned," . $query24h . ", " . $query7d . ", " . $query30d . " FROM bonusads_stats ORDER BY " . $orderby . " DESC LIMIT 10");
while ($r = $db->fetch_array($q)) {
$r['username'] = $db->fetchOne("SELECT username FROM members WHERE id=" . $r['user_id']);
$r['country'] = $db->fetchOne("SELECT country FROM members WHERE id=" . $r['user_id']);
$r['type'] = $db->fetchOne("SELECT type FROM members WHERE id=" . $r['user_id']);
$flag = $bacontest_flags[$r['country']];
$r['country'] = strtolower($flag);
$r['type'] = $membership_name[$r['type']];
$top10[] = $r;
$r['earned'] = $db->fetchOne("SELECT earned FROM bonusads_stats");
}
/** SHOW PHP CODE (USE SMARTY) **/
{foreach from=$top10 item=foo key=k}
<tr style="text-align:center">
<td><span class="rc-position">{$k+1}</span></td>
<td>{$foo.type}</td>
<td> <img src="images/forum/flags/{$foo.country}.png" style="margin-right : 8px" title="{$item.member.country}" />{$foo.username}</td>
<td>{$foo.last24hours}</td>
<td>${$foo.earned}</td>
<td>${$foo.earned * 10}</td>
</tr>
{/foreach}
how to display the ranking of users login but he is not in the top 10 ranks
The best solution is to have two queries, one for the top 10 and one for user score.
For example, make a class Score. Which one will have two methods.
One for the top 10 and one for a single user score.
Let's say both methods return arrays. So then you just need to marge arrays and print out them.
Just add the desire user at the end. If user is already on the top 10. UNION ALL remove the duplicated. You include user_id to avoid cases where two user had same name.
SELECT user_id, user
FROM YourTable
ORDER BY orderField
LIMIT 10
UNION ALL
SELECT user_id, user
FROM YourTable
WHERE user_id = #user_id
Related
I have 6 records 3 of which has identical School and I want to get the result of counting how many school there are inside my database but it only returns the value of 2
$tblnum1 = "SELECT COUNT(*) AS ttldata FROM engoralgrade3 WHERE Years = '$yrr' GROUP BY School";
$tblnum = mysqli_query($conn, $tblnum1);
$tblnm = mysqli_fetch_array($tblnum);
echo $tblnm['ttldata'];//input should be 3
This what my data base looked like
I have checked your table, every school do have 2 rows.
maybe u want to count how many distinct school there are, so change the sql to:
select count(distinct School )from engoralgrade3
or u want to distinct the school name, try :
select distinct School from engoralgrade3
You can try this query it will work
$tblnum1 = "SELECT * FROM engoralgrade3 WHERE Years = '$yrr' GROUP BY School";
$tblnum = mysqli_query($conn, $tblnum1);
$tblnm = mysqli_num_rows($tblnum);
echo $tblnm ;
it may be the var $yrr is not identical for all six records in database which cause make returnred value is 2 not 3 .
need a help with a PHP/MySQL issue. I have a table named bids and two column named buyer and tagged both using int.
buyer
--------------
8
5
2
tagged
--------------
5
4
1
I'm trying to detect multiple same entry number. I want if a same number appears on both of the column it shouldnt display on the menu list anymore, hope yo understand.
Any tip?
code below
$query = "SELECT b.auction, b.buyer, b.bid, b.bidwhen, b.quantity, b.willwin, b.tagged, b.balance, u.nick, u.rate_sum FROM " . $DBPrefix . "bids b
LEFT JOIN " . $DBPrefix . "users u ON (u.id = b.bidder) WHERE b.auction = :auc_id
ORDER BY b.bid asc, b.quantity DESC, b.willwin asc"; $params = array(); $params[] = array(':auc_id', $id, 'int');
I have two tables and i want to echo the total call count once each user logins:
Login
Firstname | Lastname | Login ID
------------------------------
Tyler | Durden | 3
Call Count
Name | Call Count | Open | GrandTotal
------------------------------
Tyler Durden| 100 | 33 | 133
i tried:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name=".$_SESSION['firstname']. ' ' .$_SESSION['lastname']." ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
But its not working, i think i have to join the the two tables to get it to work. What do you think?
Assuming your Call Count table is actually called csvdata, you'll want to format your SQL request string a bit by adding single quotes around the WHERE name = part.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name='".$_SESSION['firstname']. ' ' .$_SESSION['lastname']."' ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Call Count'].' Call Count';
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
Good practice would require that you use primary keys to facilitate joins between tables and make sure two users with the same name can be differenciated.
In that case you may want to consider replacing the Name column in your Call Count table for your loginID. This way you could get your name from the Login table (as shown below). Also, as bad as it is to have duplicated data like your user's name in both tables, you do not need your GrandTotal column since you can easily get the sum of CallCount and Open to get the exact same number. In the end, your query should look more like this (assuming your tables are called Login and CallCount).
<?php
$result = mysqli_query($mysqli, "SELECT l.FirstName, l.LastName, cc.CallCount, cc.Open, (cc.CallCount + cc.Open) AS GrandTotal FROM Login AS l JOIN CallCount AS cc ON l.LoginID = cc.LoginID WHERE l.FirstName LIKE \"".$_SESSION['firstname']."\" AND l.LastName LIKE \"".$_SESSION['lastname']."\"");
// ...
?>
I have a page that has a column "order". When I add a new item the order should be 1 + max of the item. But if I delete an item between these values like i have 8 9 10 , if I delete item with 9 the new item will have 11 value and I will have 8 10 11.But I want 8 9 10. How can I do this in php? should I create a function? It works to add a new item with the max+1, but if I delete is not good.
My code:
$queryMaxOrd = mysql_query("select max(ord) from article where user_id=" .$_SESSION['userId']);
$row = mysql_fetch_row($queryMaxOrd);
$valueMaxOrd = $row[0] + 1;
$_SESSION['a'] = $valueMaxOrd;
and the insert query....
Any ideas?
First thing to consider is removing mysql_* and start using mysqli_* or pdo. Your code is vulnerable to sql injections.
With your code structure you have you adding each order as a new record to your table, so when you have 5 orders with one users your table will look like this.
user: Adiiia | order: 5
user: Adiiia | order: 3
user: Adiiia | order: 7
user: Adiiia | order: 2
user: Adiiia | order: 9
when you query the database you are saying to mysql: get me the maximum number of the record i have with user: Adiiia. The result should be 9
If you want to count the sum of the records you should use sum
select sum(ord) from article where user_id='".$_SESSION['userId']."'
The result should be 26
The best thing you can do is by creating a new table orders_sum
The structure should like like this.
Total_orders | User
when a user have a new order you can update the table by adding one or else if have a order removed then removing minus one order.
Something like this.
Total_orders | User
5 | Adiiia
When you want to update:
select Total_orders from orders_sum where User='Adiiia'
Find how many orders the user have by fetching the table.
$orders = $row['Total_orders'];
$plus_orders = $row['Total_orders'] + 1;
update orders_sum set Total_orders='".$plus_orders."' where user='Adiiia'
When you want to delete:
select Total_orders from orders_sum where User='Adiiia'
Find how many orders the user have by fetching the table.
$orders = $row['Total_orders'];
$minus_orders = $row['Total_orders'] - 1;
update orders_sum set Total_orders='".$minus_orders."' where user='Adiiia'
Lets say article table having primary key article_id then after delete any article for user_id. Fetch all the article_id for the user_id. Then update the order for all the article_id for that user.
$queryArticle = mysql_query("select article_id from article where user_id=" .$_SESSION['userId']." order by article_id asc");
$allArticle = [];
while($rowArticle = mysql_fetch_assoc($queryArticle)){
$allArticle[] = $rowArticle['article_id'];
}
$query = "UPDATE article SET ord = (CASE order ";
foreach($allArticle as $order => $article_id) {
$query .= " WHEN {$article_id} THEN {$order}";
}
$query .= " END CASE) WHERE article_id IN (" . implode(",", $allArticle) . ")";
This question already has answers here:
Rank function in MySQL
(13 answers)
Closed 8 years ago.
I have a text based mafia game and I am selected some GameRecords. The game records are all defined in the "users" table. For this example I am using "totalcrimes". I need to select all the rows from the users table and order it by totalcrimes and then find out which row each specific user is that is viewing the page.
If I was the user that was "ranked" 30th it would echo "30". The code I use to find the top 5 is here however I need to expand on it:
<?php
$i = 0;
$FindCrimes = mysql_query("SELECT * FROM players WHERE status='Alive' AND robot = 0 ORDER BY `totalcrimes` DESC LIMIT 5");
while($Row = mysql_fetch_assoc($FindCrimes)){
$Username = $Row['playername'];
$TotalCrimes = number_format($Row['totalcrimes']);
$i++;
echo "
<tr>
<td bgcolor='#111111' width='5%'>$i</td>
<td bgcolor='#111111' width='50%'><a href='viewplayer?playerid=$Username'>$Username</a></td>
<td bgcolor='#333333' width='45%'>$TotalCrimes</a></td></td>
</tr>
";
}
?>
I am going to assume that you already have a variable set to hold the current users ID number and total crimes, so in this case I will use $user as my variable.
Change yours to fit.
Now, I see 2 instances in which you could mean as your post wasn't very specific, so I will address both.
To show the number at the top of the page, you would use something like;
<?php
$sql = "SELECT * FROM `players` WHERE `totalcrimes` > '{$user['totalcrimes']}'";
$run = mysql_query($sql);
$rank = mysql_num_rows($run) + 1;
echo 'Your rank: ' . $rank;
Other than that, I see it's possibly being used to highlight your row, so something like this would suffice;
<?php
$i = 0;
$FindCrimes = mysql_query("SELECT * FROM players WHERE status='Alive' AND robot = 0 ORDER BY `totalcrimes` DESC LIMIT 5");
while($Row = mysql_fetch_assoc($FindCrimes))
{
$Username = $Row['playername'];
$TotalCrimes = number_format($Row['totalcrimes']);
$i++;
$primary = '#111111';
$secondary = '#333333';
if ($Row['id'] == $user['id'])
{
$primary = '#222222';
$secondary = '#444444';
}
echo "<tr>
<td bgcolor='$primary' width='5%'>$i</td>
<td bgcolor='$primary' width='50%'><a href='viewplayer?playerid=$Username'>$Username</a></td>
<td bgcolor='$secondary' width='45%'>$TotalCrimes</a></td></td>
</tr>";
}
If neither of those give your requirements, please comment and I'll edit to suit.
edit: I've worked on games for a few years - care to share the link to yours?
This can do the trick
SELECT COUNT(*)+1 as rank
FROM users
WHERE totalcrimes > (SELECT totalcrimes
FROM users
WHERE user_id='12345' AND status='Alive' AND robot='0');
So it counts all rows with greater totalcrimes than selected user (in this example I have used user_id column and some id 12345), than adds 1 on that sum and returns as rank value.
Course, modify WHERE clause inside the brackets to make it work for you.
I assumed that table name is users and user's id is integer user_id.
Test preview (Navicat Premium):
What this query does? It returns number of selected rows + 1 as rank column, from the table users where totalcrimes is greater than totalcrimes of some user. That user's totalcrimes is selected by another query (by its user_id). If you have multiple users with same totalcrimes value, this query will return same rank for all of them.