Reputation: 417
I am thing of a way to pass about 15 parameters to a url pagination:
for example I have:
$input = $_GET['input'];
$categories = $_GET['category'];
$state = $_GET['state'];
$zipcode = $_GET['zipcode'];
I could do it this way and works fine:
$paginate.= "<a href='$targetpage?page=$prev&input=".$_GET['input']."&
category=".$_GET['category']."&state=".$_GET['state']."&
zipcode=".$GET['zipcode']."'>Previous</a>";
But I have a lot more Parameters to pass. Could someone show me how it's done using an array or anything better?
Thank you
Upvotes: 5
Views: 3483
Reputation: 1668
Assuming all the $_GET
keys are the same as the parameter names in the URL, you could do this:
$url = $targetpage . '?page=' . $prev . '&';
foreach ($_GET as $k => $v) {
$url .= $k . '=' . $v . '&';
}
$paginate.= "<a href='$url'>Previous</a>";
.. Or you could just use PHP's inbuilt function http_build_query()
Upvotes: 0
Reputation: 5136
Yo can use this function:
http_build_query
php.net example:
<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
Upvotes: 5
Reputation: 197684
A function that turns an array into a URL query is available in PHP, it is called:
The usage is pretty straight forward:
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
The above example will output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
I recommend the manual page just linked for more information.
If you're looking for something object-oriented, the Net_URL2
Pear Package is useful.
It also allows to change some parameters conditionally, which is normally very useful for pagination, see my answer to "keeping url parameters during pagination" for two examples, one Array/PHP standard extension based, and another one for Net_URL2
.
Upvotes: 3