2011-06-16 82 views
0

好的。我创造了这个另外一个问题,但我想我有一个bit.Ill复杂它尽量简化这里我的问题:如何使用数据提供者实现php-mysql数据分页

假设你有这样的代码:

<?php 
    if($_POST['page']){ 
     $page = $_POST['page']; 
     $cur_page = $page; 
     $page -= 1; 
     $per_page = 15; 
     $previous_btn = true; 
     $next_btn = true; 
     $first_btn = true; 
     $last_btn = true; 
     $start = $page * $per_page; 
     include"dbconnect.php"; 

     $query_pag_data = "SELECT msg_id,message from test LIMIT $start, $per_page"; 
     $result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error()); 
     $msg = ""; 
     while ($row = mysql_fetch_array($result_pag_data)) { 
      $htmlmsg=htmlentities($row['message']); 
      $msg .= "<li><b>" . $row['msg_id'] . "</b> " . $htmlmsg . "</li>"; 
     } 
     $msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data 

     /* --------------------------------------------- */ 
     $query_pag_num = "SELECT COUNT(*) AS count FROM student"; 
     $result_pag_num = mysql_query($query_pag_num); 
     $row = mysql_fetch_array($result_pag_num); 
     $count = $row['count']; 
     $no_of_paginations = ceil($count/$per_page); 

     /* ---------------Calculating the starting and endign values for the loop----------------------------------- */ 
     if ($cur_page >= 7) { 
      $start_loop = $cur_page - 3; 
      if ($no_of_paginations > $cur_page + 3) 
       $end_loop = $cur_page + 3; 
      else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) { 
       $start_loop = $no_of_paginations - 6; 
       $end_loop = $no_of_paginations; 
      } else { 
       $end_loop = $no_of_paginations; 
      } 
     } else { 
      $start_loop = 1; 
      if ($no_of_paginations > 7) 
       $end_loop = 7; 
      else 
       $end_loop = $no_of_paginations; 
     } 
     /* ----------------------------------------------------------------------------------------------------------- */ 
     $msg .= "<div class='pagination'><ul>"; 

     // FOR ENABLING THE FIRST BUTTON 
     if ($first_btn && $cur_page > 1) { 
      $msg .= "<li p='1' class='active'>First</li>"; 
     } else if ($first_btn) { 
      $msg .= "<li p='1' class='inactive'>First</li>"; 
     } 

     // FOR ENABLING THE PREVIOUS BUTTON 
     if ($previous_btn && $cur_page > 1) { 
      $pre = $cur_page - 1; 
      $msg .= "<li p='$pre' class='active'>Previous</li>"; 
     } else if ($previous_btn) { 
      $msg .= "<li class='inactive'>Previous</li>"; 
     } 
     for ($i = $start_loop; $i <= $end_loop; $i++) { 

      if ($cur_page == $i) 
       $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>"; 
      else 
       $msg .= "<li p='$i' class='active'>{$i}</li>"; 
     } 

     // TO ENABLE THE NEXT BUTTON 
     if ($next_btn && $cur_page < $no_of_paginations) { 
      $nex = $cur_page + 1; 
      $msg .= "<li p='$nex' class='active'>Next</li>"; 
     } else if ($next_btn) { 
      $msg .= "<li class='inactive'>Next</li>"; 
     } 

     // TO ENABLE THE END BUTTON 
     if ($last_btn && $cur_page < $no_of_paginations) { 
      $msg .= "<li p='$no_of_paginations' class='active'>Last</li>"; 
     } else if ($last_btn) { 
      $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>"; 
     } 
     $goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>"; 
     $total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>"; 
     $msg = $msg . "</ul>" . $goto . $total_string . "</div>"; // Content for pagination 
     echo $msg; 
    } 

的代码是一个更大的分页的一部分解决方案,但我的问题是这样的:

在上面的代码中,查询数据并在同一地点执行分页算法;

这是在每个分页教程/演示/走出去的时候都看到的方法。

在我的情况下,我已经有了需要以json格式分页的数据,所以我不会查询数据库。基本上,如何实现数组的分页,例如?看看那里的样本,他们都使用LIMIT分页,我的思维不能想象没有它的任何分页方法,但我无法重新查询分贝。

有没有人试图分类别的东西,MySQL结果集?

回答

2

方式一:

使用APC/eAccelerator在高速缓存结果和数据分析到不同的页面。所以,下一次有人请求页面,不查询数据库,而是从缓存中检索数据,切片它根据页面数

PHP:

$start = ($this->pageNo - 1) * $pageSize; 
$page = array_slice($data, $start, $this->pageSize); 

另一种方式:

使用jquery插件或任何其他JavaScript插件来实现分页,它将转储所有数据一次,并将它们解析到不同的页面。一个非常有用的链接:http://th3silverlining.com/2010/04/15/pajination-a-jquery-pagination-plugin/http://tablesorter.com/docs/example-pager.html