Simple Ranking Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Simple Ranking Script

Post by Trahb »

I'm in the process of creating a ranking script for the users of my website, but am running in to problems. All I need is one that shows the first x amount of users, then has links at the bottom for x - x+50, etc, etc, only for the number of existing users. For example, if there are 263 users, I don't want it to have links for 300-350, only 1-50, 50-100, 100-150, 150-200, 200-250, 250-300.
My current code is going to have to be switched up a LOT I think, as right now, the way I see it, for each new page it will start the ranks back at 0 and go through x -- but here it goes.

Code: Select all

function doRanks() {
		$rank = 0;
		//$total = 0;  Plan was to have total be the total number of users already shown
		$query = mysql_query("SELECT * FROM `table` ORDER BY `number` DESC");
		$num_sites = mysql_num_rows($query);
		while($row = mysql_fetch_array($query)){
			$rank++;
			if($rank <= 20) {
				echo "--User info--";
				if ($rank == 12 && $rank != 3)
				{
					echo "<div class=\"box\">Advertisement #2!</div>";
				}
				if ($rank == 3)
				{
					echo "<div class=\"box\">Advertisement!</div>";
				}
					
				echo "<div class=\"hr\"></div>";
		}
		}
		if ($num_sites % 20) {
			$next = 0;
			$next = $next;
			$query = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC");
			$number = mysql_num_rows($query);
			$repeat = ceil($number/$this->max); //So I can set the max number shown?
			while ($repeat > 0) {
				switch ($repeat) {
					case 1:
						echo "[<a href='?rank=2'>50-100</a>]";
						break;
					case 2:
						echo "[<a href='?rank=3'>100-150</a>]";
						break;
                                        //Would go on, but for testing purposed I only need these two
					default:
						break;
				}
				$repeat--;
			}
		}
	}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple Ranking Script

Post by requinix »

Couple questions:
1. The first half of the code seems to use 20 per page, while the second half uses 50. What's up with that?
2. Is there a more general rule for advertisements? If you show 50 per page then when do the advertisements show up?
3. This is some kind of pagination thing, right?
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Simple Ranking Script

Post by Trahb »

The bottom half is just echoing the "50-100", in reality, the top half will have the 50. As for the ranks, after the 3rd and 12th entries on each page ( whether it's 50-100, 100-50, etc ). And yes it is.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple Ranking Script

Post by requinix »

I have a suggestion. It'll involve rewriting that function, but you should find it to be faster and easier to use than what you have now.

First issue: getting the rows you want and figuring out how many pages there should be to show all of them.
If you have a MySQL query like

Code: Select all

SELECT SQL_CALC_FOUND_ROWS * FROM `table` ORDER BY `number` DESC LIMIT $x, $y
It'll give you $y rows starting at the ($x+1)st found. If there were 100 rows in the table and $x=4,$y=10 then you'd get rows #5-14.
If you want "page" N then $x=(N-1)*$y.

If you immediately after executed the query

Code: Select all

SELECT FOUND_ROWS()
you'd get one row with one column: the number in there is how many rows MySQL would have given you without the LIMIT attached. With the above example, you'd get 100.

Second issue: the page links.
If $x=0 (starting row), $y=10 (maximum number of rows returned), and $z=100 (the number of rows in the table):
  • Page N has rank=N and shows rows (N-1)*$y+1 through N*$y
  • The current page has (N-1)*$y+1 <= $x <= N*$y
  • The last page will be the one where N*$y >= $z
Post Reply