Posted on December 21st, 2006 04:37 PM by Matt

Content Management System: Part 1
Difficulty: Advanced
Type: php
Rate this tutorial: You must be logged in to rate tutorials
Rating: with 4 votes





As promised, here is my tutorial on how to make a CMS :)

A CMS(Content Management System) will let users update anything on their website without you having to touch anything!
It will almost definitely increase the likelyhood of you being employed by at least 200%

Firstly this tutorial will use PHP and MySQL (I use PHPMyAdmin to control MySQL, if you have this installed it will make things alot

easier)

This tutorial will teach you how to make a basic News page that's updateable by the CMS, you can obviously adapt this for any circumstance

:)
It will teach you how to:
    1) Display the news currently in the database
    2) Allow you/the user to submit news to the database

(PART 2 COMING SOON WHICH WILL ALLOW YOU TO DELETE AND EDIT INFORMATION)

Step 1) Creating the database:

Firstly you'll either need to install MySQL on your local machine (localhost) or contact your host and get them to create a database for

you, say http://database.mysite.com.

there are many tutorials on google on how to create a database :)

Step 2) Connection:
Now you have your URL of your database and your database name
Either "localhost" or a url such as "http://database.mysite.com"
You will constantly be needing to connect to the database you've just made so the best idea is to create a "connection.php" file.

Insert this code into notepad or dreamweaver:


<?php
//below, change the text in capitals
    $hostname = "";   // eg. database.mysite.com (unique)
    $dbusername = "";   // the username used when setting-up the database
    $dbpassword = "";   // the password used when setting-up the database
    $database = "";   // the database name chosen when setting-up the database (unique)

    //connect to the DB and select the database you've selected
    $connection = mysql_connect($hostname, $dbusername, $dbpassword) or die(mysql_error());
    mysql_select_db($database, $connection) or die(mysql_error());
?>



Now go file > Save As.. > connection.php

and press save.

Step 3) Creating the tables:
You'll need tables to hold all the information e.g News Title. Insert this code into a blank page on notepad or dreamweaver:


<?php
//automatically connect to the database
include("connection.php");

// Create table in the database you've made
mysql_select_db("DATABASENAME", $connection);
$sql = "CREATE TABLE news
(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
newsTitle text(50),
newsDate text(50),
newsContent text(1000),
)";
mysql_query($sql,$connection);

mysql_close($connection);
?>



save this file as "tablesetup.php" upload it and run it once, this will set the tables up on your database.

Step 4) Creating the basic page:

open a blank file again
and insert the following text:


<?php
/*
Tutorial Copyright 2006 - 2007
Matt Davenport - www.theflashplace.com
*/

include("connection.php"); // connect to the database

//this is an sql statement that will go through the database and select the title, date and content of the news items.
$searchSql = mysql_query("SELECT newsTitle,newsDate,newsContent FROM `news`");

$newsItemsInDatabase = mysql_num_rows($searchSql); //check to see how many rows there are

if($newsItemsInDatabase == 0){ // if there are 0 then:
    echo('Sorry, there are currently no news items!'); // display this message
}else{
    while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><br>'); //display the results clearly
    }

};

?>



Save this as "index.php" and run it, you should have a message saying "Sorry, there are currently no news items!" this is because we

haven't yet added any information to the database!

Step 5) adding information to the database:

Again, open a blank document and enter this code:


<?php
/*
Tutorial Copyright 2006 - 2007
Matt Davenport - www.theflashplace.com
*/

include("connection.php"); // connect to the database
$job = $_REQUEST['job']; // this is needed because we will either be submitting or entering the data

if(!$job){ //if variable job isn't set
    // the next part just places the basic form onto the page
    echo('
    <form name="add_news" method="post" action="submitnews.php?job=confirm_add_news">
    Title: <input type="text" name="title">    <br>
    Content: <TEXTAREA name="content" ROWS=10 COLS=70></TEXTAREA> <br>
    </form>');
}
if($job == "confirm_add_news"){
    $date = date("F dS, Y h:i A"); // get todays date using phps date(); function
    $title = $_POST['title']; // get the title from the form
    $content = $_POST['content']; // get the content from the form
    $content = nl2br($content); // convert any space between lines into <br>
    
    // then insert this information into the mySQl database
    mysql_query("INSERT INTO news (newsTitle, newsDate, newsContent) VALUES ('$title', '$date', '$content')") or die(mysql_error());
    
    //users will then see this and can then go back to the submit news page
    die('Thank-you for submitting this news item, click <A href="submitnews.php">here</A> to go back');
    
}

?>



Now, save this page as "submitnews.php"

Step 6) Check:

now if you open the "index.php" page, you will see the new information is there!!

Step 7) Put the 2 pages into one:

This will make everything so much easier and neater, please take extra care to read over this and take note of the changes made:


<?php
/*
Tutorial Copyright 2006 - 2007
Matt Davenport - www.theflashplace.com
*/

include("connection.php"); // connect to the database
$job = $_REQUEST['job']; // this is now needed because there will be many different options for the data

if(!$job){ //if variable job isn't set
//this is an sql statement that will go through the database and select the title, date and content of the news items.
$searchSql = mysql_query("SELECT newsTitle,newsDate,newsContent FROM `news`");

$newsItemsInDatabase = mysql_num_rows($searchSql); //check to see how many rows there are

if($newsItemsInDatabase == 0){ // if there are 0 then:
    echo('Sorry, there are currently no news items!<br><A href="index.php?job=add_news">[Add News]</A>'); // display this message
}else{
    echo('<A href="index.php?job=add_news">[Add News]</A>'); //echo add news first
    while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><br>'); //display the results clearly
    }

};
}

switch ($job) //this saves us from writing thousands of if statements!
{
case "add_news" :
    // the next part just places the basic form onto the page
    echo('
    <form name="add_news" method="post" action="index.php?job=confirm_add_news">
    Title: <input type="text" name="title">    <br>
    Content: <TEXTAREA name="content" ROWS=10 COLS=70></TEXTAREA> <br>
    </form>');
break; //exit this case statement

case "confirm_add_news" :
    $date = date("F dS, Y h:i A"); // get todays date using phps date(); function
    $title = $_POST['title']; // get the title from the form
    $content = $_POST['content']; // get the content from the form
    $content = nl2br($content); // convert any space between lines into <br>
    
    // then insert this information into the mySQl database
    mysql_query("INSERT INTO news (newsTitle, newsDate, newsContent) VALUES ('$title', '$date', '$content')") or die(mysql_error());
    
    //users will then see this and can then go back to the main page
    die('Thank-you for submitting this news item, click <A href="index.php">here</A> to go back');
break;

    
}// end switch
?>



save this file as index.php and run it, test it out :)


Please also note that I haven't had time to check this code over
so any problems, please leave a comment!




Back to tutorials

Sponsored Link

pseudogeek

Matt you have forgotten to add a submit button to your form code. If you add <input type="submit" name="submit" value="Submit!"> to the end of the form in either of the submit sections the whole things works beautifully! Great code! Keep it up!

vit0

you have a coding error in your tablesetup.php <?php //automatically connect to the database include("connection.php"); // Create table in the database you've made mysql_select_db("DATABASENAME", $connection); $sql = "CREATE TABLE news ( id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), newsTitle text(50), newsDate text(50), newsContent text(1000), <<<<< You need to remove that comma )"; mysql_query($sql,$connection); mysql_close($connection); ?>

bin

Nice tutorial Matt i haven't tryed it yet but from reading though it, it sounds great:) Bin