Saqueib
Saqueib

Reputation: 3520

how to build hyperlink for query string in php

I have a question, How I can add another get variable in my current url

 book.php?action=addressbook

i want to add

 book.php?action=addressbook&page=2

how to generate hyperlink for this, i have tried it using $_SERVER['PHP_SELF'] but query string are not included in url its showing something like that

book.php?page=2

I want to append my other variables to query string

Please help

Upvotes: 0

Views: 2208

Answers (2)

alex
alex

Reputation: 490213

$get = $_GET;

$get['page'] = 2;

echo '<a href="book.php?<?php echo http_build_query($get); ?>">Page 2</a>';

Upvotes: 2

KJYe.Name
KJYe.Name

Reputation: 17169

You can also use http_build_query(); to append more params to your URL

$params = $_GET;
$params["item"] = 45;
$new_query_string = http_build_query($params);

for instance:

$data = array('page'=> 34,
              'item' => 45);

echo http_build_query($data); //page=34&item=45
or include amp

echo http_build_query($data, '', '&amp;');  //page=34&amp;&item=45

Upvotes: 2

Related Questions