Warz Posted May 25, 2010 Share Posted May 25, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/ Share on other sites More sharing options...
ignace Posted May 25, 2010 Share Posted May 25, 2010 http://www.phpfreaks.com/forums/index.php/topic,298487.msg1413445.html#msg1413445 Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/#findComment-1062996 Share on other sites More sharing options...
Warz Posted May 25, 2010 Author Share Posted May 25, 2010 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/#findComment-1063003 Share on other sites More sharing options...
ignace Posted May 25, 2010 Share Posted May 25, 2010 if ($per_page > $index) Does not make any sense. Shouldn't it be: if ($index > sizeof($total_rows)) Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/#findComment-1063127 Share on other sites More sharing options...
Warz Posted May 25, 2010 Author Share Posted May 25, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/#findComment-1063164 Share on other sites More sharing options...
ignace Posted May 26, 2010 Share Posted May 26, 2010 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); } Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/#findComment-1063410 Share on other sites More sharing options...
Psycho Posted June 1, 2010 Share Posted June 1, 2010 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 ); Quote Link to comment https://forums.phpfreaks.com/topic/202834-need-help-to-find-function-to-use-with-pagination/#findComment-1066233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.