Voting

: max(seven, three)?
(Example: nine)

The Note You're Voting On

zappascripts at gmail com
7 years ago
Here's a simple class I made that makes use of this parse_url.
I needed a way for a page to retain get parameters but also edit or add onto them.
I also had some pages that needed the same GET paramaters so I also added a way to change the path.

<?php
class Paths{

private
$url;
public function
__construct($url){
$this->url = parse_url($url);
}

public function
returnUrl(){
$return = $this->url['path'].'?'.$this->url['query'];
$return = (substr($return,-1) == "&")? substr($return,0,-1) : $return;
$this->resetQuery();
return
$return;
}

public function
changePath($path){
$this->url['path'] = $path;
}

public function
editQuery($get,$value){
$parts = explode("&",$this->url['query']);
$return = "";
foreach(
$parts as $p){
$paramData = explode("=",$p);
if(
$paramData[0] == $get){
$paramData[1] = $value;
}
$return .= implode("=",$paramData).'&';

}

$this->url['query'] = $return;
}

public function
addQuery($get,$value){
$part = $get."=".$value;
$and = ($this->url['query'] == "?") ? "" : "&";
$this->url['query'] .= $and.$part;
}

public function
checkQuery($get){
$parts = explode("&",$this->url['query']);

foreach(
$parts as $p){
$paramData = explode("=",$p);
if(
$paramData[0] == $get)
return
true;
}
return
false;

}

public function
buildQuery($get,$value){
if(
$this->checkQuery($get))
$this->editQuery($get,$value);
else
$this->addQuery($get,$value);

}

public function
resetQuery(){
$this->url = parse_url($_SERVER['REQUEST_URI']);
}




}
?>

Useage:

Test.php?foo=1:

<?php
$path
= new Paths($_SERVER['REQUEST_URI']);
$path->changePath("/baz.php");
$path->buildQuery("foo",2);
$path->buildQuery("bar",3);
echo
$path->returnUrl();
?>

returns: /baz.php?foo=2&bar=3

Hope this is of some use to someone!

<< Back to user notes page

To Top