How to: Configuration area for your website

So how do we start off... Now the stuff below is only an example and you can expand on it to meet your needs.

Okay, first you need to create a database.
Insert this into it..


CODE
CREATE TABLE `site_config` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `site_title` varchar(20) NOT NULL default '',
  `webmasters_email` varchar(20) NOT NULL default '',
  `site_metatags` varchar(20) NOT NULL default '',
  `site_version` varchar(20) NOT NULL default '',
) ENGINE=MyISAM ;

INSERT INTO `site_config` VALUES (1, 'Test Website', 'webmaster@website.com', 'php, total, dream',
 '1.0', '');


BREAKDOWN:

CODE
INSERT INTO `site_config` VALUES (1, 'Test Website', 'webmaster@website.com', 'php,total,dream',
 '1.0', '');


This code inserts example information into your newly created database site_config.

- Then you have your values. 1 is the id.
- Next is Test Website which is the title.
- Then webmaster@website.com, that is the webmaster's email.
- Then there's the metatags keywords and our example is php, total, dream.
- And last but not least is 1.0 which is the website version.

---------------------------------------------------------Moving-On---------------------------------------------------------

Next you need to make a config.php file that will connect to your database, so you can give and recieve the config information.

use this for now..

CODE
<?php
   $conn 
mysql_connect("localhost","db_username","password");
   
mysql_select_db(db_database) or die(mysql_error());
?> 



BREAKDOWN:


- localhost is your host, it usally is the same for each server.
- db_username is the username for the database you created.
- password is the password for the database.
- db_database is the name of the database.

mysql_connect connects to localhost with the creditentials that you've specified.

mysql_select_db selects the database that you've also specified.

or die(mysql_error()); give you an nice error message if it cannot connect to the database, now if it does connect you will get no error message, the page will simply be blank.

---------------------------------------------------------Moving-On---------------------------------------------------------

Okay so now we're going to use the information in the database on your index.php or home page.

First at the top of the page please put this. At the very top!


CODE
<?php
//Your Site Config File
include("config.php");
?>



That will include the connection to your site_config database.

---------------------------------------------------------Moving-On---------------------------------------------------------

This is what you will use in your header < head > & < /head >.


CODE

<?php
$query 
mysql_query("SELECT * FROM `site_config`");
while (
$get mysql_fetch_array($query))
{
echo 
'<title>$get[site_title]</title>';
echo 
'<meta name="keywords" content="$get[site_metatags]">';
echo 
'<!-- Webmasters email $get[webmasters_email -->';
echo 
'<!-- Version $get[site_version] -->';
}
?>




Next will be part 2!, We will look into an admin panel, where you can update your config information!

Sunday May 27, 2007 | http://www.totaldream.org/article/87-how_to:_configuration_area_for_your_website.html