简体   繁体   中英

PHP: Using ?… after file.php

I have this:

pm.php?v=unread

Now in unread, you can filter too by pressing Messages or Invites, and the links look like this:

<a href="?f=msgs">Msgs</a> | <a href="?f=invites">Inv.</a>

If you are inside pm.php?v=unread, and press on Msgs, it will turn out as pm.php?f=msgs . And i wish it to be pm.php?v=unread&f=msgs

How can i do this?

If you want to maintain all your $_GET vars and add an extra to your links, consider array_merge() and http_build_query() .

$gets=array_merge($_GET,array('f'=>'msgs'));//returns array with your new values overriding any old values
$getstring=http_build_query($gets);
$link="<a href="?$getstring>Msgs</a>";
<a href="?<?=($_GET['v']=="unread"?'v=unread&':'')?>f=msgs">Msgs</a>

要么

<a href="?<?=($_GET['v']?'v='.$_GET['v'].'&':'')?>f=msgs">Msgs</a>
<a href="pm.php?f=msgs&v=<?php echo $_REQUEST['v']; ?>">Msgs</a>

You have to figure out all the get variables and add them to the URLs. For example you could do something like this in your pm.php:

$params = ((isset($_GET['v'])?'v=' . $_GET['v']:'');
$params .= //do the same for all your expected variables

In the page you would do something like:

<a href="?f=msgs&<?php echo $params; ?">Msgs</a> | <a href="?f=invites&<?echo $params; ?>">Inv.</a>

This is obviously short and dirty. Assumes you will always have params, etc.

This is a fun problem. Because what if you arent using any $_GET values, and you click messages? Then its going to go pm.php&f=msgs now here is my solution :)

function appendURL($name, $value) 
{ 
    // if we have a variable holding the '?' position
    if(strpos($_SERVER['REQUEST_URI'], '?')) 
    { 
        // append the value in the & position
        return $_SERVER['REQUEST_URI'] . "&$name=$value"; 

    } 
    // otherwise, append it in the ? position
    return $_SERVER['REQUEST_URI'] . "?$name=$value";
}

Now in your case

<a href="<?=appendURL('f', 'msgs')?>">Messages</a>

Haven't done this in a while, so don't crucify me if something is a little off! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM