On my home site it has pagination but this pagination only works when there is a page variable set in the URL, e.g. http://www.example.com/index?page=1
However if the user just lands on my index page it won't have the page variable set and the results on my page wont display as no page number has been set, so I wanted to know if there is a way of setting the URL variable when the user just lands on the page and the variable isn't set?
define a variable for
$page
for default value and use isset
that u may use for getting $_GET[] values if exist or not
if(isset($_GET['page'])){
$page = $_GET['page']
}
else{
$_GET['page'] = //set default value for page..
}
I would recommend doing something like:
$page = 1;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
Instead of using $_GET['page']
all over your code - use $page
A few things you might consider...
// php will evaluate to false for the empty string as well as zero, so this forces a set to zero
if (!$_REQUEST["page"])
$_REQUEST["page"] = 0;
alternatively...
// check if the variable is set first...
if (!isset($_REQUEST["page"]))
$_REQUEST["page"] = 0;
You could also do the same for the $_GET
array if that's what you're using.
Something like this should work
$options = array('options' => array(
'default' => 1, // value to return if the filter fails
'min_range' => 1
));
if(isset($_GET['page'])){
$page = filter_var((int)$_GET['page'], FILTER_VALIDATE_INT, $options);
}else{
$page = 1;
}