A simple Pagination function in PHP

Following function can be used for pagination blocks. You need to call this function, and print the return string. The return string format is << <> >> i.e. first page link, previous page link, page numbers link, next page link, last page link. You need to add javascript function called paginate(page_no) which takes page no as the argument & in turns do AJAX call for fetching the next records. Argument list for pagination function:
  1. $current_page: current page number
  2. $cnt: total records
  3. $limit: No of records to be displayed
function pagination($current_page,$cnt,$limit){ $last_page = ceil($cnt/$limit); $str = ""; $first_pg = ""; $prev_pg = ""; $next = ""; $last = ""; if($last_page == 1){ $first_pg = " << "; $prev_pg = " < "; }else{ if($current_page > 1){ $first_pg = "&lt;a href=\"javascript:void(0)\" onClick=\"paginate(1)\"> << &lt;/a&gt;"; $prev_pg = "&lt;a href=\"javascript:void(0)\" onClick=\"paginate($current_page-1)\"> < &lt;/a&gt;"; } for($i=1;$i<=$last_page;$i++){ if($i == $current_page) $str .= $i." | "; else $str .= "&lt;a href=\"javascript:void(0)\" onClick=\"paginate($i)\">&lt;u&gt;".$i."&lt;/u&gt;&lt;/a&gt; | "; } $str = substr($str,0,strlen($str)-2); if($current_page != $last_page){ $next = "&lt;a href=\"javascript:void(0)\" onClick=\"paginate($current_page+1)\"> > &lt;/a&gt;"; $last = "&lt;a href=\"javascript:void(0)\" onClick=\"paginate($last_page)\"> >> &lt;/a&gt;"; } if($current_page == $last_page){ $next = " > "; $last = " >> "; } } $page_string = $first_pg.$prev_pg.$str.$next.$last; return $page_string; }

Comments

Thanks for the nice

Thanks for the nice pagination function.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>

More information about formatting options