使用分页从数据库中抽取的编号结果 - PHP

时间:2011-10-22 14:29:26

标签: php

好的我就是这个我正在为一个网站创建一个脚本设计,它将使用分页从数据库中绘制引号,

我已经很好地完成了这一部分,设置了分页类并实际打印出结果,但我一直在尝试使用编号系统进行打印 - 就像每个都一样。

我希望每个报价都有自己的编号,如:

  1. “一个人不能超越他的影子”;
  2. “foo不是傻瓜,只是在php中使用”
  3. “等等”;

    看看我的班级:

    class pagination extends cleanPost{
    
    var $p=1, $max_r,$limits;
    var $count_all=0,$sql,$total,$table,$totalres,$totalpages;
    var $r,$i;
    var $show=10;
    
    
    
    function setMax($max_r){
    
           $this->p = $this->clean($_GET['p']);
           $this->max_r = $max_r;
    
            if(empty($this->p))
            {
                 $this->p = 1;
            }
    
           $this->limits = ($this->p - 1) * $this->max_r;
    
        }  
    function setData($table){
    
       $this->table = $table;
       $this->sql = "SELECT * FROM ".$this->table." LIMIT ".$this->limits.",".$this->max_r."";
       $this->sql = mysql_query($this->sql) or die(mysql_error());
       $this->total = "SELECT * FROM ".$this->table."";
       $this->totalres =  mysql_query($this->total) or die(mysql_error());
       $this->count_all = mysql_num_rows($this->totalres); // count all the rows
       $this->totalpages = ceil($this->count_all / $this->max_r); // work out total pages 
    }  
    
    
    
    function displayLinks($show){
    
        $this->show = $show; // How many links to show
    
        echo "<br><br>";
    
        if($this->p > 1) // If p > then one then give link to first page
        {
            $pagination .= "<div class=\"pagination\">
             <a href=?p=1> [first] </a>  ";    
        }
        else
        { // else show nothing
            $pagination .= "<div class=\"pagination\">"."";
    }
    if($this->p != 1)
        { // if p aint equal to 1 then show previous text
    
            $previous = $this->p-1;
    
        $pagination .= "<a href=?p=$previous>«prev</a>";
    
    }
    else
        { //else show nothing
        $pagination.= "<span class=\"disabled\">first</span>"."";
    } 
    for($i =1; $i <= $this->show; $i++) // show ($show) links
    {
    
            if($this->p > $this->totalpages)
                { // if p is greater then totalpages then display nothing
                echo "";
    
        }
        else if($_GET["p"] == $this->p)
                { //if p is equal to the current loop value then dont display that value as link
                   $pagination .= $this->p ;
        }
        else{
                   $pagination .= " <a href=?p=".$this->p."> ".$this->p." </a>"; // else display the rest as links
        }
    
        $this->p++; //increment $p  
    }
    
    
    
    if($_GET["p"] == $this->totalpages)
        {// if page is equal to totalpages then  dont display the last page at the end of links
       $pagination.= "<span class=\"disabled\">last</span>"."";
    }
    else  if($_GET["p"] != $this->totalpages)
    
    {
    // $pagination .= "..."; // display dots   
    #   $pagination .= "<a href=?p=".$this->totalpages.">  ".$this->totalpages." </a>"; 
    }
    else { // else display the last page link after other ones
    
    }
    if($_GET["p"] < $this->totalpages)// if p is less then total pages then show next link
    {
        $next = $_GET["p"] + 1;
        $pagination .= "<a href=?p=$next> next»</a>"; 
        $pagination.= "<a href=?p=".$this->totalpages."> [last] </a>"."";   
    }
      $pagination.= "</div>\n";            
    echo $pagination;
    

    }

    ////////// 这是我的报价显示页面

    $page= new pagination;
      $page->setMax(5);//set the maximum quote shown per page
      $page   ->setData("quotes");
       $page  ->display();
    
    echo "<table border=1 width=100%>";
      while ($row = mysql_fetch_array($page->sql)) 
      { 
    
        echo  $num_results = mysql_num_rows($page->sql);
        echo "<tr>";
    
      for ($i=0; $i <$num_results; $i++)
    {
    echo "<td>";
     $num_found = $i + 1;
    
     echo $num_found.$row['ngwaquote'];
      echo "</td>";
     }
    echo "</tr>";
    
      }
      echo "</table><p>";
       $page  ->displayLinks(4) ;
    

    我得到的结果是,如果我点击分页链接中的第一个数字 给我 quote.html?p = 1时

    我让他们连续列出来 页面:1 1 [引用]滚石没有苔藓。[/ quote]
    2 [引用]滚石没有苔藓。[/ quote]
    3 [引用]滚石没有苔藓。[/ quote]
    4 [引用]滚石没有苔藓。[/ quote]
    5 [引用]滚石没有苔藓。[/ quote]

    但是另一页quote.html?p = 2 而不是从开始 第2页 6 [qoute]无[/报价] 7 [引用]滚石没有苔藓。[/ quote]

    而非再次从一个开始计数 页面:1 1 [qoute]无[/报价] 2 [引用]滚石没有苔藓。[/ quote]

    我希望它看起来像我们在BBC FOOTBALL博客中看到的那样 评论页面2从101开始 而不是从1再次开始

    我希望我的问题不会太久理解,如果我很抱歉只是为了更好地理解而发布这些问题。

1 个答案:

答案 0 :(得分:0)

我认为你可能会使问题复杂化。一些伪代码:

Current number = Number of items per page * Current page number

Current page number来自您的网址,其中包含?p= GET参数。

然后在每次迭代中回显引用,向Current number添加一个引号。