佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 903|回复: 2

[新手]如何用php mysql建立rank number?

[复制链接]
发表于 17-4-2008 11:54 AM | 显示全部楼层 |阅读模式
各位大大,小弟是php的新手,
我想弄个Rank Number在 Row 旁边,但我的方式是wrong的,当进了第2面那
还是重第1算起。应该是9-16的。有谁懂如何做是好吗?

page.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 score DESC';
        //$sql = "SELECT * FROM user where rank >= $row[rank] order by score * 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 "";
        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['score'],"
\n";
        }
         echo "
RankIDUsernamescore
";
         echo "$page_number $links $i";


        //Display the full navigation in one go
        echo $pager->renderFullNav();
       
        //Or you can display the inidividual links
        //echo "
";
       
        //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();
?>
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 17-4-2008 11:55 AM | 显示全部楼层
<?php
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package        PHPSense
* @author        Jatinder Singh Thind
* @copyright    Copyright (c) 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;
    }
}

?>


这是Jatinder的Pagination Code
回复

使用道具 举报

发表于 18-4-2008 05:41 PM | 显示全部楼层
好长的 Code 咧   看得有点晕。。。

可以再解释一下你想做什么,跟 error 是什么吗?
然后,请再整理一下你的 code ,那些 comment 掉的尽量不要 post 来。看得很辛苦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 1-1-2026 05:41 PM , Processed in 0.112111 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表