How to Create a Dynamic Page in Php Image

How to Create a Dynamic Page in Php

Creating dynamic pages in Php basically means generating new content on the fly without having to reload the page. This can be useful for a variety of reasons, including retrieving and inserting various bits of information on a single page or generating a whole new page of results. So if you have a website that makes use of databases or RSS feeds, chances are that you’ll benefit from using some sort of dynamic page creation tool.

In this tutorial, we are going to create a very simple dynamic page in Php. We will need the following details to create this page: Scenario: You would like to create a php page which will return all records from the Student table of the Database. So you need a database connection, Here your code will be based on MySQL installation.

Folder and files used in this project

Take a look at the folder and files structure used in this project.

  • Manage Site – This is like an admin folder which contains files to create and view all the dynamic web pages of the website.
    • Cleditor – A light weight javascript library for UI text editor.
    • js – Admin area javascript files
    • home.php – Home page for the admin area
    • header.php – The header part of the admin
    • footer.php – The footer part of the admin
    • manage_pages.php – Manage all the pages that are created or to be created. delete pages from this section
    • add_edit_page.php – Add or edit a page here
    • tagline.php – Update website tagline here
  • libs – It contain configuration files, common functions and database connection script
    • common_functions.php – Commonly used function for the websites and admin area.
    • constants.php – Define constatant like table names, databasebe user access, site name
    • config.php – Database connection script.
  • js – It contains javascript files for the website.
  • images – Images for the website.
  • css – The stylesheet files for the website.
  • index.php – The home page of the website.
  • header.php – Dynamic header of the website.
  • footer.php – Dynamic footer of the website.
  • sidebar.php – Dynamic right sidebar of the website.
  • about-us.php – About us page.
  • contact-us.php – Contact us page with form.
  • category.php – Category page with sub pages.
  • page.php – A dynamic page that will load page content based on id.

Dynamically Generate Simple Links

PHP script to dynamically generate simple links to the files present in current directory

$dir_open = opendir('.');

while(false !== ($fname = readdir($dir_open))){
    if($fname != "." && $fname != ".."){
        $link = "<a href='./$fname'> $fname </a><br />";
        echo $link;

<? //Generate simple text file on the fly using php

   header("Content-type: text/plain");
   header("Content-Disposition: attachment; filename=myfile.txt");

   // do your Db dynamic content into $result
   print "how to make a dynamic web pages using php & mysql\n";
   print $result;



// Session is started.

// Name of the template file.
$template_file = 'couples-template.php';

// Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
$base_path = '/couple/';

// Path to the directory where you store the "couples-template.php" file.
$template_path = '../template/';

// Path to the directory where php will store the auto-generated couple's pages.
$couples_path = '../couples/';

// Posted data.
$data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
$data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
// $data['groom-surname'] = $_POST['groom-surname'];
// $data['bride-surname'] = $_POST['bride-surname'];
$data['wedding-date'] = $_POST['wedding-date'];
$data['email'] = $_POST['email'];
$data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));

// Data array (Should match with data above's order).
$placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');

// Get the couples-template.php as a string.
$template = file_get_contents($template_path.$template_file);

// Fills the template.
$new_file = str_replace($placeholders, $data, $template);

Using External Files

To this point, every script in the book has consisted of a single file that contains all of the required code. But as you develop more complex Web sites, you’ll see that this methodology has many limitations. PHP can readily make use of external files, which allows you to divide your scripts into their distinct parts. Frequently you will use external files to extract your HTML from your PHP or to separate out commonly used processes.

PHP has four functions for using external files: include(), include_once(), require(), and require_once(). To use them, your PHP script would have a line like


The use of one of these functions has the end result of taking all the contents of the included file and dropping it in the parent script (the one calling the function) at that juncture. An important attribute of included files is that PHP will treat the included code as HTML (i.e., send it directly to the browser) unless it contains code within the PHP tags.

Previous versions of PHP had a different distinction between when you’d use include() and when you’d use require(). Now the functions are exactly the same when working properly but behave differently when they fail. If an include() function doesn’t work (it cannot include the file for some reason), a warning will be printed to the Web browser ( Figure 3.1 ), but the script will continue to run. If require() fails, an error is printed and the script is halted ( Figure 3.2 ).

Figure 3.1 Two failed include() calls.

Figure 3.2 The first failure of a require() function will print an error and terminate the execution of the script.

Both functions also have a _once() extension, which guarantees that the file in question is included only one time should a script accidentally attempt to include it repeatedly.


In this first example, I’ll use included files to separate my HTML formatting from my PHP code. Then, the rest of the examples in this chapter will be able to use the same HTML layout without you having to rewrite the code every time.

How to set up the project in your server.

Follow these steps to setup this project in your server:

  • Download the project zip file (link is available below).
  • Create a folder name simple-website under your htdocs/www folder.
  • In your phpmyadmin create a database named simple-website (you can give any other name too). Execute the SQL file database.sql in your database (remove create statement if you have already create a database).
  • Go to constants.php under libs folder. (libs/constants.php). Replace the database name in DB_DATABASE which your database name, add database username, hostname, passsword.
  • Also change the site URL (HTTP_SERVER and SITE_DIR)

Tips and tricks about the project.

There are some points that you need to remember and follow:

  • The table mp_pages has a field page_name it must be unique for every record. It means that every page must have a unique page name.
  • For the home page use index in the page_name field(mandatory), similarly for other pages like contact-us.php use contact-us(can be some other name ).
  • Pages can be created in two ways, (a) create a physical page with any name e.g. abc.php and then add/upload to your server. then add a database record with a same page which will have all the content, title and meta tags. (b) The other way is just to create a new page through database but with unique page name (this is used when you want pretty page url) and then the content will be called through their ids. like creating a page.php page and getting their contents, title (page.php?id=10).
  • To get a facebook like box plugin you need to create a facebook application first and then you can get the code from this link. If you are having trouble to create facebook application then read this tutorial which will help you in creating facebook application.
  • To get a google badge similar in the website. Go to google developers page. You will just need to replace your google plus/ page id in the url.


The dynamic web page is no longer that new a thing. Almost everyone has heard about its existence, however few know exactly how it’s created. This is exactly what this article is all about. So, let’s get to the point and start researching the subject.

Similar Posts


No Comment.