Jump to content

Need help to find function to use with pagination


Warz

Recommended Posts

Hi,

 

I'm working on a pagination for my site and I'm having some trouble finding a function.

 

To explain the function, you need to know that user can choose to display "any amount of rows per page"

 

I'm using an index to determine which page user are on.  For instance if rows per page is 2 and index is 0 then I'm on page 1. Basically here is two examples:

 

Per page: 2

IndexPage

01

22

43

64

 

Per page: 5

IndexPage

01

52

103

154

 

Ok, so here's the thing: say if index is 14 and per page is 2 and then I change to per page 10, well then we can't use 14 as index anymore, since valid index is only 0,10,20,30 and so on... I would need to make 14 --> 10

 

Now, I'm not sure what is the best way to calculate this....or what functions to use?

Link to comment
Share on other sites

Thanks, but I already got a working pagination class.. I just need this specific case to work

 

function calculatevalue($index,$total_rows,$per_page) {
            if ($per_page > $index)
            {
                return 0;
            }
            else
            {
                // Need code here to solve issue...
                return $index;
            }
}

Link to comment
Share on other sites

Yes, I guess you're right about that,

 

so far what I worked out is this formula, but to be honest I'm not that great with math, and there seems to be a lot of issues and it's only half working:

/**
* This function returns the new "index" value to be used based on the new "per page" value
*/
function calculatevalue($index,$total_rows,$per_page,$old_per_page)
{
            if ($index > $total_rows)
            {
                return 0;
            }
return ceil(($index+$old_per_page)/$total_rows)*$per_page;
}
    

Link to comment
Share on other sites

I'm not that great with math

 

The Math involved is of the level 1 + 1 and you can do that, can you?

 

What does calculateValue() do? Return a page number?

 

function calculateValue($index, $total_rows, $per_page) {
  if (0 > $index) return 0;
  if ($index > $total_rows) return $total_rows - 1;
  return ceil($total_rows/$per_page);
}

Link to comment
Share on other sites

It sounds like you are tying to derermine the page based upon the starting index. That's sbackwards. You should use the page number and records per page to determine the starting index:

 

$startingIndex = ($page-1) * $recordsPerPage );

 

Although, If you have a current index and the records per page changes, you can determine the new page to display (based upon the previously viewed index) as follows:

$page = floor(($oldIndex+1) / $recordsPerPage);
$startingIndex = ($page-1) * $recordsPerPage );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.