waynew Posted October 27, 2009 Share Posted October 27, 2009 The title of this thread is basically the result I'm aiming for. I have pagination (paged results) in place. Say for example, I show 2 items per page and there are 10 items in the system, I want page 1 to show 1-2 of 10 and page 2 to show 3-4 of 10 and so forth. I have a script at the moment that I'm working on, but the result is never right, no matter what I do: $start_num = $page->page_num * $advert->per_page; $end_num = $start_num + $advert->per_page; if($end_num > $all) $end_num = $all; $showing = "$start_num - $end_num of $all"; $page->page_num is the current page number. $advert->per_page is how many items to show on each page. $page->page_num will never be 0 or bigger than the total amount of pages in the system. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/ Share on other sites More sharing options...
ashton321 Posted October 27, 2009 Share Posted October 27, 2009 check out this tutorial http://www.phpfreaks.com/tutorial/basic-pagination Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/#findComment-945284 Share on other sites More sharing options...
waynew Posted October 27, 2009 Author Share Posted October 27, 2009 I have pagination setup and working perfectly. I'm trying to figure out how to show 1-2 of 10 etc. Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/#findComment-945288 Share on other sites More sharing options...
joel24 Posted October 27, 2009 Share Posted October 27, 2009 this *should* work... $start_num = ($page->page_num * $advert->per_page) - ($advert->per_page - 1); $end_num = $start_num + $advert->per_page; if($end_num > $all) $end_num = $all; $showing = "$start_num - $end_num of $all"; Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/#findComment-945289 Share on other sites More sharing options...
PravinS Posted October 27, 2009 Share Posted October 27, 2009 Use below given functions function select_row($sql) { //echo $sql . "<br />"; if ($sql!="") { $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error()); if ($result) { while($row = mysql_fetch_assoc($result)) $data[] = $row; } return $data; } } function pagingSlot($sql, $recperpage, $pagesetlimit, $page, $class, $getvars) { $rescnt=mysql_query($sql); $totcnt=mysql_num_rows($rescnt); if (!$page) $page = 1; $first=(($page-1)* $recperpage); $sql = $sql . " limit ".$first.",".$recperpage; $res = select_row($sql); $serial_no = ($page - 1) * $recperpage; $t = ($totcnt/$recperpage); $arr=split('[.]',$t); if ($arr[1]) $totalpages=$arr[0]+1; else $totalpages=$arr[0]; if ($totalpages > $pagesetlimit) { if ($page > 1) $pagesetstart = $page - 1; else $pagesetstart = $page; $pagesetend= ($pagesetstart-1) + $pagesetlimit; if ($pagesetend > $totalpages) { $pagesetend = $totalpages; $pagesetstart = $pagesetend - $pagesetlimit + 1; } } else { $pagesetstart = 1; $pagesetend = $totalpages; } $str = ""; if ($page > 1) { $prev = $page - 1; $str.= "<a href='".$_SERVER['PHP_SELF']."?page=$prev".$getvars."' class='".$class."'> << </a> | "; } else { $str.= "<< | "; } for ($i=$pagesetstart; $i<=$pagesetend; $i++) { if ($i <= $totalpages) { if (!$page) $page=1; if ($page==$i) $str.= '<font color=red>'.$i.'</font> '; else $str.= "<a href='".$_SERVER['PHP_SELF']."?page=$i".$getvars."' class='".$class."'>".$i."</a> "; } } if ($page < $totalpages) { $next = $page + 1; $str.= " | <a href='".$_SERVER['PHP_SELF']."?page=$next".$getvars."' class='".$class."'> >> </a> "; } else { $str.= " | >> "; } if ($totcnt == 0) $str = ""; $arr["records"]=$res; $arr["link"]=$str; $arr["serial"]=$serial_no; return $arr; } Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/#findComment-945296 Share on other sites More sharing options...
waynew Posted October 27, 2009 Author Share Posted October 27, 2009 Worked a charm. Cheers! Just made one change: $start_num = ($page->page_num * $advert->per_page) - ($advert->per_page - 1); $end_num = ($start_num + $advert->per_page) - 1; //take away one from end_num if($end_num > $all) $end_num = $all; $string_1 = "$start_num - $end_num of $all"; Seems to be working fine! Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/#findComment-945297 Share on other sites More sharing options...
waynew Posted October 27, 2009 Author Share Posted October 27, 2009 Use below given functions function select_row($sql) { //echo $sql . "<br />"; if ($sql!="") { $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error()); if ($result) { while($row = mysql_fetch_assoc($result)) $data[] = $row; } return $data; } } function pagingSlot($sql, $recperpage, $pagesetlimit, $page, $class, $getvars) { $rescnt=mysql_query($sql); $totcnt=mysql_num_rows($rescnt); if (!$page) $page = 1; $first=(($page-1)* $recperpage); $sql = $sql . " limit ".$first.",".$recperpage; $res = select_row($sql); $serial_no = ($page - 1) * $recperpage; $t = ($totcnt/$recperpage); $arr=split('[.]',$t); if ($arr[1]) $totalpages=$arr[0]+1; else $totalpages=$arr[0]; if ($totalpages > $pagesetlimit) { if ($page > 1) $pagesetstart = $page - 1; else $pagesetstart = $page; $pagesetend= ($pagesetstart-1) + $pagesetlimit; if ($pagesetend > $totalpages) { $pagesetend = $totalpages; $pagesetstart = $pagesetend - $pagesetlimit + 1; } } else { $pagesetstart = 1; $pagesetend = $totalpages; } $str = ""; if ($page > 1) { $prev = $page - 1; $str.= "<a href='".$_SERVER['PHP_SELF']."?page=$prev".$getvars."' class='".$class."'> << </a> | "; } else { $str.= "<< | "; } for ($i=$pagesetstart; $i<=$pagesetend; $i++) { if ($i <= $totalpages) { if (!$page) $page=1; if ($page==$i) $str.= '<font color=red>'.$i.'</font> '; else $str.= "<a href='".$_SERVER['PHP_SELF']."?page=$i".$getvars."' class='".$class."'>".$i."</a> "; } } if ($page < $totalpages) { $next = $page + 1; $str.= " | <a href='".$_SERVER['PHP_SELF']."?page=$next".$getvars."' class='".$class."'> >> </a> "; } else { $str.= " | >> "; } if ($totcnt == 0) $str = ""; $arr["records"]=$res; $arr["link"]=$str; $arr["serial"]=$serial_no; return $arr; } Thanks man, but I have it fixed. I have a pagination class that I made that works perfectly. Might add in support for showing the current range. Link to comment https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/#findComment-945299 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.