The Little Guy Posted June 7, 2011 Share Posted June 7, 2011 I have a method that I can't seem to get to work. public function query_remove($key){ $str = ""; $key_value = explode("&", $query_string); foreach($key_value as $kv){ list($k, $v) = explode("=", $kv); if($key != $k) $str .= "&$k=$v"; } $this->url_query = ltrim($str, "&"); return $this; } Basically I want to take the current URL Query String (GET) and remove elements from it. I can get it to remove one element, but I can't remove 2+. Query String: elementA=valueA&elementB=valueB&elementC=valueC Works: $myclass->query_remove('elementA'); New String: elementB=valueB&elementC=valueC Doesn't Work: $myclass->query_remove('elementA')->query_remove('elementB'); New String: elementA=valueA&elementC=valueC As you can see, elementA is still there, but should have been removed. Any suggestions how I could get this to work the way I want? I also would like it to work individually, so I could call it 10 different times if I wanted. // Original String: elementA=valueA&elementB=valueB&elementC=valueC echo $myclass->query_remove('elementA')->query_remove('elementB'); //now: elementC=valueC echo $myclass->query_remove('elementA')->query_remove('elementC'); //now: elementB=valueB echo $myclass->query_remove('elementB')->query_remove('elementC'); //now: elementA=valueA echo $myclass->query_remove('elementB') //now: elementA=valueA&elementC=valueC Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted June 7, 2011 Share Posted June 7, 2011 That's an awful method you are using. You can add this to your class. The function param is an array of the elements that you wish to remove: <?php function query_remove($string, array $elements) { $parts = explode('&', $string); $new_query = array(); foreach($parts as $param_val) { $param_val_parts = explode('=', $param_val); if(!in_array($param_val_parts[0], $elements)) { $new_query[] = $param_val; } } return implode('&', $new_query); } $string = 'elementA=valueA&elementB=valueB&elementC=valueC'; echo "<p>".query_remove($string, array('elementA','elementC'))."</p>"; echo "<p>".query_remove($string, array('elementB','elementC'))."</p>"; echo "<p>".query_remove($string, array('elementA'))."</p>"; ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 7, 2011 Share Posted June 7, 2011 Also, why all of explode, loop, concating? Rough example, maybe add neil's suggestion to it: parse_str($query_string, $array); unset($array[$key]); $new_query_string = http_build_query($array); Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 7, 2011 Author Share Posted June 7, 2011 I would like to do it the way I have because then I can add items to it too. echo $myclass->query_remove('elementA')->query_remove('elementB')->query_add('elementD', 'valueD'); //now: elementC=valueC&elementD=valueD Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 7, 2011 Share Posted June 7, 2011 public function query_remove($key) { parse_str($this->url_query, $get_array); unset($get_array[$key]); $this->url_query = http_build_query($get_array); return $this; } public function query_add($key, $val) { parse_str($this->url_query, $get_array); $get_array[$key] = $val; $this->url_query = http_build_query($get_array); return $this; } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 7, 2011 Share Posted June 7, 2011 But personally I would keep it as an array in the object and convert it to a query string when needed: public function __tostring() { return http_build_query($this->get_array); } public function query_remove($key) { unset($this->get_array[$key]); return $this; } public function query_add($key, $val) { $this->get_array[$key] = $val; return $this; } echo $myclass->query_remove('elementA')->query_remove('elementB')->query_add('elementD', 'valueD'); Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 7, 2011 Author Share Posted June 7, 2011 But personally I would keep it as an array in the object and convert it to a query string when needed: public function __tostring() { return http_build_query($this->get_array); } public function query_remove($key) { unset($this->get_array[$key]); return $this; } public function query_add($key, $val) { $this->get_array[$key] = $val; return $this; } echo $myclass->query_remove('elementA')->query_remove('elementB')->query_add('elementD', 'valueD'); Where/what is get_array? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 7, 2011 Share Posted June 7, 2011 It is just my replacement for $this->url_query since it's not a query string I renamed it. How you initialize it depends on how you were initializing $this->url_query. In the constructor? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 7, 2011 Author Share Posted June 7, 2011 Cool thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.