Jump to content

can anyone explains this snippet in the pagination


medicalboy

Recommended Posts

this is a part of a class of aguestbook with paginationbut i can't understand well this mathematical procedure as i am anewbie

 var $itemsPerPage = 5;  $page = isset($_GET['page']) ? $_GET['page'] : 1;$startItem = ($page-1)*$this->itemsPerPage;if (($startItem + $this->itemsPerPage) > sizeof($list)) $endItem = sizeof($list); else $endItem = $startItem + $this->itemsPerPage;

can anyone explains this in detail?,and i'll be appreciated.........whay the start item is as like that?and this if coded like that?

Link to comment
Share on other sites

this is a part of a class of aguestbook with paginationbut i can't understand well this mathematical procedure as i am anewbie
 var $itemsPerPage = 5;  $page = isset($_GET['page']) ? $_GET['page'] : 1;$startItem = ($page-1)*$this->itemsPerPage;if (($startItem + $this->itemsPerPage) > sizeof($list)) $endItem = sizeof($list); else $endItem = $startItem + $this->itemsPerPage;

can anyone explains this in detail?,and i'll be appreciated.........whay the start item is as like that?and this if coded like that?

well, the first item initializes the value of the "viewport" or the number of items to show at once. It's saved as a variable to make it easier to configure if they wanted to change it something else.$page is set to either a value defined in the GET array, or if it doesn't exist, it will default to page 1. The syntax used is that of the ternary operator, which is essentially a short hand if/else statement.$startItem is initialized to a value that will act as an offset based on the page the viewer is on, using array indexing (where the first item is 0). So if you are on page 1, the first item is (1-1)*5 = 0. page 2 would be (2-1)*5 = 5. (where five would actually be the sixth index in the list: 0,1,2,3,4,5).the if/else statement might be easier understood when opened up.
if (($startItem + $this->itemsPerPage) > sizeof($list)){ $endItem = sizeof($list);} else{ $endItem = $startItem + $this->itemsPerPage;};

the if clause is just a way to define what the last (end) item shown will be. If the $startItem index plus the $itemsPerPage (viewport) will be greater than the actual amount of items to be shown, the $endItem is set to the last item in the list. else, the $endItem is equal to $startItem + $itemsPerPage (viewport).edit: this-> is just a way to reference a variable defined within a particular scope. I don't think it was needed here, but it will still work.

Link to comment
Share on other sites

well, the first item initializes the value of the "viewport" or the number of items to show at once. It's saved as a variable to make it easier to configure if they wanted to change it something else.$page is set to either a value defined in the GET array, or if it doesn't exist, it will default to page 1. The syntax used is that of the ternary operator, which is essentially a short hand if/else statement.$startItem is initialized to a value that will act as an offset based on the page the viewer is on, using array indexing (where the first item is 0). So if you are on page 1, the first item is (1-1)*5 = 0. page 2 would be (2-1)*5 = 5. (where five would actually be the sixth index in the list: 0,1,2,3,4,5).the if/else statement might be easier understood when opened up.
if (($startItem + $this->itemsPerPage) > sizeof($list)){ $endItem = sizeof($list);} else{ $endItem = $startItem + $this->itemsPerPage;};

the if clause is just a way to define what the last (end) item shown will be. If the $startItem index plus the $itemsPerPage (viewport) will be greater than the actual amount of items to be shown, the $endItem is set to the last item in the list. else, the $endItem is equal to $startItem + $itemsPerPage (viewport).edit: this-> is just a way to reference a variable defined within a particular scope. I don't think it was needed here, but it will still work.

awesome explaination!! u rock it,i learned anew things from your wordsi understand this well now,thanks.......
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...