2011-12-26 47 views
1

在我的代碼中我有這樣的
enter image description here如何做分頁............
////////這是我的分頁班///////如何爲我的代碼添加分頁?這個分頁工作的下一步是什麼?

class simple_pagination 
{ 
    function check_integer($which) { 
     if(isset($_REQUEST[$which])){ 
      if (intval($_REQUEST[$which])>0) { 
       //check the paging variable was set or not, 
       //if yes then return its number: 
       //for example: ?page=5, then it will return 5 (integer) 
       return intval($_REQUEST[$which]); 
      } else { 
       return false; 
      } 
     } 
     return false; 
    }//end of check_integer() 

    function get_current_page() { 
     if(($var=$this->check_integer('page'))) { 
      //return value of 'page', in support to above method 
      return $var; 
     } else { 
      //return 1, if it wasnt set before, page=1 
      return 1; 
     } 
    }//end of method get_current_page() 

    function doPages($page_size, $thepage, $query_string, $total=0) { 
    //echo $query_string;exit; 
     //per page count 
     $index_limit = 10; 

     //set the query string to blank, then later attach it with $query_string 
     $query=''; 

     if(strlen($query_string)>0){ 
      $query = "&".$query_string; 
     } 

     //get the current page number example: 3, 4 etc: see above method description 
     $current = $this->get_current_page(); 

     $total_pages=ceil($total/$page_size); 
     //echo $total;exit; 
     $start=max($current-intval($index_limit/2), 1); 
     $end=$start+$index_limit-1; 

     echo '<div class="paging">'; 
     if($current==1) { 
      echo '<span class="prn"> First &lt;&lt;</span>&nbsp;'; 
     } else { 
      $i = $current-1; 
       echo " <a href='$thepage?currentpage=1'>First </a>&nbsp; "; 

      echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">&lt;&lt; </a>&nbsp;'; 
      echo '<span class="prn">...</span>&nbsp;'; 
     } 

     if($start > 1) { 
      $i = 1; 
      echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;'; 
     } 

     for ($i = $start; $i <= $end && $i <= $total_pages; $i++){ 
      if($i==$current) { 
       echo '<span>'.$i.'</span>&nbsp;'; 
      } else { 
       echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;'; 
      } 
     } 

     if($total_pages > $end){ 
      $i = $total_pages; 
      echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;'; 
     } 

     if($current < $total_pages) { 
      $i = $current+1; 

      echo '<span class="prn">...</span>&nbsp;'; 

      echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'"> &gt;&gt; </a>&nbsp;'; 
      echo " <a href='$thepage?page=$total_pages'>Last </a>&nbsp; "; 

     } else { 
      echo '<span class="prn"> Last &gt;&gt;</span>&nbsp;'; 
     } 

     //if nothing passed to method or zero, then dont print result, else print the total count below: 
     if ($total != 0){ 
      //prints the total result count just below the paging 
      echo '<p id="total_count">(total '.$total.' records)</p></div>'; 
     } 

    }//end of method doPages() 

//////wifi.php/////////

 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">Show 
      <select name="results_page" id= "results_page" onChange="this.form.submit();">   
       <option value="5" NO >5</option> 
       <option value="10" NO >10</option> 
       <option value="20" SELECTED >20</option> 
       <option value="50" NO >50</option> 
      </select>  
       results per page 
     </form> 
<?php  
     $pagination = new simple_pagination(); 
      $offset  = 5; 
      $array_old  = $objquote->viewdata(); 
      $total_records = count($array_old); 

      $pagination->doPages($offset,$_SERVER['PHP_SELF'], '', $total_records); ?> 

我已經寫了像上面。這個分頁工作的下一步是什麼? //////圖像///// enter image description here

+1

你從mysql數據庫獲取行嗎? – NAVEED 2011-12-26 05:44:51

+3

聊天說話不受歡迎,請花時間輸入「please」而不是「plz」。如果你從你的詞彙中取出「plz」,你會在* all *你的在線交易中更成功...... – meagar 2011-12-26 05:47:07

+0

@NAVEED是的,我從mysql數據庫中獲取行。 – User1988 2011-12-26 06:48:14

回答

1

如果你正在從數據庫中結果/行,你可以得到選擇使用LIMIT從數據庫中的行號。

LIMIT子句可以被用來限制的行數由SELECT語句

對於實例返回 :

SELECT * FROM `your_table` LIMIT 0, 10 

所以,你可以設置SQL的LIMIT表單提交後查詢並將結果循環顯示。

否則有一些PHP類/教程分頁:

編輯:

後埔蜇你的代碼有問題,我們可以看到你在doPages類中編碼爲$index_limit = 10。您還需要通過$index_limitdoPages課程。

+1

在onchage事件