eladchen Posted March 15, 2012 Share Posted March 15, 2012 Hey, im using MSSQL server ( i know ) im trying to build pagination class for my photos page this is the query im using to retrive data $sql_query = "WITH Photos AS (SELECT ROW_NUMBER() OVER(ORDER BY id) "; $sql_query .= "AS RowNumber, id, filename, type, size, caption FROM photographs) "; $sql_query .= "SELECT * FROM Photos WHERE RowNumber BETWEEN {$pagination->offset_start()} AND {$pagination->offset_start()}"; the query works fine im using 3 veriables to keep track of $current_page for the current page number, $per_page for the results per page i want to display, $total_count for the total items; im using two functions - $pagination->offset_start() and $pagination->offset_end() if the result per page veriable is 3 then offset_start() should return 0 and offset_end() should return 3 then when im on page number 2 they should return 4 to 7 hence 3 results per page.. and if for example $per_page is 36 then it should return 0 to 36 on the first page on the seconed page it should return 37 to 73. i cant figure out the math and will love some help please. i hope to god it's nothing simple or else i'l feel like a complete idiot.. P.S sorry for any bad english Quote Link to comment https://forums.phpfreaks.com/topic/259019-resolving-an-offset-value/ Share on other sites More sharing options...
eladchen Posted March 16, 2012 Author Share Posted March 16, 2012 SOLVED - using this public function offset_start() { // in case $per_page is 5 - first value will be 1 // seconed value will be 6, third will be 11 and so on.. $offset_start = ($this->current_page - 1) * $this->per_page; return $offset_start + 1; } public function offset_end() { $offset_end = ($this->current_page - 1) * $this->per_page; return $offset_end + $this->per_page; } Quote Link to comment https://forums.phpfreaks.com/topic/259019-resolving-an-offset-value/#findComment-1327988 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.