Jump to content

Rank is incorrect


3raser

Recommended Posts

Ever since I switched over to pagination, the ranks for the sites have been off. I use to have an $i++ increment that kept the ranks, but now with pagination, it just ranks those on the current page. Is there a way to bypass this?

 

public function grabSites()
{
	//amount of sites
	$amount = mysql_num_rows(mysql_query("SELECT * FROM sites"));

	if($amount > 0)
	{
		//amount per page
		$p_page = 13;

		//track page
		$start = $_GET['page'];

		//max_pages
		$max_pages = $amount / $p_page;

		//set default
		if(!$start) $start = 0;

		//new query
		$query = mysql_query("SELECT id,title,description,votes,date,outl,site_url FROM sites ORDER BY votes DESC LIMIT $start, $p_page");

		while($fetch = mysql_fetch_assoc($query))
		{
			++$i;
			$i = $i + $p_page;	

			echo "
			<div id='post-1' class='post'>
			<h2 class='title'><font color='white'>#". $i." ".$fetch['title'] ."</font></h2>
			<div class='entry'>
			<b>". $fetch['title'] ." currently has ". $fetch['votes'] ." votes. 
			<a href='vote.php?id=". $fetch['id'] ."'>Vote now</a>.</b>
			<br/><br/>
			<p>". nl2br(stripslashes($fetch['description'])) ."</p>
			</div>
			<p class='meta'><a href='". $fetch['site_url'] ."'>Visit</a></p>
			</div>
			";
		}

		//set back and forth variables
		$previous = $start - $p_page;
		$next = $start + $p_page;

		?>
		<hr>
		<center>
		<?php


		if(!($start<=0)) echo "<a href='index.php?page=". $previous ."'><<</a>";

		$i = 1;
		for($x = 0; $x < $amount; $x = $x + $p_page)
		{
			echo "<a href='index.php?page=". $x ."'> ". $i ." </a>";
			$i++;
		}

		if(!($start>=$amount-$p_page)) echo "<a href='index.php?page=". $next ."'>>></a></center>";
		?>
		</center>
		<?php

	}
	else
	{
			echo "
			<div id='post-1' class='post'>
			<h2 class='title'><a href='#'>Oh NOEZ!</a></h2>
			<h3 class='date'>". date("M-d-Y") ."</h3>
			<div class='entry'>
			<p>There are currently no sites to display!</p>
			</div>
			<p class='meta'>View</p>
			</div>
			";
	}
}

Link to comment
Share on other sites

You could do something like this:

$rank = $p_page * $_GET['page'] + $i;

 

Say your on page 5 of the results and there are 10 results per page and you're on the 4th result of the query, you would get this:

10 * 5 + 4

50 + 4

54 = rank

 

Thanks! But it didn't seem to work. :/ - The first page was fine, but the 2nd page had numbers going into the 100s when there are only 31 sites in the database.

Link to comment
Share on other sites

$offset = ($_GET['page'] - 1) * $p_page;

 

Add this check:

 

if(!isset($_GET['page']) || $_GET['page'] < 1) $_GET['page'] = 1;

 

PS to reverse engineer use:

 

$page = $offset / $p_page + 1;

 

This returns 0 for all ranks. And what is the point of the !isset if statement?

 

Tried this:

 

		//amount of sites
		$amount = mysql_num_rows(mysql_query("SELECT * FROM sites"));

		if($amount > 0)
		{
			//amount per page
			$p_page = 13;

			//track page
			$start = $_GET['page'];

			//max_pages
			$max_pages = $amount / $p_page;

			$offset = ($_GET['page'] - 1) * $p_page;
			if(!isset($_GET['page']) || $_GET['page'] < 1) $_GET['page'] = 1;

			//set default
			if(!$start) $start = 0;

			//new query
			$query = mysql_query("SELECT id,title,description,votes,date,outl,site_url FROM sites ORDER BY votes DESC LIMIT $start, $p_page");

			while($fetch = mysql_fetch_assoc($query))
			{
				$i++;
				$rank = $offset / $p_page + 1;

				echo "
				<div id='post-1' class='post'>
				<h2 class='title'><font color='white'>#". $rank." ".$fetch['title'] ."</font></h2>
				<div class='entry'>
				<b>". $fetch['title'] ." currently has ". $fetch['votes'] ." votes. 
				<a href='vote.php?id=". $fetch['id'] ."'>Vote now</a>.</b>
				<br/><br/>
				<p>". nl2br(stripslashes($fetch['description'])) ."</p>
				</div>
				<p class='meta'> <a href='index.php?report=". $fetch['id'] ."'>Report</a> / <a href='". $fetch['site_url'] ."'>Visit</a></p>
				</div>
				";
			}

			//set back and forth variables
			$previous = $start - $p_page;
			$next = $start + $p_page;

			?>
			<hr>
			<center>
			<?php


			if(!($start<=0)) echo "<a href='index.php?page=". $previous ."'><<</a>";

			$i = 1;
			for($x = 0; $x < $amount; $x = $x + $p_page)
			{
				echo "<a href='index.php?page=". $x ."'> ". $i ." </a>";
				$i++;
			}

			if(!($start>=$amount-$p_page)) echo "<a href='index.php?page=". $next ."'>>></a></center>";
			?>
			</center>
			<?php

Link to comment
Share on other sites

Try this:

			//amount of sites
		$amount = 0;
		if($res = mysql_query("SELECT count(*) FROM sites"))
			 $row = mysql_fetch_row($res);
			 $amount = $row[0];
		}

		if($amount > 0)
		{
			//amount per page
			$p_page = 13;

			//max_pages
			$max_pages = $amount / $p_page;

			//track page
			if(empty($_GET['page']) || $_GET['page'] < 1) $_GET['page'] = 1;
			$start = ($_GET['page'] - 1) * $p_page;

			//new query
			$query = mysql_query("SELECT id,title,description,votes,date,outl,site_url FROM sites ORDER BY votes DESC LIMIT $start, $p_page");

			while($fetch = mysql_fetch_assoc($query))
			{
				$i++;
				$rank = $start + $i;

				echo "
				<div id='post-1' class='post'>
				<h2 class='title'><font color='white'>#". $rank." ".$fetch['title'] ."</font></h2>
				<div class='entry'>
				<b>". $fetch['title'] ." currently has ". $fetch['votes'] ." votes. 
				<a href='vote.php?id=". $fetch['id'] ."'>Vote now</a>.</b>
				<br/><br/>
				<p>". nl2br(stripslashes($fetch['description'])) ."</p>
				</div>
				<p class='meta'> <a href='index.php?report=". $fetch['id'] ."'>Report</a> / <a href='". $fetch['site_url'] ."'>Visit</a></p>
				</div>
				";
			}

 

I changed it to get the number of rows directly from mysql just because I figure it'll be faster than counting rows with PHP once you have a lot of rows (not sure, but it doesn't hurt). You were right on your rank calculation from the start, you just placed the suggestions in the wrong places.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.