Web Design Forum: TUTORIAL: CMS - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: CMS Rate Topic: -----

#1 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

  Posted 17 May 2007 - 08:42 PM

Tutorial CMS – Part 1
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 :p wasn’t it, I hope it wasn’t to hard and that I commented it enough. If not, you’re out of luck :p.
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
0

#2 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 17 May 2007 - 08:44 PM

Tutorial CMS – Part 2
Well then, we finished part 1 without any bruises I hope? In part 2 we will make a List of all the tutorials in the ‘List’ case.. :p furthermore we are going to make the add page, any tutorials submitted there must go through approval of anyone that has access to the admin panel.

So let’s go, I hope you still have that framework from part1 , we are going to code inside
<?PHP
case ‘List’:
// HERE
break; 
?>

Now. Have fun. As always I commented as hell, this part is a bit complicated but apart from some scratches and bruises we should be alright! Here we go

<?PHP
// first off, before we are going to show the user anything, we need to assign a lot of things to
// make this work, As I’ve stated in part 1, the List case will include several sorting options,
// you can sort by Category, Difficulty title you name it everything, you can even filter the 
// List so it only contains tutorials from a certain category!
$url = "tutorial.php?act=List";
// we assign a value to the $url variable, containing the base URL for the List case
$order = strtolower($_GET[sort_by]);
// we, once again, use a $_GET[value] to get any information from the url, then  we use 
// strtolower() function to remove any capital letters from the received information
// strtolower() works like this: $bla = strtolower(BlA);, that will change BlA to bla
// easy huh;)
$list = array("category", "postdate", "author", "title", "difficulty", "rating", "views");
// then we use the array() function to make a array with all the fields we want to sort with
// and we assign it to the $list variable!
if (in_array($order, $list)) {
// we check if the received information retrieved from the $_GET[sort_by] matches any of 
// words inside the array, if there is a match we do this:
	$order = $_GET[sort_by];
	// we give $order the $_GET[] value
	$orderURL = "&sort_by=".$order;
	// we save a bit of the url in the variable $orderURL
} else {
// else if the retrieved $_GET[] value does not match anything from the array, we want it to
// have a default value, which is:
	$order = "postdate";
	// default sort_by is postdate, so by default it will sort the results by postdate
}
// next up, we are checking the ‘flow’, does the user want the results to be from top to bottom 
// or vice versa?
$sort = $_GET[flow];
// we are retrieving the &flow= value from the URL
if ($_GET[flow] == "ASC") {
// we check if the value is equal to ‘ASC’ if it is:
	$otherSort = "DESC";
	// we tell the script that $otherSort is ‘DESC’
	$sortURL = "&flow=DESC";
	// and we, once again, save a bit of the url in a variable
} else {
// else if the $_GET[flow] is not ASC we do:
	$sort = "DESC";
	// instead of ASC $sort is now ‘DESC’
	$sortURL = "&flow=ASC";
	// and the saved url should be ASC
}
// next up, we want to check if the users wants to see only results with a certain category!
$filter = $_GET[filter];
// we once again assign a variable to the retrieved information from the URL
$filterArray = array("PHP", "HTML", "Photoshop");
// we create another array containing our categories 
$filterURL = NULL;
// the base state of $filterURL is NULL, we don’t want it to have any value if the user isn’t 
// filtering the results!
if (isset($filter) && in_array($filter, $filterArray)) {
// check with the isset() function to see if $filter is set and if $filter matches any of the results 
// from the $filterArray, if we have a match:
	$filterURL = "&filter=".$filter;
	// we save the url in a variable once more!
	$a = mysql_query("SELECT * FROM tutorials WHERE approve = '1' AND category = '$filter' ORDER BY $order $sort");
	// if we are filtering, we want to change the queries ‘where’ , by adding ‘AND
	// category = ‘$filter’ we are letting the filter do it’s work
} else {
// if there is no filter set we don’t want to do anything special with the query
	$a = mysql_query("SELECT * FROM tutorials WHERE approve = '1' ORDER BY $order $sort");
	// the normal query, note the where clause, we are only selecting tutorials that are
// approved!
}
// we close the else statement
// we are not done yet, we have yet to display the tutorials in a list, so here we go!
print "<h3>All Tutorials</h3>\n";
// we display where we are with a header type 3
print "<a href=\"tutorial.php?act=List\" target=\"_self\">Change to Default order</a>\n";
// we print a link to the default settings of the List case, in case they want to reset their filter.
print "<table width=\"100%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\">
<tr>
	<th><a href=\"".$url.$filterURL."&sort_by=title".$sortURL."\" target=\"_self\">Titel</a></th>
	<th><a href=\"".$url.$filterURL."&sort_by=category".$sortURL."\" target=\"_self\">Category</a></th>
	<th><a href=\"".$url.$filterURL."&sort_by=difficulty".$sortURL."\" target=\"_self\">Difficulty</a></th>
	<th><a href=\"".$url.$filterURL."&sort_by=rating".$sortURL."\" target=\"_self\">Rating</a></th>
	<th><a href=\"".$url.$filterURL."&sort_by=views".$sortURL."\" target=\"_self\">Views</a></th>
	<th><a href=\"".$url.$filterURL."&sort_by=author".$sortURL."\" target=\"_self\">Author</a></th>
	<th><a href=\"".$url.$filterURL."&sort_by=postdate".$sortURL."\" target=\"_self\">Posted on</a></th>
</tr>";
// we just printed a table with some Table headers including the links to sort the results. 
// you See, there we see the variables again that we defined above, every time a user click
// eg on title, the links changes from eg DESC to ASC to change the sorting :)
if (mysql_num_rows($a) == 0) {
// we check if the number of results are 0, if they are
	print "
<tr>
	<td colspan=\"7\">There are no tutorials to display!</td>
</tr>\n";
// sorry, no tutorials :(
} else {
// if we do have tutorials!
	while ($b = mysql_fetch_object($a)) {
	// once again we use mysql_fetch_object to execute a query!
		print "
<tr>
	<td><a href=\"tutorial.php?act=Show&tid=".$b->id."\" target=\"_self\">".$b->title."</a></td>
	<td><a href=\"".$url."&filter=".$b->category.$orderURL.$sortURL."\" target=\"_self\">".$b->category."</a></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 out all the tutorials on a new <tr>, the first <td> has a link
		// leading to the actual tutorial, that will be displayed in the Show case
		// the second <td> allows the user to filter by category!
	}
}
print "</table>\n";
// we close some brackets and end the table!
?>


That much for the List case, I hope it wasn’t to hard and that your boat is still floating :). Let’s end this part with the ‘Add’ case. This won’t be as hard as the List case, thank god ;)

<?PHP
print "<h3>Add Tutorial</h3>\n";
// once more we display where we are!
print "Please note that the tutorials you are submitting here, will be reviewed by the staff first. We reserve the right to decline/edit or remove your tutorial from our database at any time.\n";
// we give the user some information before they submit the tutorial
	 if (!isset($_POST[posttut])) {
	// we check using !isset() to see if the submit button has been pressed, if it isn’t:
	 print "<form method=\"post\">\n";
print "<strong>Title:</strong> \n<br />\n";
print "<input type=\"text\" name=\"title\"> \n<br />\n";
print "<strong>Subtitle:</strong> \n<br />\n";
print "<input type=\"text\" name=\"subtitle\"> \n<br />\n";
print "<strong>Author:</strong> \n<br />\n";
print "<input type=\"text\" name=\"author\"> \n<br />\n";
print "<strong>Category:</strong> \n<br />\n";
print "<select name=\"category\">
<option title=\"PHP\">PHP</option>
<option title=\"HTML\">HTML</option>
<option title=\"Photoshop\">Photoshop</option>
</select>\n<br />\n";
print "<strong>Difficulty:</strong> \n<br />\n";
print "<select name=\"difficulty\">
<option title=\"Easy\">Easy</option>
<option title=\"Medium\">Medium</option>
<option title=\"Hard\">Hard</option>
</select>\n<br />\n";
print "<strong>Content:</strong> <br />\n";
print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> \n<br />\n";
print "<input type=\"submit\" name=\"posttut\" value=\"Add Tutorial\">\n";
print "</form>\n";
// we display a simple form, containing all the values that are editable by the author.
} else {
// else if the submit button HAS been pressed:
	 $getTitle = $_POST[title];
	$getSubTitle = $_POST[subtitle];
	$getAuthor = $_POST[author];
	$getCategory = $_POST[category];
	$getDifficulty = $_POST[difficulty];
	$getContent = $_POST[content];
	$getDate = date("Y-M-j");
	$getApprove = 0;
	// we assign various variables to the posted content we retrieved from the form
	$insertTutorial = mysql_query("INSERT INTO tutorials (title, subtitle, author, difficulty, category, postdate, approve, content) VALUES ('$getTitle', '$getSubTitle', '$getAuthor', '$getDifficulty', '$getCategory', '$getDate', '$getApprove', '$getContent')");
	// and we use the information from the form, and make a new record in our database
	// containing the new tutorial, since $getApprove is still 0, the tutorial is still awaiting 
	// to be approved by anyone from the admin panel!
	print "Thank you, your tutorial has been added to our database and is awaiting approval by our staff. You wil either get a e-mail or a private message telling you if the tutorial has been approved/declined and/or modified!";
}
// we close a bracket and the add part is done, easy huh, 
?>

Now all that remains is part3, I’ll post as soon as I can, but it’s time for me to get a beer and clear my head, see you all in part 3, once again if you have any questions/suggestions about this tutorial, feel free to ask it in the comments.

Wildo
0

#3 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 17 May 2007 - 08:46 PM

Tutorial CMS – Part 3
So - I kinda forgot to post part 3, so here it is, sorry for the delay!

Well then, it all comes down to this. The last part, part 3! In this part we will construct a admin panel, where you can Add(without staff approval), Edit and Delete tutorials. Nothing complicated just using simple php to make it happen.

Note: we are now coding inside the case ‘admin’: and before the break; have fun.

<?PHP
// this admin panel is not secured by password or something like that,
// if you want to integrate it with EG naresh’s user system, 
// add things like if (isset($logged[username]) && isset($logged[password])) {
// we start off, by once again making a little framework to work in,
// the admin section is divided in the 4 sections I told you about above
print "<h3>Tutorial Admin</h3>\n";
// the admin panel coming up!
print "<h2>Tutorials</h2>\n";
print "<a href=\"tutorial.php?act=Admin&mod=Add\" target=\"_self\">Add tutorial</a>";
print " | \n";
print "<a href=\"tutorial.php?act=Admin&mod=Approve\" target=\"_self\">Approve tutorials</a>";
print " | \n";
print "<a href=\"tutorial.php?act=Admin&mod=Edit\" target=\"_self\">Edit tutorials</a>";
print " | \n";
print "<a href=\"tutorial.php?act=Admin&mod=Delete\" target=\"_self\">Delete tutorials</a>";
print "<br /><br />\n";
print "";
// basically we display the navigation for the 
// admins, showing them 4 links of what to do
$whereAdmin = $_GET[mod];
// using the $_GET[value] once more to use 
// the link to know what to display
$adminArray = array("Add", "Approve", "Edit", "Delete");
// with array() we create another array with the possible admin actions
// and assigning it to a variable!		
if ($whereAdmin == "Add") {
// if the $whereAdmin variable is equal to 'Add', we do this:
print "Add tutorial:<br /><br />\n";
// we display where we are.
} elseif ($whereAdmin == "Approve") {
// elseif the $whereAdmin variable is equal to 'Approve', we do this:
print "Approve tutorials:<br /><br />\n";
// we print where we are
} elseif ($whereAdmin == "Edit") {
// elseif the $whereAdmin variable is equal to 'Edit', we do this:
print "Edit tutorial:<br /><br />\n";
// we print where we are
} elseif ($whereAdmin == "Delete") {
// elseif the $whereAdmin variable is equal to 'Delete', we do this:
print "Delete tutorial:<br /><br />\n";
// we print where we are
}
// so much for our framework, as you see we have created 4
// sections, each containing different admin options.
// our link to the admin panel now looks like
// tutorial.php?act=Admin&mod=Add
// the filename -> Admin part -> section
?>


I hope that that was not to hard for you lot. We continue by creating the content for the first section. ‘Add’. The Add section will allow staff to add tutorials without the tutorial being reviewed by another member of the staff.

We are now coding inside the ‘Add’ section of the admin panel:
<?PHP
if ($whereAdmin == "Add") {
// HERE
}
?>


Let’s throw some code in your face.

<?PHP
// if the $whereAdmin variable is equal to 'Add', we do this:
// the add tutorial part is actually 90% similair to the normal add
// page, but instead it will have $getApprove to 1 instead of 0,
// and you get to set the acces level,
// like 'All', 'Members' or only 'Staff'
// however this system doesn't come with a user system
// attached to it, so create your own user system
// and use if statement to check if who can view the tutorial
// if they cant tell 'm they are not allowed
// nothing fancy, if you need help with that
// just ask :)
print "Add tutorial:<br /><br />\n";
// our usual print of where we are!
 if (!isset($_POST[posttut])) {
// if the submit button has NOT been pressed,
// we check that by adding a '!' in front of the isset() function
	// which checks if something has a value.
	print "<form method=\"post\">\n";
	print "<strong>Title:</strong> \n<br />\n";
	print "<input type=\"text\" name=\"title\"> \n<br />\n";
	print "<strong>Subtitle:</strong> \n<br />\n";
	print "<input type=\"text\" name=\"subtitle\"> \n<br />\n";
	print "<strong>Author:</strong> \n<br />\n";
	print "<input type=\"text\" name=\"author\"> \n<br />\n";
	print "<strong>Category:</strong> \n<br />\n";
	print "<select name=\"category\">
	<option title=\"PHP\">PHP</option>
	<option title=\"HTML\">HTML</option>
	<option title=\"Photoshop\">Photoshop</option>
</select>\n<br />\n";
	print "<strong>Difficulty:</strong> \n<br />\n";
	print "<select name=\"difficulty\">
	<option title=\"Easy\">Easy</option>
	<option title=\"Medium\">Medium</option>
	<option title=\"Hard\">Hard</option>
</select>\n<br />\n";
	print "<strong>Acces:</strong> \n<br />\n";
	print "<select name=\"acces\">
	<option title=\"All\">All</option>
	<option title=\"Members\">Members</option>
	<option title=\"Staff\">Staff</option>
</select>\n<br />\n";
	print "<strong>Content:</strong> <br />\n";
	print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> \n<br />\n";
	print "<input type=\"submit\" name=\"posttut\" value=\"Add Tutorial\">\n";
	print "</form>\n";
	// easy pudding and pie form,
	} else {
	// oh the submit button HAS been pressed
	// let's process the information retrieved from the form
	// we start of by assigning the various posted
	// information into some variables
		$getTitle = $_POST[title];
		$getSubTitle = $_POST[subtitle];
		$getAuthor = $_POST[author];
		$getCategory = $_POST[category];
		$getDifficulty = $_POST[difficulty];
		$getContent = $_POST[content];
		$getAcces = $_POST[acces];
		$getDate = date("Y-m-j");
		// YYYY-MM-DD date format
		$getApprove = 1;
		// approval of 1, so it doesn't need any approval from staff
		$insertTutorial = mysql_query("INSERT INTO tutorials (title, subtitle, author, difficulty, category, postdate, approve, content, acces) VALUES ('$getTitle', '$getSubTitle', '$getAuthor', '$getDifficulty', '$getCategory', '$getDate', '$getApprove', '$getContent', '$getAcces')");
		// a lovely query inserting the new tutorial to our database
		print "Thank you, your tutorial has been added to our database. You can now see it in the tutorial list!";
		// ah lovely thank you message telling the user all went well.
}
?>


So that was the Add tutorial part of the admin panel, next up is the tutorial approval section. Let’s get going shall we!

<?PHP
// elseif the $whereAdmin variable is equal to 'Approve', we do this:
// first of all, let me tell you how we are going to 'approve' tutorials,
// we start of by making a list of all the pending tutorials,
// we select all the pending tutorials by checking if the 'approve' row
// is equal to '0'.
// every tutorial that comes out of that query will have a link to a page
// containing the full tutorial with a button to approve or decline
// the tutorial, approve will make the 'approve' row to 1,
// decline will make the row to '2', you can edit this, 
// by deleting the declined tutorial from the database,
// but that is up to you :)
print "Approve tutorials:<br /><br />\n";
// once again we display where we are!
$aa = $_GET[section];
// we use $_GET once again to define what we want to display, the list
// or the tutorial!
if (isset($aa) && $aa = "Approve") {
	// if the $aa variable is  set, 
	// and is equal to 'Approve'
	// we display the full tutorial
	if (isset($_GET[aid]) && is_numeric($_GET[aid])) {
	// we check if ‘aid’ is set in the link, and if it is a numeric value, if it is:
		$tid = $_GET[aid];
		// we assign the part of the URL ‘&aid=*’ to the $tid variable
		$a = mysql_query("SELECT * FROM tutorials WHERE approve = '0' AND id = '$tid' LIMIT 1");
		// we make a query selecting the tutorial corresponding to the $tid value and with the approve value of ‘o’
		if (mysql_num_rows($a) == 0) {
		// if there are no results returned from the query
			print "No <em>pending</em> tutorial corresponds with that ID<br />\n";
			// error message!
		} else {
		// else, we have results! Let’s display it.
			if (!isset($_POST[pending])) {
			// if the form button has not been pressed,
			// the form button, contains the form data
			// to approve or decline a tutorial
				while ($b = mysql_fetch_object($a)) {
				// once again we grab all the data using mysql_fetch_object();
					print $b->title."<br />\n";
					print $b->subtitle."<br />\n";
					print $b->category."<br />\n";
					print $b->author."<br />\n";
					print $b->content."<br /><br />\n";
					// we print out the tutorial
					print "<form method=\"post\">\n";
					print "<input type=\"radio\" name=\"pendingThis\" value=\"approve\" /> Approve <br /> 
					<input type=\"radio\" name=\"pendingThis\" value=\"decline\" /> Decline<br />\n";
					print "<input type=\"submit\" name=\"pending\" value=\"Submit\"><b r/>\n";
					print "</form>\n";
					// we show a form, allowing the admin to approve or decline the tutorial
				}
			} else {
			// the form button HAS been pressed, let’s see what the admin did.
				if ($_POST[pendingThis] == "approve") {
				// if he approved the tutorial
					$a = mysql_query("UPDATE tutorials SET approve = '1' WHERE id = '$tid'");
					print "The tutorial has been approved, you can now view it in the tutorial list.";
					// we update the approve value of the tutorial with a query
					// and display a success message
				} else {
				// if he didn’t approve the tutorial, he declined it
					$a = mysql_query("UPDATE tutorials SET approve = '2' WHERE id = '$tid'");
					print "The tutorial has been declined!";
					// we update the approve value to 2, and display a success message
				}
			}
		}
		// we close some brackets
	} else {
		print "Invalid ID given!<br />\n";
		// **** there was no id, or it was invalid!, we display an error:(
	}
} else {
	// if $aa is not set,
	// we will display all
	// the pending tutorials!
	$a = mysql_query("SELECT * FROM tutorials WHERE approve = '0'");
	// we select all the unapproved tutorials with the above query!
	if (mysql_num_rows($a) == 0) {
		// we check if the result is equal to 0, if it is:
		print "There are currently no pending tutorials!\n";
		// sorry no tutorials :(
	} else {
		// Else, Yay we have tutorials that are pending!
		while ($b = mysql_fetch_object($a)) {
			// we fetch all the results into a object with variable $b
			print "<a href=\"tutorial.php?act=Admin&mod=Approve&section=Approve&aid=".$b->id."\" target=\"_self\">".$b->title."</a><br />\n";
			print $b->category." | ".$b->author."<br />\n";
			// and we print the results with a link to view and approve them
		}
	}
}
// we close some brackets!
?>


The semi-last piece of code for you guys. The Edit part! No more talking, more coding, let’s go!

<?PHP
// what we are going to do:
// we check if the admin has supplied us with an id,
// if they did we display the edit tutorial form,
// if they don’t we show a list of all the tutorials with
// a link containing the id.
// we check if they presses the submit button of the edit
// tutorial. If they did, we edit the tutorial
// with the new data retrieved from the form.
$editId = $_GET[ei];
// and again we use the link to get some information this time,
// we are asking for ‘ie’ wich is short for EditId.
if (!$editId || !is_numeric($editId)) {
// we check if the var $editId is not set or not numeric, if it is:
	$a = mysql_query("SELECT * FROM tutorials ORDER BY postdate DESC");
	// we select all the tutorials from the database and sorting them by date
	while ($b = mysql_fetch_object($a)) {
	// ONCE again we use mysql_fetch_object(); to fetch the results into an object
		print "<b><a href=\"tutorial.php?act=Admin&mod=Edit&ei=".$b->id."\" target=\"_self\">".$b->title."</a></b><br />\n";
		print $b->subtitle."<br />\n";
		print $b->author." | ".$b->category." | ".$b->postdate."<br /><br />\n";
		// we display some of the tutorial information, including a link to edit it.
	}
} else {
// if $editId is set and is numeric we continue with editing the tutorial.
	if (!$_POST[edittutorial]) {
	// if the Edit button has not been pressed
		$a = mysql_query("SELECT * FROM tutorials WHERE id = $editId");
		while ($b = mysql_fetch_object($a)) {
		// we make a query and fetch the results with mysql_fetch_object();
			print "<form method=\"post\">\n";
			print "<strong>Title:</strong> <br />\n";
			print "<input type=\"text\" value=\"".$b->title."\" name=\"title\"> <br />\n";
			print "<strong>Subtitle:</strong> <br />\n";
			print "<input type=\"text\" value=\"".$b->subtitle."\" name=\"subtitle\"> <br />\n";
			print "<strong>Author:</strong> <br />\n";
			print "<input type=\"text\" value=\"".$b->author."\" name=\"author\"> <br />\n";
			print "<strong>Category:</strong> <br />\n";
			print "<select name=\"category\">
			<option title=\"PHP\">PHP</option>
			<option title=\"HTML\">HTML</option>
			<option title=\"Photoshop\">Photoshop</option>
		</select>\n<br />\n";
			print "<strong>Difficulty:</strong> \n<br />\n";
			print "<select name=\"difficulty\">
			<option title=\"Easy\">Easy</option>
			<option title=\"Medium\">Medium</option>
			<option title=\"Hard\">Hard</option>
		</select>\n<br />\n";
			print "<strong>Access:</strong> \n<br />\n";
			print "<select name=\"acces\">
			<option title=\"All\">All</option>
			<option title=\"Members\">Members</option>
			<option title=\"Staff\">Staff</option>
		</select>\n<br />\n";
			print "<input type=\"hidden\" name=\"tutorialid\" value=\"".$b->id."\">\n";
			print "<strong>Content:</strong> <br />\n";
			print "<textarea name=\"content\" cols=\"60\" rows=\"20\">".$b->content."</textarea> <br />\n";
			print "<input type=\"submit\" name=\"edittutorial\" value=\"Edit Tutorial\">\n";
			print "</form>\n";
			// we display the tutorial inside input fields, allowing the admin to edit it.
		}
	} else {
	// the edittutorial button has been pressed, let’s process the tutorial
		$title = $_POST[title];
		$subtitle = $_POST[subtitle];
		$author = $_POST[author];
		$category = $_POST[category];
		$content = $_POST[content];
		$difficulty = $_POST[difficulty];
		$acces = $_POST[acces];
		$editIdtutorial = $_POST[tutorialid];
		$a = mysql_query("UPDATE tutorials SET title = '$title', subtitle = '$subtitle', author = '$author', category = '$category', content = '$content', acces = '$acces', difficulty = '$difficulty' WHERE id = $editIdtutorial");
		print "The tutorial <strong>".$title."</strong> has successfully been edited!";
		// we assign various variables to the new data, and store the new data into the database
		// and we display a success message!
	}
}
?>


Now the final piece of code has yet to be revealed. Here it comes, the delete section of the admin panel, as usual I commented every line I though was important.

<?PHP
// elseif the $whereAdmin variable is equal to 'Delete', we do this:
	print "Delete tutorial:<br /><br />\n";
	$delId = $_GET[di];
	// and ONCE again we get a bit information from the URL with $_GET[di]
if (!$delId || !is_numeric($delId)) {
		// if $delId is not set, or is not numeric we do this:
		   		$a = mysql_query("SELECT * FROM tutorials WHERE approve = ’1’ ORDER BY postdate DESC");
		// a simple query selecting all the tutorials
				while ($b = mysql_fetch_object($a)) {
		// using a while loop and mysql_fetch_object we grab all the information into a object
					print "<b><a href=\"tutorial.php?act=Admin&mod=Delete&di=".$b->id."\" target=\"_self\">".$b->title."</a></b><br />";
					print $b->subtitle."<br />\n";
					print $b->author." | ".$b->category." | ".$b->postdate."<br /><br />\n";
		// we display the vital information about the tutorial,
		// including a link to the tutorial if the admin
		// wants to see it before they delete it
				}
	} else {
	// else if the id is set and it is numeric:
				if (!$_POST[delnews]) {
		// if the submit button has not been pressed we do this:
					$a = mysql_query("SELECT * FROM tutorials WHERE id = $delId");
		// we make yet another query select the tutorial with the corresponding id
					while ($b = mysql_fetch_object($a)) {
			// using a while loop and mysql_fetch_object we grab all the information into a object
						print "Are you sure you want to delete the article <strong>".$b->title."</strong>?";
						print "<form method=\"post\">";
						print "<input type=\"submit\" value=\"Delete!\" name=\"delnews\">";
				   	 print "</form>";
		// displaying a simple form asking the admin if they are sure they want to delete it
					}
	} else {
	// yes the admin is sure to delete it, and has pressed the submit button.
		   		$a = mysql_query("DELETE FROM tutorials WHERE id = '$delId'") or die ("Could not delete the tutorial!");
		// we delete the record from our database
					print "The article <strong>".$b->title."</strong> has successfully been deleted!";
		// and we tell the admin that 
			}
}
// we close some brackets and we are done!
?>


Well well, so much for this tutorial, as always, questions/suggestions? You know where to ask.

Oh wait, I promised the full working code. . . Hmm check the attachment!

Wildo

Attached File(s)


0

#4 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 18 May 2007 - 08:00 AM

I was a bit confused on how to do the tut at first but i have worked it out.
0

#5 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 18 May 2007 - 08:12 AM

glad to hear that, I hoped it was pretty straightforward, I guess I was wrong :D

Wildo
0

#6 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 18 May 2007 - 09:06 AM

nice tut, will try this tonight :clapping:
0

#7 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 May 2007 - 10:23 PM

Took me a while to work out what was happening, its one large tut. Well done again wildo.
0

#8 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 May 2007 - 10:24 PM

This is a very good CMS, ;) Everyone else seemed to like it, again, well done.
0

#9 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 May 2007 - 10:25 PM

Well done part 2 =p
0

#10 User is offline   olivier 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 279
  • Joined: 22-February 07
  • Reputation: 1
  • Location:Colchester - UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 18 May 2007 - 10:31 PM

Not the most secure/efficient way of doing things but good start ;)
0

#11 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 07:37 AM

nicely done :clapping:
0

#12 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 19 May 2007 - 08:17 AM

View Postolivier, on May 19 2007, 12:31 AM, said:

Not the most secure/efficient way of doing things but good start ;)


not ment to be your free of charge uber super WOW system, it was to teach you how to make the concept of a tutorial cms, if you want to use it, you should add your own security techniques, I was planning on writing a v2 of this system with oop and stuff, but I kinda forgot about it...

Wildo
0

#13 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 10:47 AM

View PostWildo, on May 19 2007, 09:17 AM, said:

not ment to be your free of charge uber super WOW system, it was to teach you how to make the concept of a tutorial cms, if you want to use it, you should add your own security techniques, I was planning on writing a v2 of this system with oop and stuff, but I kinda forgot about it...

Wildo


sounds good :friends: :good:
0

#14 User is offline   olivier 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 279
  • Joined: 22-February 07
  • Reputation: 1
  • Location:Colchester - UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 20 May 2007 - 08:46 PM

View PostWildo, on May 19 2007, 09:17 AM, said:

not ment to be your free of charge uber super WOW system, it was to teach you how to make the concept of a tutorial cms, if you want to use it, you should add your own security techniques, I was planning on writing a v2 of this system with oop and stuff, but I kinda forgot about it...

Wildo

Not having a go mate, just saying ;)
0

#15 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 21 May 2007 - 06:19 AM

so do I :), if you want it secure though, I have some functions that'll easily secure it.

Wildo
0

#16 User is offline   abedaboutaleb 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 30-October 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 30 October 2008 - 12:40 PM

hello i see this is a good tutorial but can you add all files like they should me and send them to me please?, or anyone one other can do this for me `?
0

#17 User is offline   Sandroe 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 24-March 09
  • Reputation: 0
  • Location:Belgium
  • Experience:Advanced
  • Area of Expertise:Designer

Posted 24 March 2009 - 05:08 PM

Very good tut thanx!
0

#18 User is offline   Sam G 

  • Forum Newcomer
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,860
  • Joined: 06-March 09
  • Reputation: 53
  • Gender:Male
  • Location:Dreamland
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 April 2010 - 10:58 AM

Threads merged.
0

#19 User is offline   Remedia 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 18-August 10
  • Reputation: 0
  • Gender:Male
  • Location:East London
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 20 August 2010 - 05:01 PM

Useful , Thanks
0

#20 User is offline   Rajendra 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 60
  • Joined: 16-April 10
  • Reputation: 0
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:System Administrator

Posted 02 September 2010 - 05:41 AM

remarkable starting, it's very nice i'm also learning too.
0

#21 User is offline   AnkitaSNV 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 17-September 10
  • Reputation: 0
  • Gender:Female
  • Location:India
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 17 September 2010 - 01:46 PM

The whole tutorial is useful and a better start, the implementation is very good and I feel newbies can take a quick start.

Thanks
0

#22 User is offline   AnkitaSNV 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 17-September 10
  • Reputation: 0
  • Gender:Female
  • Location:India
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 17 September 2010 - 01:49 PM

This is nice and its a good start. For a newbie it can be worth knowing and better to implement.

Thanks :)

http://www.orangecir...web-design.html
0

#23 User is offline   THallDesign 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 77
  • Joined: 23-September 10
  • Reputation: 4
  • Gender:Male
  • Location:Niagara Falls
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 24 September 2010 - 05:13 PM

Thanks for this tutorial helped me learn a lot actually
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users