Showing the previous 5 and next 4 results from a specific result in a query

Hey guys, first of all thanks for even checking this out and contributing to /r/phphelp, there’s a lot of people like myself out there who would get nowhere without the helping hand you all provide.
Anyway, on to my problem! I have a database holding two values in a table, names & scores. I have a piece of php called addscore.php and a piece called display.php. Display.php selects the 10 highest scores and returns them as a table.

Here’s what the display.php looks like:

[php]

<?php // Send variables for the MySQL database class. $database = mysql_connect(removed the stuff that goes here) or die('Could not connect: ' . mysql_error()); mysql_select_db('runner_scores') or die('Could not select database'); $query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 10"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); $num_results = mysql_num_rows($result); for($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo $i + 1 . ". \t \t \t \t" . $row['name'] . "\t \t \t" . $row['score'] . "m \n"; } ?>

[/php]

What I want to do is tweak this slightly so that putting
mywebsitehere.com’/display.php?usernamehere
will result in a table showing the highest score attached to this username, along with the 4 scores that are above it, and the 5 scores that are below it. Right now there can be multiple instances of the same name, so it’ll need to find the highest one only and then display the 10 results I’ve mentioned. Now that I think about it, I’ll also need to have it show where the player’s score is, rather than just showing them as no.5 (I’m guessing to do this I’ll have to retrieve the whole table first them find where my specific input occurs? I know very little :p)
I’ve done everything I have so far from tutorials, and I only have a really really basic understanding of php and sql, so complicated explanations might go over my head but any help is massively appreciated!
Thanks so much guys

LIMIT and OFFSET

$query = “SELECT * FROM scores ORDER by score DESC LIMIT 10 OFFSET 10”;


http://www.petefreitag.com/item/451.cfm

Sponsor our Newsletter | Privacy Policy | Terms of Service