Jump to content

resolving an offset value


eladchen

Recommended Posts

Hey, im using MSSQL server ( :wtf: 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 :P 

Link to comment
Share on other sites

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;
}

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.