2012-08-07 63 views
0
<?php 
/**************************************************************************** 
* paging.class.php v 1.0 
* 
* version 1.0 
* 
* This script allow to generate dynamically navigation of pages on your web site 
* It is easy to configure and use this paging class 
* 
* ---------------------------------------------------------------- 
* 
* You can find examples in the index.php script (with this package) 
* 

Copyright (C) 2010 Deyan Spasov <[email protected]> 

*******************************************************************************/ 

class paging { 

    public $showPagesNumber = true;  //Show page number  Example: Page 1 of 20 
    public $showPagesForm = true;   //If you want to use pages form. The system show form with input field and go button 
    public $showFirstAndLast = true; //If you have paging buttons first page and last page 
    public $showPrevAndNext = true;  //If you have paging button previous page and next page 
    public $numberOfPages = 7;    //Set the number of shown pages 

    public $pagingClass = 'paging';    //Set css class of your paging 
    public $pagingFirstText = '&laquo;'; //Set text of first button 
    public $pagingLastText = '&raquo;';  //Set text of last button 
    public $pagingPrevText = '&lsaquo;'; //Set text of previous button 
    public $pagingNextText = '&rsaquo;'; //Set text of next button 
    public $pagingPageText = 'Page';   //Set page text 
    public $pagingPageOfText = 'of';   //Set of text 
    public $pagingFormButtonText = 'Go'; //Set text of form button 


    /* 
     Generate standart navigation 
     example: generate('?page=', '&category=1', 5, 10, 100)  
     result: « ‹ 1 2 3 4 5 6 7 8 9 10 › » 

     @access public 

     @parameters 
      $frontUrl - Set the url before page number 
      $backUrl - Set the url after page number 
      $currentPage - Set the current page number 
      $rowsPerPage - Set how many rows you show on page 
      $allRows - Set the number of all rows that you have 

     @return string with paging HTML source code 
    */ 
    public function generate($frontUrl, $backUrl, $currentPage, $rowsPerPage, $allRows) { 

     if($allRows <= $rowsPerPage) { 
      return ''; 
     } 

     $pages = ceil ($allRows/$rowsPerPage); 

     settype($currentPage, "int"); 

     if($currentPage < 1 || $currentPage > $pages) { 
      $currentPage = 1; 
     } 

     $paging = '<div class="'.$this->pagingClass.'">'; 

     if ($currentPage > 2 && $this->showFirstAndLast) { 
      $paging .= '<a href="' . $frontUrl . '1' . $backUrl . '" title="First page" class="arrows">'.$this->pagingFirstText.'</a>'; 
     } 

     if ($currentPage > 1 && $this->showPrevAndNext) { 
      $paging .= '<a href="' . $frontUrl . '' . ($currentPage - 1) . '' . $backUrl . '" title="Previous page" class="arrows">'.$this->pagingPrevText.'</a>'; 
     } 

     $halfPages = $this->numberOfPages/2; 
     settype($halfPages, 'int'); 

     if($pages > $this->numberOfPages) { 
      if($currentPage == 1) { 
       for($i = 1; $i <= $this->numberOfPages; $i ++) 
        $paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>'; 
      } elseif ($currentPage == $pages) { 
       for($i = $pages - $this->numberOfPages + 1; $i <= $pages; $i ++) 
        $paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>'; 
      } else { 
       $start = $currentPage - $halfPages; 

       if($start == 0) { 
        $start = 1; 
       } 

       if($start < 1) { 
        $start_at = 1; 
        $end = (- 1) * $start + $halfPages + $currentPage; 
       } else { 
        $start_at = $start; 
        $end = $start_at + $this->numberOfPages - 1; 
        if ($end > $pages) { 
         $start_at = $start_at - ($end - $pages); 
         $end = $pages; 
        } 
       } 

       for($i = $start_at; $i <= $end; $i ++) 
        $paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>'; 
      } 
     } else { 
      for($i = 1; $i <= $pages; $i ++) 
       $paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>'; 
     } 

     if($currentPage < $pages && $this->showPrevAndNext) { 
      $paging .= '<a href="' . $frontUrl . '' . ($currentPage + 1) . '' . $backUrl . '" title="Next page" class="arrows">'.$this->pagingNextText.'</a>'; 
     } 

     if($currentPage + 1 < $pages && $this->showFirstAndLast) { 
      $paging .= '<a href="' . $frontUrl . '' . $pages . '' . $backUrl . '" title="Last page" class="arrows">'.$this->pagingLastText.'</a>'; 
     } 

     if($this->showPagesForm) { 
      $paging .= '<form method="post" action="" class="page_form">'.$this->pagingPageText.' <input type="text" name="page" value="" /> <button type="submit" name="go_page">'.$this->pagingFormButtonText.'</button></form>'; 
     } 

     if($this->showPagesNumber) { 
      $paging .= '<div class="page_numbers">'.$this->pagingPageText.' '.$currentPage.' '.$this->pagingPageOfText.' '.$pages.'</div>'; 
     } 

     $paging .= '<div style="clear: both"></div>'; 

     return $paging . "</div>"; 
    } 

} 

?> 

我有这个php分页类,我想不出一种方法来限制我的SQL查询。这产生的分页按钮很好,但不知道如何限制我的查询输出。另外,我将如何去与我的网址中的其他变量一起使用这个类。因为这一个不支持它,我不认为。喜欢。问题限制我的sql查询与分页类

mypage.php?状态=未决&页= 4

这里是什么在我的 “工作” 页面。

<?php 

require_once('classes/class.paging.php'); 

// PAGINATION STUFF 
$ordercount1 = mysql_query("SELECT * from orders WHERE technumber='$myuserid'"); 
$ordercount2 = mysql_num_rows($ordercount1); 

// echo $ordercount2; 

    $pagingClass = new paging(); 

    $limit = 10;  //Number of rows that we show on page 
    $allRows = $ordercount2; //Number of all rows that we have 

    if(isset($_POST['page'])) { 
     $_GET['page'] = $_POST['page']; 
    } 

    if(!isset($_GET['page']) || !is_numeric($_GET['page'])) { 
     $_GET['page'] = 1; 
    } 

    //We off the page number 
    $pagingClass->showPagesNumber = false; 
    $pagingClass->showPagesForm = false; 
    $pagingClass->numberOfPages = 10; 
    echo $pagingClass->generate('?page=', '', $_GET['page'], $limit, $allRows); 

// END PAGINATION STUFF 

?> 
+0

限制您的查询?你是什​​么意思 ?和url变量有什么问题?最重要的是不要使用'mysql_ *' - 不仅仅是[*弃用*](http://php.net/manual/en/function.mysql-query.php),而且它还会引用sql_injection。 – alfasin 2012-08-07 03:06:30

+0

限制我的查询,这样我只能显示记录数量,并使其在每个分页页面上相应地移动。 $ getmyorders = mysql_query(“SELECT'orderid','status','date' FROM orders WHERE technumber ='$ myuserid'ORDER BY date DESC LIMIT $ LIMITER_HERE”); – 2012-08-07 03:10:52

+0

$ LIMITER_HERE是我所需要的,因为这个类的编码器未能实现我所假设的。 :( – 2012-08-07 03:13:24

回答

1
  • 您所提供的链接显示用法:

    $页=新分页程序;
    $ pages-> items_total = $ num_rows [0];
    $ pages-> mid_range = 9;
    $ pages-> paginate();
    echo $ pages-> display_pages();

在这个例子中9是从你的选择结果的总数,为了找到这个数字(结果的总数)使用分页程序之前使用select count(1) from...

  • 对于POST/GET参数:你叫过来的参数的分页程序循环,并将它们保存到一个字符串之前:

foreach($_GET as $key=> $val){ $str .= "$key=$val&";}

然后覆盖类每次它创建一个HREF比如,更改:

"href=\"{$_SERVER[PHP_SELF]}?page=$i&ipp=$this->items_per_page\">$i</a> "; 

到:

"href=\"{$_SERVER[PHP_SELF]}?" . $str . "page=$i&ipp=$this->items_per_page\">$i</a> "; 
+0

我提供的链接就是一个例子。在我的主帖中粘贴的代码是实际使用的类,我想出了SQL限制器,下面是我的解决方案:http:// pastebin。com/qH2La4Mh我仍然好奇如何解决我的主要文章中列出的分页类问题。 – 2012-08-07 03:35:08

+0

我认为你所说的是我需要的,但是我将如何将它实现到上面的分页类(我的主帖子中的第一个代码框),以便它能够工作?而不是您所指的示例链接。 – 2012-08-07 03:48:28

+0

在上面的代码中有一条评论说:*您可以在index.php脚本中找到示例(使用此包)* - 查看这些示例的用法将是一个不错的主意。我做了 – alfasin 2012-08-07 04:07:27