Home >Backend Development >PHP Tutorial >A brief summary about PHP

A brief summary about PHP

WBOY
WBOYOriginal
2016-07-25 09:02:101014browse

We have all encountered this situation: a WEB application page has many search conditions, and sometimes one parameter corresponds to many values, or there is a strong superior-subordinate relationship, such as Rental Network: House type parameters include: one bedroom and one living room, one bedroom and two living rooms, etc. At this time, it is more troublesome to add the correct URL parameters to a certain plug selection conditioner. In order not to do repetitive work all day long, we wrote the following method. As an example, we use the example of Rental Network for reference (note that the following is only a demonstration, so there is no specification). Area: no limit (''), area one (area1), area two (area2), area three (area3)... Rent: no limit (''), less than 500 (rent1), 500-800 (rent2)... Type (style): no limit (''), apartment (style1), ordinary residence (style2).... Then the corresponding a tag href value is: ">No limit ">District 1 ">District 2 ….. ">No limit ">Apartment ">Ordinary residence These labels must be output dynamically in practice. As long as the values ​​of key (request parameter) and value (request parameter) are consistent, it's ok :). Corrections welcome(More here: http://fc-lamp.blog.163.com/blog/static/17456668720128275633639/ ).


  1. /**
  2. *
  3. * Search URL string processing
  4. * @author: fc_lamp
  5. * @blog: http://fc-lamp.blog.163.com/
  6. * @param str $key main parameter
  7. * @param str $value Parameter value
  8. * @param str $query The search string (this value is generally ignored)
  9. * @param str $page_key Page number parameter (during paging, the page number will be passed to GET, so it needs to be deleted)
  10. * @internal
  11. * If there is a key in the character, the key in the original string will be deleted and a new value will be assigned.
  12. * If the input value of the character key is NULL, the KEY will be deleted.
  13. */
  14. function query_str($key, $value = '', $query = '', $page_key = 'page')
  15. {
  16. $query = empty ( $query ) ? $_SERVER ['QUERY_STRING'] : $query;
  17. if (empty ( $query ))
  18. {
  19. $query = "?$key=" . urlencode ( "$value" );
  20. } else
  21. {
  22. parse_str ( $query, $q );
  23. if (isset ( $q [$page_key] ))
  24. {
  25. unset ( $q [$page_key] );
  26. }
  27. if (isset ( $q [$key] ))
  28. {
  29. unset ( $q [$key] );
  30. }
  31. if ($value === NULL)
  32. {
  33. $query = '?' . http_build_query ( $q );
  34. } else
  35. {
  36. if (! empty ( $q ))
  37. {
  38. $query = '?' . http_build_query ( $q ) . "&$key=" . urlencode ( "$value" );
  39. } else
  40. {
  41. $query = "? $key=" . urlencode ( "$value" );
  42. }
  43. }
  44. }
  45. return $query;
  46. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn