In php I’m adding an extra URL segment ?sort to sort posts by ascending or descending.
<?php
//get current URL
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//filter URL and create sorting links
if(filter_var($url, FILTER_VALIDATE_URL))
{ ?>
<a href="<?php echo $url; ?>?sort=asc">Small</a>
<a href="<?php echo $url; ?>?sort=desc">Large</a>
<?php } ?>
The problem: every time I click a link the url segment repeats in the URL.
Example: http://mysite.com/another_segment/?sort=asc?sort=asc?sort=asc
To disallow repetition, how I replace the URL segment whenever I click one of the two sorting link? I don’t need to separate parameters with & symbols. I’m expecting just one delimiter. I’m using it in a pagination script so the parameter needs to stay in the URL when I browse pages.
I’m trying to avoid javascript as well
You should use the following function to remove the “sort” key in the existing URL and then append.
Your code should modify this way