//Preparing --Start--function pageThis($max, $max_page_links, $query, $page_link){//Uncomment the next 4 lines if don't wish to use this as a function//$page_link="paged.php";//$max=30;//Maximum items per page (rows)//$max_page_links=10;//Maximum links number in the page//$query="select * from some_table";//Your query$num_stuff=mysql_num_rows(mysql_query($query));//Getting the total possible rowsif (isset($_GET['page'])&&$_GET['page']>0){$start=intval($_GET['page'])-1;//Now the page number make more sense (page=1 is actually the first page)$current_page=intval($_GET['page']);//Some cleaning (SQL Injection prevention, and get rid of negative numbers)}//If no parameters passed.. just give the first pageelse {$current_page=1;$start=0;}//If a large page numbre passed (more than there actually is) just give the last pageif ($current_page>ceil($num_stuff/$max)){$current_page=ceil($num_stuff/$max);$start=ceil($num_stuff/$max)-1;}$start*=$max;//Which row to start with$get_stuff_query.=" limit $start, $max";//Adding the limit//Preparing --End--//Actual paging --Start--if ($num_stuff>$max){//Is there any need for pagin?if ($current_page>1){//Making previous page & first page links when needed$previous_page=$current_page-1;//previousious means -1echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=1\">|<</a> ";//First page is the page number.. you guessed it right.. 1echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=$previous_page\"<<</a> ";}if ($current_page>$max_page_links/2){//Are we going far away from the first viewed page link?if (ceil($num_stuff/$max)-$current_page<(($max_page_links/2)+1)){//Are we getting closer to last viewed page link?$start_counter=$current_page-($max_page_links-(ceil($num_stuff/$max)-$current_page));//Yes, Then we need to view more page links$end_counter=ceil($num_stuff/$max);//And no need to view page links more than the query can bring}else{$start_counter=$current_page-(($max_page_links/2)-1);//No, then just view some links before the currentrent page$end_counter=$current_page+($max_page_links/2);//And some links after}}else{//Still in the first pages?$start_counter=1;//Start with page one$end_counter=$max_page_links;//Show only enough links}for ($i=$start_counter;$i<=$end_counter;$i++){//A loop for viewing the linksif ($i==$end_counter){//Is this the last link?if ($i==$current_page){//Are we actually on the last page? Because we don't need the | after the linkecho "<a style=\"color:blue;\" class=\"link\">".$i."</a>";//Then make it look like we're on that page}else{echo "<a class=\"link\" href=\"".$page_link."page=".$i."\">".$i."</a>";//Well yeah, it's the last link.. but we're not there yet.}}else{//Not the last page you say.. mmm.. then print normally (with | after the link)if ($i==$current_page){//Are we vewing this page?echo "<a style=\"color:blue;\" class=\"link\">".$i."</a> | ";//Make us know it}else{//Not viewing.. just a normal link (the most common case here)echo "<a class=\"link\" href=\"".$page_link."page=".$i."\">".$i."</a> | ";//Nothing to say}}}if ($current_page<ceil($num_stuff/$max)){//Making the next and last page links$next_page=$current_page+1;//Next means +1$last_page=ceil($num_stuff/$max);//and the last page is the page.. whell.. it's the last one the query can bringecho " <a style=\"text-decoration: none;\" href=\"".$page_link."page=$next_page\">>></a>";echo " <a style=\"text-decoration: none;\" href=\"".$page_link."page=$last_page\">>|</a>";}}}
Sunday, 8 July 2012
Very simple but very powerful (Web Development)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment