Preserve & update/rebuild query string


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.