Hello. Such a problem: I can not make navigation in php.

In general, the situation is this: There is a page where there are two GET variables $ _GET ['id'] and $ _GET ['page'] The first variable is responsible for the user id, it may not be (If not present, it specifies its page). The second variable is responsible for displaying messages from the database. Here is my question: How to add to $ _GET ['page'] + 10, but do not lose $ _GET ['id'], and if there is no $ _GET ['id'] variable, then only specify $ _GET ['page'] and add 10.

Example: http://site.com/index.php?id=5&page=20 or http://site.com/index.php?page=20 You need to generate a link in the link <a href="Selected Link"> TEST </a>

It seems to be all logical, but it does not work. Who knows how to implement? Thank you in advance.

    1 answer 1

    I usually use my bike function:

    function formGet($str, $k, $v = ''){ if(is_array($k)) foreach($k as $k2 => $v2) $str = formGet($str, $k2, $v2); else if(!strlen($str)) $str .= '?&'.$k.'='.$v; else { if(strpos($str, '?') === false) $str .= '?&'.$k.'='.$v; else if(preg_match('/&?'.$k.'=/', $str)) $str = preg_replace('/(&)?'.$k.'=[^&]+/', '$1'.$k.'='.$v, $str); else $str .= '&'.$k.'='.$v; } return $str; } if(!isset($_GET['page']) || $_GET['page'] < 10) $_GET['page'] = 10; $link = formGet($_SERVER['REQUEST_URI'], 'page', $_GET['page']+10); 
    • Thank. But it still does not work much correctly. Suppose if the link is <a href="/profile/<?=$link?> "> test </a> then the link is generated site.com/profile/profile?id=5&page=20 , If you remove the link <a href = "/ <? = link?>"> test </a>, then the link site.com/?id=5&page=20 is formed, but I could not find what it depends on. - Elected
    • That's right. The function does not touch the path links - only get-parameters. - ling
    • Then why does it duplicate the link "profile / profile? Id = 5 ..."? - Elected
    • one
      Because at you in href'e links one more profile is added. - ling
    • if you don’t add a profile: sait.com/?id=5&page=20 Perhaps the profile counts $ _SERVER ['REQUEST_URI'] - Favorite