Over the past couple weeks I realized there isn’t really much information about how dynamic web pages work. The lack of this information makes it harder for new developers to grasp these topics that are fairly simple, but may seem hard without a good example. So the next couple articles are going to cover some of these topics. If you have a topic you would like for me to cover be sure to let me know in the comments below.
On ward to Url Variables.
Url variables is one of the first topics to learn for a new developer wanting to move from static web sites to dynamic sites. Url variables, also called query strings in some languages(asp.net), allow a variable to be passed from one page to another with very little overhead and very little code.
We have all seen the sites, such as mine here, where they have a url such as corymathews.com/?id=12 or something similar. These extra items after the ? are called url variables and I am going to explain what these are, what are they used for, and how do they work?
What are url variables?
Url variables allow developers to pass multiple values from one page to another in order to display different results every time the same page is loaded. This saves the developer from having to make hundreds of pages which are exactly the same. This is mainly because doing so wouldn’t be very realistic or good for any budget.
How do url parameters work?
Take a look at the following link:
We see a couple different parts. We see the domain osguide.net a directory named Software the current page index.php and then we see a ?p=2. This ?p=2 is a url variable setting the variable “p” equal to 2. In this case its being used as a variable to store the current page for my pagination script on the site, but it could be for just about anything. When the page is first loaded I have programmed it page to look to see if the variable p is declared in the url bar. If it is declared then my page knows to display the corresponding page of results, in this case 2. On the other hand you must always take into account if the url parameter is not declared. In my example here if p was not in the url I would consider this page 1 and thus set p to 1.
A thing to note here is that this url can be rewritten as http://www.osguide.net/Software/?p=2 since the index.php is understood and not actually needed.
What if you want to pass multiple parameters? This can be easily accomplished, with the & symbol. For instance in the previous example If I also wanted to set the variable ‘R’ I would simple use a url like so:
This can go on and on for as long as you like. Take a look at some of the sites you go to such as gmail, they may pass quite a few different url variables, mainly depending on your browser. All you have to do is keep adding the & in between every variable.
So What are url parameters used for?
Probably most common use for url parameters is to associate the correct information to display from a database. The way this works logically is very simple. A page is set up kind of like a template for content without any “real” content in it. Then from the url variable passed in a query is run on a database and the related page’s contents are returned from the database to be displayed on the page. This allows a developer to create one page to show the details of anything, such as this article, without having to create a different page for every article.