Skip to main content

PHP Generate Links

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:


<?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

Popular posts from this blog

Beginner Java Exercise: Sentinel Values and Do-While Loops

In my previous post on while loops, we used a loop-continuation-condition to test the arguments. In this example, we'll loop at a sentinel-controlled loop. The sentinel value is a special input value that tests the condition within the while loop. To jump right to it, we'll test if an int variable is not equal to 0. The data != 0 within the while (data != 0) { ... } is the sentinel-controlled-condition. In the following example, we'll keep adding an integer to itself until the user enters 0. Once the user enters 0, the loop will break and the user will be displayed with the sum of all of the integers that he/she has entered. As you can see from the code above, the code is somewhat redundant. It asks the user to enter an integer twice: Once before the loop begins, and an x amount of times within the loop (until the user enters 0). A better approach would be through a do-while loop. In a do-while loop, you "do" something "while" the condition...

Creating your own ArrayList in Java

Wanted to show that certain data structures in Java can be created by you. In this example, we'll go ahead and create an ArrayList data structure that has some of the methods that the built in ArrayList class has. We'll create 2 constructors: The default constructor that creates an ArrayList with a default size of 10. Constructor that allows an initial size to be passed to the array. We'll also create a number of methods: void add(Object x);  A method that allows you to place an Object at the end of the ArrayList. void add(int index, Object x);  A method that allows you to place a value at a given location. Object get(int index):  Allows you to retrieve a value of the arrayList array from a given location. int size();  Allows you to get the number of elements currently in the Arraylist. boolean isEmpty();  Tests to see if the Arraylist is empty. boolean isIn(Object x);  A method that sees if a particular object exist in the arrayList. int ...

Laravel 6.x with React and react-router

This will get you started on getting your first React/Laravel application deployed to your server. We'll cover everything from installation to deployment. Start by reading the installation instructions on  https://laravel.com/docs/6.x#installing-laravel . We'll cover those details below. Setting Up Laravel Check that you have the latest version of PHP installed on your computer.  It must be >= 7.2.0. Open terminal to get the Laravel installation tool. Type in composer global require laravel/installer Type in laravel to verify installation. Navigate to a directory on your computer where you want to install your project on your terminal. Run the following command: laravel new project_name (replace project_name with your project name). Once complete, cd into your new project. Type the following command: php artisan serve. You'll get a message like the following if it's running successfully: Laravel development server started: http://127.0.0.1:8000 ...