开发者

Multiple GETs in URL at the same time?

I'm trying to GET variables out of an URL, but everytime the URL already has a query, it gets overwritten by the next query. Example: I have a link;

<a href="?page=34">Page 34</a>

When you click on that link this link becomes visible;

<a href="?item=45">Item 45</a>

But when I click that link, the other one get overwritten so the URL looks like;

www.domainname.ext/?item45

But I want it to look like;

www.domainname.ext/?page=34&item45

Any way to do开发者_如何学JAVA that? Thanks in advance!


From within a given "page", you need to store the page ID and use that stored value when you create "item" links.

<?php
$pageID = $_GET['page'];

// ....

?>

<a href="?page=<?php echo $pageID; ?>&amp;item=45" title="Item 45">Item 45</a>


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

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

PHP http_build_query

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


<a href="?page={$page->id}&amp;item={$item->id}">
    Page {$page->id}, Item {$item->id}
</a>


When outputting the links, you'll have to include all of the relevant query string parameters; there's no automatic "merge".

If you could change your server-side stuff to using RESTful URLs, you could get this sort of behavior. E.g., starting with

http://www.domainname.ext

this link

<a href='page34'>Page 34</a>

would take you to

http://www.domainname.ext/page34

after which this link

<a href='item43'>Item 43</a>

would take you to

http://www.domainname.ext/page34/item43

...but that requires quite a big change in your server-side stuff.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜