I would like to display values for "Showing `x` to `y` out of `z` entries" on a search result page, but I can't seem to get the math right.

Here are some of my variables:

```
// Total number of results
$total = mysqli_num_rows($rs);

// Items per page
$item_per_page = 3;

// Page number
$page_number = x;

// break records into pages
$total_pages = ceil($get_total_rows[0] / $item_per_page);

// get starting position to fetch the records
$page_position = (($page_number - 1) * $item_per_page);

```

Here's what I tried:

```
echo $page_position . ' to ' . ($page_position + $item_per_page) . ' out of ' . $total;

```

But let's say I have 11 records, displaying 3 on each page. On the 4th page there are only 2 results, but with my code above, it will say `"9 to 12 out of 11"`.

Can someone help me out with some formula to calculate number of entries shown on a given page?