4

This is what I have

echo '<button><a href="'.$_SERVER['REQUEST_URI'].'&startrow='.($startrow+100).'">Next 100</a></button>';

I have 2 parameters in my url one is campaign, second is startrow, how can I just remove 'startrow' parameter and its value from the url. I want something like this.

echo '<button><a href="'.REMOVE &startrow=x($_SERVER['REQUEST_URI']).'&startrow='.($startrow+100).'">Next 100</a></button>';

When I press the next 100 button, the page reloads and it adds startrow parameter in the url hence if I pressed next 100 button 5 times, I have 5 startrow paramter in the url, so I want to remove the previous startrow parameter first then add the new one

My actual url: enquirytable.php/campaign=all&startrow=100

When I click on Next 100 button: enquirytable.php/campaign=all&startrow=100&startrow=200

What I want: enquirytable.php/campaign=all&startrow=200

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Shujaat Shaikh
  • 189
  • 1
  • 5
  • 17

4 Answers4

5

Probably the long one, but an accurate way. Here we are using parse_url and parse_str to achieve desired output.

Try with an example

<?php

$url="http://www.example.com/some/path?startrow=100&campaign=abc";
echo change_url_parameter($url, "startrow", 200);

function change_url_parameter($url,$parameterName,$parameterValue) {
    $url=parse_url($url);
    parse_str($url["query"],$parameters);
    unset($parameters[$parameterName]);
    $parameters[$parameterName]=$parameterValue;
    return  sprintf("%s://%s%s?%s", 
        $url["scheme"],
        $url["host"],
        $url["path"],
        http_build_query($parameters));
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • sir, this is just removing the previous paramater but not adding it again, – Shujaat Shaikh Nov 09 '17 at 07:54
  • @ShujaatShaikh this will remove the parameter which you want to remove and for adding that parameter you can append a parameter which you want to attach. and thats it. – Sahil Gulati Nov 09 '17 at 07:55
  • I have edited my question in the best possible way sir, can you plz have a look? – Shujaat Shaikh Nov 09 '17 at 07:58
  • @ShujaatShaikh I have updated a post in which i have defined a function where you can change url's parameter value i think that can be helpful and work for sure. – Sahil Gulati Nov 09 '17 at 08:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158580/discussion-between-sahil-gulati-and-shujaat-shaikh). – Sahil Gulati Nov 09 '17 at 08:03
  • I guess return $url["scheme"].'://'.$url["host"].$url["path"]."?".http_build_query($parameters); is better – Vit Sep 21 '21 at 11:58
  • @Vit Thanks for suggestion :) Improved old post, with a more appropriate way. – Sahil Gulati Sep 21 '21 at 15:20
0

Building the URL that way is not clean, instead of having to remove some parameters, you should start from a clean route then add those needed.

You can do something like :

echo '<button><a href="'.$_SERVER['PHP_SELF'].'&startrow='.($startrow+100).'">Next 100</a></button>';

If you really want to do it with $_SERVER['REQUEST_URI'] this should work :

function getUrl($startRow) {
    $url = preg_replace('@&startnow=[\d]+@', '', $_SERVER['REQUEST_URI']);
    return $url.'&startrow='.$startRow;
}

// ...

echo '<button><a href="'.getUrl($startrow+100).'">Next 100</a></button>';
0

Sahil answer update, so function can consume parameters with dot and keep url fragments. PHP 7.4.

"url": "https://www.zzz.ccc/?zzz.er=ty&aa.dd=vv#123",
    "param_to_change": "zzz.er",
    "old_behave": "https://www.zzz.ccc/?zzz_er=ty&aa_dd=vv&zzz.er=4444",
    "new_behave": "https://www.zzz.ccc/?zzz.er=4444&aa.dd=vv#123"

public function change_1_url_param($url,$parameter,$parameterValue)
{
    $url_init = $url;
    $url=parse_url($url);
    parse_str($url["query"],$parameters);
    unset($parameters[$parameter]);
    $parameters[$parameter]=$parameterValue;

    $url_fragment = '';
    if(isset($url['fragment']))
       $url_fragment = '#'.$url['fragment'];

    if(mb_stripos($parameter,'.')!==false){
        $url_q = $url["query"];
        $explode_0 = explode($parameter.'=',$url_q);
        if(!isset($explode_0[1]))
            return $url_init;
        $explode_1 = explode('&',$explode_0[1]);
        $u = str_replace( $explode_1[0] , $parameterValue , $url_q );
        $u = $url["scheme"].'://'.$url["host"].$url["path"]."?".$u.$url_fragment;
    }
    else
        $u = $url["scheme"].'://'.$url["host"].$url["path"]."?".http_build_query($parameters).$url_fragment;

    return $u;
}
Vit
  • 396
  • 2
  • 7
  • 16
0
function update_params($update) {
    return $SERVER['SCRIPT_NAME'].'?'.http_build_query(array_merge($_GET, $update));
}

echo update_params(['startrow', $startrow+100]);
tbodt
  • 16,609
  • 6
  • 58
  • 83