The following code can be used throughout your existing PHP classes. If you use a similar URI pattern, you won't have any issues with instantly plugging the methods into your existing code. Otherwise, you may have to tweak them slightly.
To generate a link, you'll call the generateLink() method from your file and pass either NULL or an associative array with key/value pairs to add onto the URI.
i.e.
Example URI: http://example.com/index.php?page=view_by_size&type=0&id=10&xxx=something
$this->_frequent->generateLink(array("type" => "2"));
Read the comments above each method to understand what's happening:
To generate a link, you'll call the generateLink() method from your file and pass either NULL or an associative array with key/value pairs to add onto the URI.
i.e.
Example URI: http://example.com/index.php?page=view_by_size&type=0&id=10&xxx=something
$this->_frequent->generateLink(array("type" => "2"));
Read the comments above each method to understand what's happening:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Frequent { | |
/** | |
* String query to associative array | |
* | |
* Grabs the URI string from the address bar | |
* Since all of my pages start with a ?page=__something__, I split the uri string into 2 segments and at that point | |
* and discard the http://example.com (i.e. $uri[0]) | |
* Split the remainder of into it's own segments using the & sign as the delimiter | |
* Initialize the $segments array and assign the "page" key to it (again, since ?page=__something__ will always be the first portion of the URI string | |
* Remove the page value from the $uri array since we don't need it any more | |
* | |
* Loop through the remainder of the $uri values and split them using the = sign as the delimiter. Store into $temp variable | |
* - i.e. $uri will look like this at this point: | |
* array("type=2", | |
* "id=10", | |
* "xxx=something" | |
* ); | |
* | |
* Assign $temp[0] as the key to $segments array and $temp[1] as value to $segments array | |
* - i.e. $temp will look like this: | |
* array("type" => "2", | |
* "id" => "10", | |
* "xxx" => "something" | |
* ); | |
* | |
* The loop will also take care of any long strings that might have been created | |
* - i.e. ?page=__something__&type=2&id=10&xxx=something&xxx=something2&xxx=something3 | |
* - In this example, each xxx key will get overwritten with the last xxx | |
* | |
* Any additional segments are appended last: that is if an array of key => value pairs is passed | |
* - i.e. $add_segment = array("final" => "3"); | |
* In this example the final $segments array will look like this: | |
* array("type" => "2", | |
* "id" => "10", | |
* "xxx" => "something" | |
* "final" => "3" | |
* ); | |
* | |
* Also, since $add_segments is passed last, if the same key name is passed as one already assigned to segments, | |
* the already assigned key will get overwritten. | |
* | |
* @param $add_segment | Must be either NULL or Array | |
* @return array $segments; | |
*/ | |
public function uriToArray($add_segment = NULL) { | |
$uri = $_SERVER["REQUEST_URI"]; | |
$uri = explode("?page=", $uri); | |
unset($uri[0]); | |
$uri = explode("&", $uri[1]); | |
$segments["page"] = $uri[0]; | |
unset($uri[0]); | |
foreach($uri as $segment) { | |
$temp = explode("=", $segment); | |
$segments[$temp[0]] = $temp[1]; | |
} | |
if ($add_segment !== NULL) { | |
foreach($add_segment as $key => $value) { | |
$segments[$key] = $value; | |
} | |
} | |
return $segments; | |
} | |
/** | |
* Generates a link | |
* | |
* Grabs the segments from the uriToArray() method and passes any additional segments via $add_segment | |
* - i.e. $this->_frequent_obj->generateLink(array("type" => "2")); | |
* - If no additional segments, NULL will be passed | |
* Since ?page=__something__ is always the first GET variable, the $link string is initialized with the ?page=XXX | |
* The page key/value pair is unset from the $link_array variable since we don't want to pass it again | |
* The rest of the key => value pairs are run added to the $link string variable via &key=value | |
* The link is returned after all values have been concatenated | |
* | |
* @param array $add_segment | |
* @return string $link | |
*/ | |
public function generateLink($add_segment = NULL) { | |
$link_array = $this->uriToArray($add_segment); | |
$link = "?page=" . $link_array["page"]; | |
unset($link_array["page"]); | |
foreach($link_array as $key => $value) { | |
$link .= "&" . $key . "=" . $value; | |
} | |
return $link; | |
} | |
} |
Comments
Post a Comment