txm
11-17-2008, 01:42 AM
Hi everyone! Many people are asking me how to create a simple dynamic page system with PHP take a look and tell me what do you think.
Let me first explain the index.php. In the index PHP you will make a session and database connection, so you do not need to include your configuration file in all your scripts
<?php
// Creating the session. Remember only in the index.php
session_start();
// Here goes you mysql database connection, Define connection details
define(DBHOST, 'localhost'); // The hostname to your databases
define(DBUSER, 'your_user'); // Username assigned with database server
define(DBPASS, 'your_password'); // Password assigned with your database username
define(DBDATA, 'your_database'); // And the name of your database
// And the connection
$connection = mysql_connect (DBHOST, DBUSER, DBPASS);
if (!$connection) {
echo '<p>Unable to connect to database server. Check your mysql connection details</p>';
}
// Locating the database
if (!mysql_select_db('DBDATA', $connection)) {
echo '<p>Unable to locate database. Check you details</p>';
}
/* Second step is just a normal HTML code with the page system code */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ITDNET :: HOME</title>
<style type="text/css">
<!--
body {
width: 770px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
-->
</style>
</head>
<body>
<div id="container">
<div id="menu">
<li><a href="?page=home">Home</a></li>
<li><a href="?page=test">Test page</a></li>
</div>
<div id="page">
<?php
/**
* Okay! Lets start with the dynamic page system
* Remember from now you <a href*> link will start up with ?page=
* Here we will also include the home.php file (NOT THE INDEX!)
**/
if (isset($_GET['page'])) {
if (file_exists($_GET['page'].'.php')) {
include($_GET['page'].'.php');
} else {
echo 'ERROR 404 FILE NOT FOUND';
}
} else {
include('home.php');
}
/* END of the page system */
?>
</div>
</body>
</html>
Okay! Let me explain. <div id="page"> Here is the place where youe pages are going to be displayed. For example if you have file called register.php and you want to make visible
just write a new link in <div id="menu"> ---> <li><a href="?page=register"></a></li>
And now the test.php
<?php
echo 'Hallo there. This is the test page';
?>
That`s all. Simple but good.
I hope I helped just one of you!
Let me first explain the index.php. In the index PHP you will make a session and database connection, so you do not need to include your configuration file in all your scripts
<?php
// Creating the session. Remember only in the index.php
session_start();
// Here goes you mysql database connection, Define connection details
define(DBHOST, 'localhost'); // The hostname to your databases
define(DBUSER, 'your_user'); // Username assigned with database server
define(DBPASS, 'your_password'); // Password assigned with your database username
define(DBDATA, 'your_database'); // And the name of your database
// And the connection
$connection = mysql_connect (DBHOST, DBUSER, DBPASS);
if (!$connection) {
echo '<p>Unable to connect to database server. Check your mysql connection details</p>';
}
// Locating the database
if (!mysql_select_db('DBDATA', $connection)) {
echo '<p>Unable to locate database. Check you details</p>';
}
/* Second step is just a normal HTML code with the page system code */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ITDNET :: HOME</title>
<style type="text/css">
<!--
body {
width: 770px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
-->
</style>
</head>
<body>
<div id="container">
<div id="menu">
<li><a href="?page=home">Home</a></li>
<li><a href="?page=test">Test page</a></li>
</div>
<div id="page">
<?php
/**
* Okay! Lets start with the dynamic page system
* Remember from now you <a href*> link will start up with ?page=
* Here we will also include the home.php file (NOT THE INDEX!)
**/
if (isset($_GET['page'])) {
if (file_exists($_GET['page'].'.php')) {
include($_GET['page'].'.php');
} else {
echo 'ERROR 404 FILE NOT FOUND';
}
} else {
include('home.php');
}
/* END of the page system */
?>
</div>
</body>
</html>
Okay! Let me explain. <div id="page"> Here is the place where youe pages are going to be displayed. For example if you have file called register.php and you want to make visible
just write a new link in <div id="menu"> ---> <li><a href="?page=register"></a></li>
And now the test.php
<?php
echo 'Hallo there. This is the test page';
?>
That`s all. Simple but good.
I hope I helped just one of you!