Jump to content

[Help] How to put Rank on pagination


llipop88

Recommended Posts

Hi All,

 

I am new to php, just wanna study as part of the course. I would like to add rank on each row data query from database.

I try some method, but it did't work right. My way was when click on second page. The Rank start over from number 1-8, by right it should start from number 8 to 16. and page number 3 start from 17 to 25. I try to modify the code from Jatinder. But it seem like too difficult for me.

below are the code. please advice

 

The code shown below

this is page.php

<?php

//Include the PS_Pagination class

include('ps_pagination.php');

 

//Connect to mysql db

$conn = mysql_connect('localhost','root','xxxxxx');

mysql_select_db('games',$conn);

$sql = 'SELECT * FROM user ORDER BY subject DESC';

//$sql = "SELECT * FROM user where rank >= $row[rank] order by subject * 1 asc"

 

 

//while($data = mysql_fetch_array($sql)) {

 

$rank = 1;

//Create a PS_Pagination object

$pager = new PS_Pagination($conn,$sql,8,3);

 

//The paginate() function returns a mysql result set

$rs = $pager->paginate();

echo "<table cellpadding=10 border=1><tr><td>Rank</td><td>ID</td><td>Username</td><td>game 01 score</td></tr>";

while($row = mysql_fetch_assoc($rs)) {

 

$ranks = $rank++;

 

    echo "<tr>";

    echo "<td>$ranks</td>";

        echo "<td>".$row['id']."</td>";

        echo "<td>".$row['name']."</td>";

        echo "<td>".$row['subject']."</td>";

        echo "</tr>";

//echo $row['email'],"<br />\n";

}

echo "</table>";

echo "$page_number $links $i";

 

 

//Display the full navigation in one go

echo $pager->renderFullNav();

 

//Or you can display the inidividual links

//echo "<br />";

 

//Display the link to first page: First

//echo $pager->renderFirst();

 

//Display the link to previous page: <<

//echo $pager->renderPrev();

 

//Display page links: 1 2 3

//echo $pager->renderNav();

 

//Display the link to next page: >>

//echo $pager->renderNext();

 

//Display the link to last page: Last

//echo $pager->renderLast();

?>

 

this is ps_pagination.php from Jatinder

<?php

/**

* PHPSense Pagination Class

*

* PHP tutorials and scripts

*

* @package PHPSense

* @author Jatinder Singh Thind

* @copyright Copyright © 2006, Jatinder Singh Thind

* @link http://www.phpsense.com

*/

 

// ------------------------------------------------------------------------

 

class PS_Pagination {

var $php_self;

var $rows_per_page; //Number of records to display per page

var $total_rows; //Total number of rows returned by the query

var $links_per_page; //Number of links to display per page

var $sql;

var $debug = false;

var $conn;

var $page;

var $max_pages;

var $offset;

 

/**

* Constructor

*

* @param resource $connection Mysql connection link

* @param string $sql SQL query to paginate. Example : SELECT * FROM users

* @param integer $rows_per_page Number of records to display per page. Defaults to 10

* @param integer $links_per_page Number of links to display per page. Defaults to 5

*/

 

function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5) {

$this->conn = $connection;

$this->sql = $sql;

$this->rows_per_page = $rows_per_page;

$this->links_per_page = $links_per_page;

$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);

if(isset($_GET['page'])) {

$this->page = intval($_GET['page']);

}

}

 

/**

* Executes the SQL query and initializes internal variables

*

* @access public

* @return resource

*/

function paginate() {

if(!$this->conn) {

if($this->debug) echo "MySQL connection missing<br />";

return false;

}

 

$all_rs = @mysql_query($this->sql);

if(!$all_rs) {

if($this->debug) echo "SQL query failed. Check your query.<br />";

return false;

}

$this->total_rows = mysql_num_rows($all_rs);

@mysql_close($all_rs);

 

$this->max_pages = ceil($this->total_rows/$this->rows_per_page);

//Check the page value just in case someone is trying to input an aribitrary value

if($this->page > $this->max_pages || $this->page <= 0) {

$this->page = 1;

}

 

//Calculate Offset

$this->offset = $this->rows_per_page * ($this->page-1);

 

//Fetch the required result set

$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");

if(!$rs) {

if($this->debug) echo "Pagination query failed. Check your query.<br />";

return false;

}

return $rs;

}

 

/**

* Display the link to the first page

*

* @access public

* @param string $tag Text string to be displayed as the link. Defaults to 'First'

* @return string

*/

function renderFirst($tag='First') {

if($this->page == 1) {

return $tag;

}

else {

return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';

}

}

 

/**

* Display the link to the last page

*

* @access public

* @param string $tag Text string to be displayed as the link. Defaults to 'Last'

* @return string

*/

function renderLast($tag='Last') {

if($this->page == $this->max_pages) {

return $tag;

}

else {

return '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';

}

}

 

/**

* Display the next link

*

* @access public

* @param string $tag Text string to be displayed as the link. Defaults to '>>'

* @return string

*/

function renderNext($tag=' >>') {

if($this->page < $this->max_pages) {

return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';

}

else {

return $tag;

}

}

 

/**

* Display the previous link

*

* @access public

* @param string $tag Text string to be displayed as the link. Defaults to '<<'

* @return string

*/

function renderPrev($tag='<<') {

if($this->page > 1) {

return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';

}

else {

return $tag;

}

}

 

/**

* Display the page links

*

* @access public

* @return string

*/

function renderNav() {

for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {

if($this->page >= $i) {

$start = $i;

}

}

 

if($this->max_pages > $this->links_per_page) {

$end = $start+$this->links_per_page;

if($end > $this->max_pages) $end = $this->max_pages+1;

}

else {

$end = $this->max_pages;

}

 

$links = '';

 

for( $i=$start ; $i<$end ; $i++) {

if($i == $this->page) {

$links .= " $i ";

}

else {

$links .= ' <a href="'.$this->php_self.'?page='.$i.'">'.$i.'</a> ';

}

}

 

return $links;

}

 

/**

* Display full pagination navigation

*

* @access public

* @return string

*/

function renderFullNav() {

return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();

}

 

/**

* Set debug mode

*

* @access public

* @param bool $debug Set to TRUE to enable debug messages

* @return void

*/

function setDebug($debug) {

$this->debug = $debug;

}

}

 

?>

 

 

Please help..urgent..dateline for project submission are near  :(

Link to comment
Share on other sites

change

$rank = 1;
   //Create a PS_Pagination object
   $pager = new PS_Pagination($conn,$sql,8,3);
   
   //The paginate() function returns a mysql result set
   $rs = $pager->paginate();
    echo "<table cellpadding=10 border=1><tr><td>Rank</td><td>ID</td><td>Username</td><td>game 01 score</td></tr>";
   while($row = mysql_fetch_assoc($rs)) {

to

//$rank = 1;
   //Create a PS_Pagination object
   $pager = new PS_Pagination($conn,$sql,8,3);
   
   //The paginate() function returns a mysql result set
   $rs = $pager->paginate();
   $rank = $pager->offset; // <-- add this line
    echo "<table cellpadding=10 border=1><tr><td>Rank</td><td>ID</td><td>Username</td><td>game 01 score</td></tr>";
   while($row = mysql_fetch_assoc($rs)) {

Link to comment
Share on other sites

I got it..Thank you sasa..Thank you very much  ;D ;D ;D

 

I change the

$rs = $pager->paginate();

$rank = $pager->offset;

echo "<table cellpadding=10 border=1><tr><td>Rank</td><td>ID</td><td>Username</td><td>Game 01 Score</td></tr>";

while($row = mysql_fetch_assoc($rs)) {

$ranks = $rank++;

    echo "<tr>";

to

$rs = $pager->paginate();

$rank = $pager->offset;

echo "<table cellpadding=10 border=1><tr><td>Rank</td><td>ID</td><td>Username</td><td>Game 01 Score</td></tr>";

while($row = mysql_fetch_assoc($rs)) {

$ranks = $rank++ +1;

    echo "<tr>";

 

warm me is I did wrong  :D, becoz i got limited php knowledge..

Link to comment
Share on other sites

Thank you sasa. it work well.

one more question. regarding the data query

 

$sql = 'SELECT * FROM dolly ORDER BY game01 DESC';

 

how do I need to skip if there got NULL in that row.

That mean I wanna skip those data which any of the game01 are NULL.

 

eg.

rank  username  score

1      pop          30

2      kiki          20

3      jiji            10

4      hihi          0

5      gigi                <---skip this

6      vivi                <---and this

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.