Dynamic Page Creation: PHP

Advertisement:

In this PHP program we will not be generating pages, but creating the illusion of dynamic page creation!
i.e., we will have a bunch of files inside a directory. We will fetch the contents of these files and display on a single file(index.php).

Advantages:
1. Eliminating duplicate coding: Since, we will display the contents on single page, all the common content will be written in that single page.
2. Common user will not be able to track the exact location of the individual files.
3. We can track, if the user is trying to access a file that does not exist, and then we can show appropriate messages: This will be user friendly and also helps in Search Engine Optimization(SEO), as we can specially design our 404 pages.

Note: Even though this dynamic page creation may not be something you are looking for right now, I am sure, you will encounter a situation in your project development where this technique will come handy. So nevertheless, have it in your tool box!



YouTube Link: http://www.youtube.com/watch?v=RrSRO248NOQ [Watch the Video In Full Screen.]


PHP Program with CSS implemented: (index.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
<html>
 <head><title> Dynamic Pages </title>
 
	<style type="text/css">
 
		* {
			font-family: Verdana;
			font-size: 24pt;
 
		}
		#menu {
			position: absolute;
			top: 50px;
			left: 200px;
 
			padding: 10px;
			margin:  10px;
 
			background-color: pink;
 
		}	
 
		#content {
			position: absolute;
			top: 50px;
			left: 450px;
 
			padding: 10px;
			margin:  10px;
 
			background-color: yellow;
			color: red;
 
		}
 
 
	</style>
 </head>
 <body>
 
  <div id="menu">
 
	<a href="index.php">Home</a><br />
	<a href="index.php?page=js">Javascript</a><br />
	<a href="index.php?page=cSharp">C Sharp</a><br />
	<a href="index.php?page=css">CSS</a><br />
 
  </div>
 
 <div id="content">
 
	<?php
 		$p = $_GET['page'];
 
		$page = "sub/".$p.".php";
 
		if(file_exists($page))
			include($page);
		elseif($p=="")
			echo "This is Home Page";
		else
			echo "what are you looking for! ?";
 
	?>
  </div>
 
 
 </body>
</html>

In the menu div, we are having some anchor tags, using which we are passing the file names to the variable page. Which we receive in the php code(same page) and add the exact file location and check whether the file exists in the specified location or not.

Based on the file name passed, we will include the corresponding file content in the content div section. If nothing is passed, then its home page. If some irrelevant file names are passed, then we will show some messages: Here we can design a 404 error message page and show it – each time someone tries to access a page that does not exists.

File Structure:
file-structure
Save all these below files ( js.php, cSharp.php, css.php and index.php ) as shown in above image.

js.php Content:

This is Javascript File...

cSharp.php Content:

This is C Sharp....

css.php Content:

This is Cascading StyleSheet...

[ Above files are kept simple in-order to save time and to keep the program explanation simple. You can use your web designing skills to make the pages as you like. ]

PHP Code Explanation:

$p = $_GET['page'];

This line of code accepts the parameter sent by the anchor tags and stores it in a variable by name p.

		$page = "sub/".$p.".php";

sub is the folder name, inside which we have our js.php, cSharp.php and css.php files.

Ex:
If the link corresponding to cSharp is clicked. Then variable p will have the string cSharp in it.
So, variable page will have sub/cSharp.php in it.

if-elseif-else Statement:

		if(file_exists($page))
			include($page);
		elseif($p=="")
			echo "This is Home Page";
		else
			echo "what are you looking for! ?";

file_exists is a built-in php function, which checks if the specified page/file exists or not.
If it exists, then we will include that page. i.e., the content of the page will be displayed.
If variable p has nothing in it, then the clicked link is home page.
If some non-existing page is passed, then we display the message “What are you looking for! ?”.

10 thoughts on “Dynamic Page Creation: PHP”

  1. on the index page it showing some error…………

    “Notice: Undefined index: page in C:\wamp\www\regiosoutions\index.php on line 53”

  2. @Abhilash, The variable name inside GET i.e., page should be present both inside php and in the hyperlink. Below are both the codes. Please check them in your program.

            <a href="index.php?page=js" rel="nofollow">Javascript</a><br />
    	<a href="index.php?page=cSharp" rel="nofollow">C Sharp</a><br />
    	<a href="index.php?page=css" rel="nofollow">CSS</a><br />

    and

    $p = $_GET['page'];


  3. May i know your mail please ? so i can send you one document. I need to edit that document. I am poor in language. So please help me. Thanks in advance….

  4. @Suresh, Please refer to the comment number 2. Try copy pasting above code. If that works(which should), then you know you have made some mistakes while typing or in some other logic.

    What kind of documents you have ?

    You can post it or share the info with us via our forum: Technotip Forum

  5. @Suresh, @Srini, I know how frustrating it is when we can’t get to what we want immediately.

    Its bit complicated to explain this using WordPress, as its internal structure is very different.

    @Suresh, Can you please post your code in the forum that you were trying to post here in the comment section few days back.

    please use

     <blockquote> </blockquote>


    to post your code in the forum.

  6. Hi, i just got your tutorial about creating dynamic pages in php. It’s so simple and well grounded basic one. I love this post and registered my self in ur site to get more updates from you.

    Earlier i was really didn’t like php because i don’t have knowledge about that. I used to use Dreamwaver to replace the html code with php. I have some doubts, if you guys can explain me how to solve those, i am really thank full to u.

    1. Creating some list of text links in index.php using database. (i did it)
    2. displaying the related content for those links in post.php. (i did it)
    3. In post.php, i want to display the remaining links except which article link we opened in post.php. ( not getting how to do this).
    4. If we can display the remaining links in post.php, how to display the content of those links in that same page. ( we already used that page for displaying the content of index.php links, but how to use the same page for post.php links also.??)

    Sorry, for my poor english and if there is any silly issue please let me know.

    Thanks in advance.

    Pardhu

Leave a Reply

Your email address will not be published. Required fields are marked *