Posted By

simplistik on 10/01/08


Tagged

String query update preserve


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

pixelsoul


Preserve & update/rebuild query string


 / Published in: PHP
 

Allows you preserve and update a query string and it's existing values easily, instead of constantly appending the new value to the end of the query string

  1. <?php
  2. /**
  3. * Name: Preserve and update/rebuild query string<br>
  4. * @param Example:
  5. * Example URL: http://www.site.com/?category=foo&order=desc&page=2
  6. *
  7. * <a href="<?php echo queryString('order','asc'); ?>">Order ASC</a>
  8. *
  9. * Output HTML: <a href="?category=foo&amp;order=asc&amp;page=2">Order ASC</a>
  10. * Output URL: http://www.site.com/?category=foo&order=asc&page=2
  11. *
  12. * Not http://www.site.com/?category=foo&order=desc&page=2&order=asc
  13. */
  14. function queryString($str,$val)
  15. {
  16. $queryString = array();
  17. $queryString = $_GET;
  18. $queryString[$str] = $val;
  19. $queryString = "?".htmlspecialchars(http_build_query($queryString),ENT_QUOTES);
  20.  
  21. return $queryString;
  22. }
  23. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: pixelsoul on August 23, 2013

I like this. The only change I would make is the ability to add multiple parameters and values at one time.

You need to login to post a comment.