Forum Moderators: coopster
I've had a rather good start to sorting out my page numbering script for a project im working, i'm to the point where i can do the following:
Display the total number of pages and the page currently viewing, allow the user the define the number of items to display per page and to be able to jump to page of choice.
So with that mentioned all functionality is there, now i want some eye candy to go with it
As mentioned ive sorted out the total number of, and current page viewing displays out, now i want to show the number of items from and to out of the total, now the total part is complete its the from - to areas that im having trouble with.
This is an example of the output (Without drop down menu for no of items per page):
Items 7 to 9 of 3 Previous 1 2 3 Next Results page 3 of 3
The "Items 7 to 9 of 3" is the bit im trying to solve if anyone can help me here that would be great.
NOTE: i would provide some code but i dont see it nessasary here as its just the formula im after...
TIA
The following is the data i have to work with to generate the formula im almost there altho smoking weed and being hungover doesnt help.
$pageNumber = 1 (The page currently being viewed)
$prodsPerPage = 10 (This is the total number of products per page that a user can see)
$totalProducts = 43 (This is the total number of products in the database relating to a certain category)
See if that helps with the solution.
NOTE: the values of the last three variables will change, please bare in mind when providing a solution.
TIA
Ok, let's see:
/*
$limit is the max items per page
$numItems is the total of items
*/
$limit = (empty($limit))? 1 : $limit;
$basePageNum = floor($numItems / $limit);
$offset = $numItems - ($basePageNum * $limit);
if ($page == $basePageNum + 1)
{
$range2 = $offset;
}
else
{
$range2 = $limit;
}
I hope give you an idea at least
if(!function_exists('my_textpage')) //Outputs the text: 7 - 9 of about 21 records.
{
function my_textpage($page, $prodsPerPage, $total)
{
if((int)$total)
{
$low = $prodsPerPage * ($page - 1) + 1;
if(($high = $prodsPerPage * $page) > $total) $high = $total;
$text = $low." - ".$high." of about ".$total." records. ";
return $text;
}
else $text = "No records found. ";
}
}
if(!function_exists('my_choosePage')) //outputs the HTML to choose page. First prev 1 2 3 [4] 5 next last
{
function my_choosePage($page, $prodsPerPage, $selectsPerPage, $total, $link)
{
if($page_max = ceil($total/$prodsPerPage))
{
if($page > 1)
{
$prev = $limit - 1;
$text = "<a href=\"".$link.".html?page=1\" target=\"_top\">First </a><a href=\"".$link.".html?page=$prev\" target=\"_top\">Prev </a>";
}
$k = $page;
$i = 0;
while($k > 1)
{
$k--;
$i++;
if($i == floor($selectsPerPage/2 - 1)) break;
}
for($i = 0; $i < $selectsPerPage; $i++)
{
if($k > $page_max) break;
if($k!= $page) $text .="<a href=\"".$link.".html?page=$k\" target=\"_top\">$k </a> ";
else $text .=" [ $k ] ";
$k++;
}
if($page_max > $page)
{
$next = $page + 1;
$text .="<a href=\"".$link.".html?page=$next\" target=\"_top\">Next </a><a href=\"".$link.".html?page=$page_max\" target=\"_top\">Last </a>";
}
}
return $text;
}
}
Hope this helps!
selectsPerPage is the number of how many pages are to be displayed. See the google
Best regards
Michal Cibor
[edited by: mcibor at 4:57 pm (utc) on Nov. 6, 2005]
//Products To display number
$productsTo=$_GET['page']*$limit;
//If final page or only page then determine overflow and deduct to create actual amount relative to total stock
if($_GET['page']==$displayPages){
$overFlow=$productsTo-$totalRows;
$productsTo=$productsTo-$overFlow;
}
//Products From display number
//If first page set to 1
if($_GET['page']==1){
$productsFrom=1;
}else{
//Anything else do some math to get desired number
$deduction=($_GET['page']*$limit);
$productsFrom=($deduction-($limit-1));
}
Seems to work so far altho im testing it on a database with few products in there will add in some more and expand the categories to test it out properly, any comments on the code would be great.
NOTE: If there are no items to display the user is presented with a Sorry nothing in stock message - so i havent coded in the need to display 0's for no items as this will be dealt with before getting to this stage...
TIA Once again