3raser Posted April 8, 2011 Share Posted April 8, 2011 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> "; } } Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/ Share on other sites More sharing options...
ShoeLace1291 Posted April 8, 2011 Share Posted April 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/#findComment-1198585 Share on other sites More sharing options...
3raser Posted April 8, 2011 Author Share Posted April 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/#findComment-1198588 Share on other sites More sharing options...
ignace Posted April 8, 2011 Share Posted April 8, 2011 $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; Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/#findComment-1198937 Share on other sites More sharing options...
3raser Posted April 8, 2011 Author Share Posted April 8, 2011 $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 Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/#findComment-1199001 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/#findComment-1199005 Share on other sites More sharing options...
ignace Posted April 9, 2011 Share Posted April 9, 2011 I figure it'll be faster than counting rows with PHP Indeed. MyISAM even returns it straight away without actually having to go over the entire table. Quote Link to comment https://forums.phpfreaks.com/topic/233050-rank-is-incorrect/#findComment-1199120 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.