If you pay attention to URL's you may notice those fancy URL's that are structured like with parts like ?ID=1 in them. This can be achieved in a number of ways, however some methods are complex, and some are relatively easy. In actual fact there's something in PHP called query strings and although it's not the best way to do it, you can use query strings to generate special structures like ID numbers and such. In this tutorial you will be show a simple way of being able to do this.
Step 1: Create your document
Create a blank PHP document called page.php, make sure you don't name it something different as the code will be based around the filename as page.php
Step 2: The query string code
Now the query string code is quite basic but will need some explaination. So differing to my other tutorials im going to lay the code down now and then go through each section and explain
<?
if(!$page){ $page = $HTTP_GET_VARS['ID']; }
if($page=="1"){
?>
This is content on query string 1
<?
}
elseif($page=="2"){
?>
This is content on query string 2
<?
}
elseif($page=="3"){
?>
This is content on query string 3
<?
}
else{
?>
Invalid ID specified.
<?
}
?>
This is the full code of the query string. We shall now break it down:
HTTP_GET_VARS
<?
if(!$page){ $page = $HTTP_GET_VARS['ID']; }
if($page=="1"){
?>
Hopefully you can see why naming your php document page.php was a good idea. Anyway first of all the query string will define what special structure you want to be able to add into your URL.
if(!$page){ $page = $HTTP_GET_VARS['ID']; }
The HTTP_GET_VARS query will be what defines the special structure in your URL. For this URL the special structure will follow a ID structure. This can be changed to your liking.
Making more query strings
In the code there are three query strings, you can add as many as you like but you must make sure you correctly create a new query string in the code.
<?
}
elseif($page=="2"){
?>
Notice the use of the elseif query, using simple numeracy skills you would number each string by number starting at 1 and ending at how many query string you like
Properly closing the query strings
PHP is a little more harse on code errors and will usually cause a blank white page to replace your lovely website with one line of text saying php error on line 84 or whatever. Bad times. In order to stop the query strings you must like shown in the above example of creating multiple query strings, you must number each string in a numeric fashion, but once you have all the query strings you want you must add this code at the end:
<?
}
else{
?>
Invalid ID specified.
The random "Invalid ID specified" text under this is not a error but infact acts as a error message.
Example:
A page is using query strings and there are 3 in total. Someone tries to access the 4th query string, but it doesn't exist. So the else query will recongise that a 4th query string doesn't exist and will display whatever's under the code. You don't have to put any text, but if you don't a user will be greeted with a blank page. So my recommendation would be to add some information about a invalid ID entered and suggest possible solutions to the problem.
Accessing the special URL structure
Okay I've gave you the explaination of the query strings, but how do you access the content via URL? First off all you will need to save the page.php document to a web-server of some sort. For this tutorial placing it in the directory that your index page is would be good.
Now to access the content you would now simply add this URL into your browser:
http://yoursite.com/page.php?ID=1
To help, If set up a live example. Click the link below to view it:
Why not try chaning the ID number to different numbers to see what happens
Extra credit work?
With query strings you can also take out the .php bit of the page name so the URL to access the content would simply be:
http://yoursite.com/page?ID=1
But in order to do this you must add some information to a file called .htaccess. Now some web-servers will not have a .htaccess file created by default some will some won't. If you site doesn't have one, then you can go ahead and create it simply create a file in notepad or some sort of editor called .htaccess no need to add a file extension or anything just name it .htaccess. You will then need to upload it within the public_html folder on your web-server and want to add this code to it:
Options +MultiViews
Save the .htaccess file with that code within it and you should now be able to access pages on your website without having to place the file extension in (such as .html and .php)










Jim Spence
December 7th
Saturday I was searching for blogs related to Web Promotion and specifically how do i add url numbers to a http and I found your related blog.