We can now show pages of records based on the page number.
However we obviously don't want the page number hard coded in this script.
Instead let's get it from the query string.
If I add a question mark to the you r l followed by a page variable.
We can access the value of this variable in the global get an array like this.
So then if I change the value in the query string the page of results changes.
However if I remove the query string
we get an error.
This is B Carson elements with the index of page doesn't appear in the get array to fix this.
First we need to check if it's set.
And if it is we can use it.
If not we default to page 1.
So now in the browser if we don't put the page we get page one and if we do.
We get the page specified.
We can improve this code though.
The ternary operator allows us to return a value based on a boolean condition.
So instead of this entire IF statement we can use the ternary operator directly.
When we create a new object what the ternary operator does here is if this is true use this value and
if it's false.
Use this one.
So without a query string this defaults to page 1 as before.
If I add a query string with the page number then it uses that value for the page.
We can improve this even further though.
A new feature that was added in p-h P7 was the null coalescing operator.
This was added specifically for when we're using is set with the ternary operator.
So now we can replace this with this.
If this value exists then this value will be used.
If not then what follows the double question mark will be used instead.
So without a query string this still defaults to page 1 as before.
And if I add a query string.
Then again it uses that value for the page.