Tutorial Management System Part 5: Finishing Up

Posted in PHP,Tutorials.
May 26, 2008

Welcome to the final part of this long tutorial. By now you should have your admin panel up, but not all working, that’s ok. Once we have finished this section it will all be running fine. Every page made in this section will be put into the root folder you were asked to choose in section 1. I will try to explain this section with a fair amount of detail so that everything you have made will start to make sense. I hope you have made records in the category and tutorials table otherwise this section will be a lot more confusing then needed.

With this section we will start with the tutorials.php page. Add the following code and I will explain how it works.

<? include (‘dbconnect.php’); ?>
<h1>Newest Tutorials</h1>
<?php
$query = “SELECT * FROM `tutorials` ORDER BY id DESC LIMIT 0, 10″;
$result = mysql_query($query)
or die(“Error in query: $query . ” . mysql_error()); 

if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_row($result))
{

$query1 = “SELECT * FROM category WHERE id = ‘”.$row[4].”‘”;
$result1 = mysql_query($query1)
or die (“Error in query: $query1. ” . mysql_error());
$row2 = mysql_fetch_object($result1);
$category = $row2->name;
?>
<table width=”100%” cellpadding=”3″ cellspacing=”0″>
<tr>
<td><a href=”viewtut.php?id=<?php echo $row[0]; ?>”><img src=”<?php echo $row[3]; ?>” alt=”<?php echo $row[1]; ?>” border=”0″></a></td>
<td valign=”top” width=”100%”><h2><a href=”viewtut.php?id=<?php echo $row[0]; ?>”><?php echo $row[1]; ?></a></h2><b>Category: </b><?php echo $category; ?></td>
</tr>
<tr>
<td colspan=”2″><?php echo $row[6]; ?></td>
</tr>
</table>
<hr>
<?php
}
}

mysql_close($connection);
?>

This page is not one of the more important ones, but it is a good page for explaining how things work. First of all, it will get the 10 newest records from the tutorials table and display them from newest to oldest. It then puts all the info into a table and displays it nicely. You may recall a few sections back I said that when you choose a category for your tutorial it will put the tutorials ID, so if I were to echo that record it would give me a number rather then the categories name. Well its still the same, but we have managed to get around it. We use that ID number of the category to fetch any categories name that has that ID, of course there will only be one. The name is then placed into the variable $category and we echo it, giving us a word instead of a number.

Lets move onto category.php. Create the document and add this code;

<?php
include (“dbconnect.php”);
$id = $_GET['id']; 

if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$max_results = 10;
$from = (($page * $max_results) – $max_results);
?>
<h1>Newest Tutorials</h1>
<?php
$query = “SELECT * FROM `tutorials` WHERE category = ‘$id’ ORDER BY id DESC LIMIT 0, 10″;
$result = mysql_query($query)
or die(“Error in query: $query . ” . mysql_error());

if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_row($result))
{

$query1 = “SELECT * FROM category WHERE id = ‘”.$row[4].”‘”;
$result1 = mysql_query($query1)
or die (“Error in query: $query1. ” . mysql_error());
$row2 = mysql_fetch_object($result1);
$category = $row2->name;
?>
<table width=”100%” cellpadding=”3″ cellspacing=”0″>
<tr>
<td width=”40″><a href=”viewtut.php?id=<?php echo $row[0]; ?>”><img src=”../images/avatars/<?php echo $row[3]; ?>” alt=”<?php echo $row[1]; ?>” height=”40″ width=”40″ border=”0″></a></td>
<td valign=”top” width=”100%”><h2><a href=”viewtut.php?id=<?php echo $row[0]; ?>”><?php echo $row[1]; ?></a></h2><b>Category: </b><?php echo $category; ?></td>
</tr>
<tr>
<td colspan=”2″><?php echo $row[6]; ?></td>
</tr>
</table>
<hr>
<?php
}
}
?>
<?php
echo ‘<center>’;

$total_results = mysql_result(mysql_query(“SELECT COUNT(*) as Num FROM tutorials WHERE category = ‘$id’”),0);

$total_pages = ceil($total_results/$max_results);
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo’['.$i.']‘;
}else{
echo “<a href=’?page=$i’>$i</a> “;
} }
echo ‘<br>Page ‘.$page.’ of ‘.$total_pages;
echo ‘</span>’;

mysql_close($connection);
?>

This page works much the same as tutorials.php, but it will only display tutorials from a certain category. It knows what category to use by fetching the categories id from the URL. For example, if you go to www.yoursite.com/category.php it will give you a blank document, but if you go to www.yoursite.com/category.php?id=2 it will give you all the tutorials under the category with the ID number 2. It does this by checking all the tutorial records and fetching the ones that have ‘2’ in their category field.

Now we come to one of the most crucial pages in the entire system, viewtut.php. That’s because this is the page that displays the tutorials themselves. Create the page and add this code.

<? include (‘dbconnect.php’); 

if ((!isset($_GET['id']) || trim($_GET['id']) == ”))
{
die (‘Missing record ID!’);
}

$id = $_GET['id'];
$query = “SELECT * FROM tutorials WHERE id = ‘$id’”;
$result = mysql_query($query)
or die (“Error in query: $query. ” . mysql_error());
$row = mysql_fetch_row($result);

$query1 = “SELECT * FROM category WHERE id = ‘”.$row[4].”‘”;
$result1 = mysql_query($query1)
or die (“Error in query: $query1. ” . mysql_error());
$row2 = mysql_fetch_object($result1);
$category = $row2->name;

if (mysql_num_rows($result) > 0)
{
?>
<h1><?php echo $row[1]; ?></h1>
<table width=”100%”>
<tr>
<td><b>Category: </b><?php echo $category; ?></td>
</tr>
<tr>
<td><b>Skill Level:</b> <?php echo $row[5]; ?><hr class=”news” /></td>
</tr>
<tr>
<td><?php echo stripslashes($row[7]); ?></h1></td>
</tr>
<tr>
<td><span class=”poster”>Writen By <?php echo $row[8]; ?></span></td>
</tr>
</table>
<hr />
<?php
}
else
{
echo ‘Tutorial not found!’;
}

$query = “SELECT * FROM comments WHERE id = ‘$id’ AND cat = ‘tutorials’ ORDER BY cid ASC”;
$result = mysql_query($query)
or die (“Error in query: $query. ” . mysql_error());

if (mysql_num_rows($result) > 0)
{

while($row = mysql_fetch_row($result))
{
$comment = stripslashes(strip_tags(htmlspecialchars($row[6], ENT_QUOTES)));
?>
<p class=”comment”>
<b><? echo $row[7]; ?> – <?php echo htmlentities($row[4]); ?>, <?php echo stripslashes(htmlentities($row[5])); ?></b><br>
<?php echo $comment; ?>
</p>
<?php
}

}
else
{
echo ‘<p class=”comments”>No Comments</p>’;
}

?>
<hr>
<form action=”post.php?id=<?php echo $id; ?>” method=”post” enctype=”multipart/form-data”>
<h2>Add Comment</h2>
<h2>Name</h2>
<input name=”name” type=”text” size=”50″><br>
<br>
<h2>Email</h2>
<input name=”email” type=”text” size=”50″><br>
<br>
<h2>Comment, HTML is off.</h2>
<textarea name=”comment” cols=”50″ rows=”4″></textarea>
<br>
<br>
<input name=”submit” type=”Submit” value=”Submit”>
<input type=”reset”>
</form>
<?php
mysql_close($connection);
?>

This page works in the same way that category does, in the way that it wont work without ?id= on the end. Only difference is that this time it uses the tutorials ID instead of the categories. There’s not a great deal on this page that needs explaining because most of it has been used before on another page. But you may notice that near the bottom there is a form that is processed my post.php. This is the comments form and will be used to add comments. But there’s no post you may say. That’s our next and final page of the tutorial.

Our last page, once this one is complete you system should be working beautifully. So lets go ahead and make it. Create a page called post.php and add this code;

<?php
include (“dbconnect.php”); 

$id = $_GET['id'];
$ip = $_SERVER['REMOTE_ADDR'];
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$query = “INSERT INTO comments (id, ip, cat, name, email, comment, date) VALUES ( ‘”.$id.”‘, ‘”.$ip.”‘, ‘tutorials’, ‘”.$name.”‘, ‘”.$email.”‘, ‘”.$comment.”‘, NOW())”;
mysql_query($query)or die(mysql_error());

echo ‘Comment posted!<a href=”viewtut.php?id=’.$id.’”>Go back.</a>’;
?>

Pretty simple looking for a final page isn’t it. That’s because it is very simple. When you hit submit on the form from the viewtut.php it sends you to this page, but it uses a ?id= so that it knows what tutorial to add it to. This page simply adds the information to the comments table and then redirects you back to the page you came from using header (“Location:”.$HTTP_REFERER); You will never even see this page.

So here we are, guess what, your done! Yep, that’s it, its all done. I thank you heaps for reading this tutorial that took so long to write. You can use the links below to navigate to any of the previous sections and you will also find a link to the zip of all the files in this tutorial if your too lazy to make them yourself or have screwed up, or if your just plain smart enough that you don’t need to read the tutorial. I hope this works for well for you. And I advise you password protect the admin folder. I have not added that into this tutorial, but most cPanel users will be able to do it with cPanel. Again, I thank you for reading this big long tutorial.

« Previous [1] [2] [3] [4] [5] | Download ZIP


More...


Join our RSS feed

Digg

Del.icio.us

Reddit

Furl


5 Replies...

scott

You have alot of syntec errors through out the entire script.
Did you even test any of this prior to posting it?
The fact that i get nothing but errors when i attempt to actually use the script.
I followed the tutorials exactly.
And because of that, i DO get errors.

Google

Domenico

Well the code is copied and pasted from the code I’m using in the directory above. It’s possible that something has been copied wrong or put in the wrong place, although I’m sure I checked it. I will check it again this afternoon and edit this comment accordingly.

Until then, try the download link. I know for a fact that that one works because all I did was zip the directory.

EDIT
You were right, it does produce ALOT of errors. However, this is not caused by the code, the code as it is shown is absolutely fine. The problem is the way WordPress outputs symbols. The symbol ‘ does not come out correctly on a coding program when copied from a WordPress page. I am unsure how to fix this, but I do advise that everyone download the files.
Second EDIT
Ok, the code is fixed. It was painstaking, but its fixed. Scott, you were partially correct and I thank you for pointing it out, however it was of no fault of either of us. The code is now copy and paste safe.

deluxe

not download

XtrackterX

please add the .zip with a working one?

Designkai

Sorry, I have fixed this.

RSS feed for comments on this post. TrackBack URL

Leave a comment