So I was writing a tutorial CMS for learning purposes the other day, and I got convinced to turn it into a tutorial. So here it is!
I divided the tutorial in 3 parts, below you can see what each part will contain.
To make this work you will need a PHP enabled server with a mysql database and of course any text editor of your choice!
This tutorial includes:
Part1: Latest and Popular tutorials
Part2: All tutorials with various sorting options, like only certain category, sort by title, asc/desc you name it!
Part 1: Show single tutorial
Part 2: Add tutorial with approve/decline by staff
Part 3:Add(without approval)/Edit and Delete admin panel
All tutorials will have a lot of rows. Like :
Title
Subtitle
Author
Category
Difficulty
Views
Rating
Votes
Access
Attachment (non-upload able)
And approve
This tutorial will be a all-in-one-file-script, meaning that we will only be creating one file to make it all happen, you can of course edit the script to suit your needs with several pages, like a universal config.php file, which most of you will have.
Oh btw, Don’t worry, I will paste the full working code at the end of this tutorial
And here is the mysql table you need:
CREATE TABLE `tutorials` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `title` TINYTEXT NOT NULL , `subtitle` TINYTEXT NOT NULL , `category` TINYTEXT NOT NULL , `author` TINYTEXT NOT NULL , `rating` VARCHAR( 255 ) NOT NULL , `approve` VARCHAR( 255 ) NOT NULL , `postdate` DATE NOT NULL , `acces` VARCHAR( 255 ) NOT NULL , `views` INT( 11 ) NOT NULL , `content` LONGTEXT NOT NULL , `attachement` VARCHAR( 255 ) NOT NULL , `difficulty` VARCHAR( 255 ) NOT NULL , `votes` VARCHAR( 255 ) NOT NULL ) ENGINE = MYISAM;
So, you think we are ready for some php? I think we are . . . so let’s go! We start off by creating a function and connecting to our database. I commented all the lines I thought were important.
<?PHP
function Ratethis($rating) {
// here we are creating a function called 'Ratethis',
// we will need this function to calculate the rating of a tutorial!
if ((($rating >= 1.00)or($rating == 1.50)) && ($rating <= 1.49)) {
return '1';
}
// ok this comment counts for every if statement in this function
// we check if the rating given in the string $rating is within certain values
// to calculate wich value we want to return. so let's say $rating is '1.88' it
// would return 1.5 as below. giving the tutorial the rating of 1.5
// the reason I do this, is to avoid ratings like 1.43576
// that just looks sloppy don't you think?
if ((($rating >= 1.50)or($rating == 1.50)) && ($rating <= 1.99)) {
return '1.5';
}
if ((($rating >= 2.00)or($rating == 2.00)) && ($rating <= 2.49)) {
return '2';
}
if ((($rating >= 2.50)or($rating == 2.50)) && ($rating <= 2.99)) {
return '2.5';
}
if ((($rating >= 3.00)or($rating == 3.00)) && ($rating <= 3.49)) {
return '3';
}
if ((($rating >= 3.50)or($rating == 3.50)) && ($rating <= 3.99)) {
return '3.5';
}
if ((($rating >= 4.00)or($rating == 4.00)) && ($rating <= 4.49)) {
return '4';
}
if ((($rating >= 4.50)or($rating == 4.50)) && ($rating <= 4.99)) {
return '4.5';
}
if ($rating == 5.0) {
return '5';
}
// and we close the function called
// Ratethis with a lovely bracket!
}
$connect = mysql_connect("localhost", "root", "");
// let's connect to our oh so lovely database!
$cdb = mysql_select_db("test");
// and let's select a database as wel;) quiet important :P
?>I hope that wasn’t to hard... we continue by adding a ‘framework’ to our system. Take a look, I commented it as before.
<?PHP
// so like any other system, we want links to the different sections within the system!
// so let's make some!
print "<h2>Tutorials</h2>\n";
print "<a href=\"tutorial.php?act=\" target=\"_self\">Tutorial Index</a>";
print " | \n";
print "<a href=\"tutorial.php?act=List\" target=\"_self\">All Tutorials</a>";
print " | \n";
print "<a href=\"tutorial.php?act=Add\" target=\"_self\">Add a Tutorial</a>";
print " | \n";
print "<a href=\"tutorial.php?act=Admin\" target=\"_self\">Admin</a>\n";
print "<br /><br />\n";
// hmm. . . those links don't work yet! let's make them work by
// creating this 'framework' for this system
switch ($_GET[act]) {
// using switch ($var) { we create some snazzy and functional links
// it will make the link look like tutorial.php?act= snazzy huh;)
default:
// the default page we want to display if $_GET[act] isn’t defined in the URL
break;
case ‘Show’:
// if the link is tutorial.php?act=Show we display what is inside this and the break; line
break;
case ‘List’:
// if the link is tutorial.php?act=List we display what is inside this and the break; line
break;
case ‘Add :
// if the link is tutorial.php?act=Add we display what is inside this and the break; line
break;
case ‘Admin’:
// if the link is tutorial.php?act=Admin we display what is inside this and the break; line
break;
}
// remember we opened brackets when we used the switch statement? Now we close them
mysql_close($connect);
// and we close our connect variable, not really important, don’t really know why I’m doing
// this but Hey . . who cares :p
?>Ok, so we have our framework set up. Time for some real hard coding! I’m going to work down on our framework starting with the case ‘Default’ and ending with the case ‘Admin’. Bare with me
Note: we are now coding inside default: and the first break;!
Basically what I, excuse me, We want to display, is the latest and the most popular tutorials in our database. We will start with the latest.
<?PHP
print "<h3>Latest Tutorials</h3>\n";
// we print a header telling the visitor what it will see below
$a = mysql_query("SELECT * FROM tutorials WHERE approve = '1' ORDER BY postdate DESC LIMIT 5");
// above is the query selecting results where there approve row is set to 1, we order those
// results by postdate and descending, meaning from top to bottom and limiting the results to 5
print "<a href=\"tutorial.php?act=List\" target=\"_self\">All Tutorials</a>\n";
// we print a link to all the tutorials, this is in the List case which we will make soon!
print "<table width=\"100%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\">
<tr>
<th>Titel</th>
<th>Category</th>
<th>Difficulty</th>
<th>Rating</th>
<th>Views</th>
<th>Author</th>
<th>Posted on</th>
</tr>";
// above we printed a table to contain our results we will display some lines below.
if (mysql_num_rows($a) == 0) {
// if the amount of results retrieved, using mysql_num_rows is equal to 0 we do this:
print "
<tr>
<td colspan=\"7\">There are no tutorials to display!</td>
</tr>\n";
// we print that there are no tutorials :(
} else {
// if the amount of results retrieved is NOT equal to 0 we do this:
while ($b = mysql_fetch_object($a)) {
// using a while loop with mysql_fetch_object, wich will make it so
// that all the results are treated like a object
print "
<tr>
<td><a href=\"tutorial.php?act=Show&tid=".$b->id."\" target=\"_self\">".$b->title."</a></td>
<td>".$b->category."</td>
<td>".$b->difficulty."</td>
<td>".$b->rating."</td>
<td>".$b->views."</td>
<td>".$b->author."</td>
<td>".$b->postdate."</td>
</tr>\n";
// we print every results, you see that the first results even has a link,
// the link brings the visitor to the full tutorial
}
// we close the while loop
}
// we close the else statement
print "</table>\n";
// we end the table
?>Well that was a ****load of code
So we have on case finished, let’s continue with the next case, ‘Show’! Show will display the full tutorial:
<?PHP
print "<h3>Show Tutorial</h3>\n";
// once again we show were we are with a header 3
$tid = $_GET[tid];
// we are assigning a variable to the $_GET[id] for further reference, if you don’t understand
// what $_GET[id] does, it looks inside the URL for a piece of text like &id=(SOMETHING)
if (!isset($tid) || !is_numeric($tid)) {
// here we check using a ‘!’ to see if the $tid variable is NOT set or that the $tid variable is not
// numeric. If it is not set or it isn’t numeric we do what is inside the brackets
print "Invalid or no ID given, redirecting you. . .\n";
// we tell the users that the id they have provided is invalid
print "<meta http-equiv=\"refresh\" content=\"3;URL=tutorial.php?act=List\" />\n";
// and we redirect the user to the List case to see all the tutorials!
} else {
// if the $tid variable IS set and has a numeric value we do what is inside these brackets!
$a = mysql_query("SELECT * FROM tutorials WHERE id = '$tid' AND approve = '1' LIMIT 1");
// we make a query select 1 tutorials, see the limit1?, where the id is corresponding to // the value of the $tid variable!
if (mysql_num_rows($a) == 0) {
// now we check if got any results from that query, if the number of results is equal to
// 0, we, once again, do what is inside these brackets
print "The tutorial matching the id you provided does not exist!";
// sorry no tutorials match that id!
} else {
// else if the results is greater then 1 we do this:
while ($b = mysql_fetch_object($a)) {
// once again we create a while loop with mysql_fetch_object();
$newView = $b->views + 1;
// we add a view by grabbing the old amount of views
// retrieved from the query and adding 1 to it.
$addView = mysql_query("UPDATE tutorials SET views = '$newView' WHERE id = '$tid'");
// and we update the amount of views with the new amount of views!
$attachement = $b->attachement;
// we assign a new variable to the attachement object retrieved from the
// query
if (!$attachement) {
// we check if $attachement has any value, if it doesn’t we do this:
$attachement = "--";
// gives $attachement a new value,
} else {
// $attachment does have a value! We do this:
$attachement = "<a href=\"".$b->attachement."\" target=\"blank\">Click to download</a>";
// $attachement is now a link with the URL being the attachement
// object!
}
if (isset($_POST[ratethis])) {
// we check if the submit button for the rating has been pressed!
// I’m not 100% sure this actually works as intended,
// it’s up to you to figure it out, I’m no genius at math so… :(
$newVotes = $b->votes + 1;
$old_total = $b->rating * $b->votes;
$new_total = $old_total + $_POST[rating];
$new_rating = $new_total / $newVotes;
$setNewRating = Ratethis($new_rating);
$updateRating = mysql_query("UPDATE tutorials SET votes = '$newVotes', rating = '$setNewRating' WHERE id = '$b->id'");
// we update the new rating for the tutorial displayed
$rateIt = "Thanks for the vote!";
// we make a variable containing a message
} else {
// if the submit button for the rating hasn’t been pressed
$rateIt = "<select name=\"rating\">
<option value=\"5\" selected>5</option>
<option value=\"4\">4</option>
<option value=\"3\">3</option>
<option value=\"2\">2</option>
<option value=\"1\">1</option>
</select>
<input type=\"submit\" name=\"ratethis\" value=\"Rate\">";
// $rateIt got a new value, it is now the dropdownbox for the
// rating waiting to be displayer
}
print "<form method=\"post\">";
// we print some html, starting a form!
print "<strong>".$b->title."</strong>\n";
// we print out the tutorial’s title in bold text
print "<hr noshade color=\"#222222\" size=\"1px\" />\n";
// we make a nice horizontal line across the screen
print "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"10\">
// we print some html, containing the html for a table
<tr>
<td width=\"40%\" valign=\"top\">Category: <strong>".$b->category."</strong><br />
Difficulty: <strong>".$b->difficulty."</strong><br />
Rating: <strong>".$b->rating."</strong> (".$b->votes." votes) ".$rateIt."<br />
Views: <strong>".$b->views."</strong><br /></td>
<td valign=\"top\">Posted on: <strong>".$b->postdate."</strong><br />
Acces: <strong>".$b->acces."</strong><br />
Author: <strong>".$b->author."</strong><br />
Attachement: <strong>".$attachement."</strong><br /></td>
</tr>
</table>\n";
// that is just some basic html/php to show various information from the information retrieved
// from the database
print "<hr noshade color=\"#222222\" size=\"1px\" />\n";
// we make another nice little line
print nl2br($b->content)."\n";
// we print the actual tutorial, with the n12br() function to allow
// $b->content to have linebreaks and stuff!
print "</form>\n";
// we close the form
}
}
}
// and we close some brackets!
?>That’s it for part 1, at the end of part 3 you will find the whole working code, Any questions/suggestions Let me know
Wildo
Help






















