Thursday, October 21, 2010

how to create static html page from a database table using php

In this post we are creating a html file or pages which is static page in specific loacation of folder from a databse table using php. In this program we are also creating folders in specific location maintain url.
This table contains all the data for the "static" pages of the site.

Note: Please remove all "\" from bellow code, I have use it because blogger does not show html tags.

Step 1. Create a table as bellow.

CREATE TABLE `template` (
  `id` bigint(200) NOT NULL auto_increment,
  `name` varchar(200) NOT NULL,
  `address` text NOT NULL,
  `city` varchar(200) NOT NULL,
  `country` varchar(200) NOT NULL,
  `companyname` varchar(200) NOT NULL,
  `empid` varchar(200) NOT NULL,
  PRIMARY KEY  (`id`)
);

Step 2. Copy bellow code and create a php file name refresh.html.

<\?php
ob_start();

/**************** DataBase Conectivity ************/
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test");

/************** Create Directory(Folder)*************/
$query_dir = "select * from template";
$ressel_dir = mysql_query($query_dir);
while($data_dir = mysql_fetch_array($ressel_dir))
{
            $datadir = $data_dir['name'];
            $datapage = $data_dir['id'];
           
            /**************Include Pages**************/
            include ("template.php");
            $content = ob_get_contents();
            ob_end_clean();

            $dir_name = strtolower(str_replace(' ', '-', $datadir));
            $dir_page = strtolower(str_replace(' ', '-', $datapage));

            @mkdir($dir_name, 0777, true);

            /*************** Create File Path *****************/
            $filename = $dir_name."/".$dir_page.".html";

            /************** Write contents on file ****************/
            $fp = fopen($filename, "w") or die("Could't open $filename");

            fwrite($fp, $content);
            fclose($fp);
}

echo "All File written Success Full<\br />";
echo "Congratulation HTML File has been created";

?>

Step 3. Copy bellow code and create a php file name template.php

$query= "select * from template where id='".$datapage."'";
$res = mysql_query($query) or die(mysql_error());
$data = mysql_fetch_assoc($res);
?>
<\html>
<\head>
            <\title>creating static html page from a database table using php<\/title>
<\/head>
<\body>
<\table border="0" cellpadding="0" cellspacing="0" width="100%">
            <\tr>
                        <\td><\b>Name<\/b><\/td>
                        <\td><\b>Address<\/b><\/td>
                        <\td><\b>City<\/b><\/td>
                        <\td><\b>Country<\/b><\/td>
                        <\td><\b>Company Name<\/b><\/td>
                        <\td><\b>Employee ID<\/b><\/td>
            <\/tr>
            <\tr>
                        <\td><\?=$data['name']?><\/td>
                        <\td><\?=$data['address']?><\/td>
                        <\td><\?=$data['city']?><\/td>
                        <\td><\?=$data['country']?><\/td>
                        <\td><\?=$data['companyname']?><\/td>
                        <\td><\?=$data['empid']?><\/td>
            <\/tr>
<\/table>
<\/body>
<\/html>