dsebywql016137 2011-10-22 14:29
浏览 28

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

Ok my is this I am creating a script for a website am designing that will using pagination to draw quotes out from database,

I have done that part quite well, setting up the pagination class and actually making it to print out the results, but I have been trying to make it print with a numbering system - as in each.

I want each quote to have its own number like:

  1. "One cannot overrun his shadow";
  2. "foo is not fool, just used in php"
  3. "so on and so forth";

    Look at my class:

    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>
    ";            
    echo $pagination;
    

    }

    ////////// and this is my quote display page

    $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) ;
    

    the results am getting is that if i click on the first number in the pagination link to give me quote.html?p=1

    I get them to list it serially page:1 1[quote]A rolling stone gathers no moss.[/quote]
    2[quote]A rolling stone gathers no moss.[/quote]
    3[quote]A rolling stone gathers no moss.[/quote]
    4[quote]A rolling stone gathers no moss.[/quote]
    5[quote]A rolling stone gathers no moss.[/quote]

    but the other page quote.html?p=2 instead of starting from page:2 6[qoute]nothing[/quote] 7[quote]A rolling stone gathers no moss.[/quote]

    rather starts counting from one again page:1 1[qoute]nothing[/quote] 2[quote]A rolling stone gathers no moss.[/quote]

    I want it to appear like we have in BBC FOOTBALL blogs where comment page number 2 starts from 101 instead of starting all over from 1 again

    I hope my question is not too long to understand, if that I am sorry just felt like posting them all for better understanding.

  • 写回答

1条回答 默认 最新

  • dongyou5098 2011-10-22 14:35
    关注

    I think you might be over complicating the problem. Some pseudocode:

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

    Current page number comes from your URL with the ?p= GET parameter.

    Then on each iteration to echo out a quote add one to the Current number.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助