klaroen Posted June 18, 2006 Share Posted June 18, 2006 Oke this is the request:Is there a query function or code to do this:I have a database, and there are kind of diffrent number in it: 500 , 360 , 578, 9978, 7589, 125, 154, etc...Now when I select one of those, can I ask with a code with place he has from the top?So: I select the number 578 for example, can it tell me what 'rank' it has? so it should be 3th...Is there a code for that? Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 18, 2006 Share Posted June 18, 2006 Yes. But we'll need a great deal more information to tell you how. What are those numbers? What is 'rank'. What's the database table structure? Quote Link to comment Share on other sites More sharing options...
klaroen Posted June 18, 2006 Author Share Posted June 18, 2006 Imagine: I have a list of products and they all have their price. [code]<?php $player=$_SESSION['player']; $userstats="SELECT * from koc_products where username='$player'"; $userstats2=mysql_query($userstats) or die("Error while connecting to database! Please report this to an admin!"); $stat=mysql_fetch_array($userstats2); $ranking=[b]thecodeineed[/b]($stat[price]);print(" The product is our [b]?[/b] most expansive product!");[/code]So as you see the code I need should tell me how muchst he is in the list of most expensive products...Is that understandable?(ps: rank is the number of how high he is ( how muschest)(is that spelled like that?? ) Quote Link to comment Share on other sites More sharing options...
.josh Posted June 19, 2006 Share Posted June 19, 2006 if you are simply looking for the highest number in the list, you would do like this (assuming "price" is the column name) :[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']select[/span] price [color=green]from[/color] [color=orange]tablename[/color] [color=green]order by[/color] price [color=green]desc[/color] limit 1[!--sql2--][/div][!--sql3--]so if you had a column price and it had the following info in it:price===454536432583543294534984that query would return 34984 Quote Link to comment Share on other sites More sharing options...
klaroen Posted June 19, 2006 Author Share Posted June 19, 2006 yeah, that I know :p... I'll give another example:In a online game every player has his troops and has his attack power and defence power. Now, if I select the defence from the player that is logged in with query can I get the rank of his defence with a function?So his defence is like 5009 and there is only one player better with 6582 and a lot players are lower... Now, can I ask my database to count out for me that he's defence is the second highest?Or for other players how high their defence is (counted in places)...(get it?) Quote Link to comment Share on other sites More sharing options...
.josh Posted June 19, 2006 Share Posted June 19, 2006 i don't know if mysql has a built in function for that or not, but i suppose you could write a script in php for that. and even still, there has GOT to be a better way than this, but this is my ghetto code:[code]$username = 'klaroen';$sql = "select * from table order by defense desc";$result = mysql_query($sql);while ($info = mysql_fetch_array($result)) { $playerinfo[] = $info;}$numplayers = count($playerinfo);$pos = 1;while ($playerinfo[$pos]['playername'] != $username) { $pos++;}echo "Name: " . $username . "<br>";echo "Defense: " . $playerinfo[$pos]['defense'] . "<br>";echo "Defense rank: " . $pos;[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.