Welcome to the second part of the 5 section tutorial on how to create a Tutorial Management system. This section we will be looking at managing the categories for your tutorials. We will be working only in the admin folder that we set up in Part 1, so there is no need to use any other folder. Every page we create in this section will go into the admin folder.
Let’s start. Create a document named ‘category.php’ and add the following code to it.
$query = "SELECT * FROM `category` ORDER BY id ASC";
$result = mysql_query($query)
or die("Error in query: $query . " . mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_object($result))
{
?>
<h1><?php echo $row->id; ?>: <?php echo $row->name; ?> <a href="edit_category.php?id=<?php echo $row->id; ?>">Edit category</a> | <a href="delete_category.php?id=<?php echo $row->id; ?>" onclick="return confirm('Are you sure you want to delete category?')">Delete category</a></h1>
<h2>Description</h2>
<?php echo $row->description; ?><br />
<img src="<?php echo $row->avatar ?>" alt="<?php echo $row->name; ?>"><hr>
<?php
}
}
else
{
echo 'No Catagories';
}
echo '<a href="add_category.php">Add category</a> | <a href="index.php">Go back to admin home page</a>';
mysql_close($connection);
?>
This page will display all your categories and their information and give you links to manage the information. It will not work at the moment, so don’t bother trying to use it.
Now we move onto add_category.php. I will have to start explaining in depth how this works otherwise you will not understand what I’m talking about later on. Ok, create add_category.php and add the following code into it.
$query = "INSERT INTO category (name, avatar, description, site) VALUES ( '".$name."', '".$avatar."', '".$description."', '".$site."')";
mysql_query($query)or die(mysql_error());
echo 'Category Successfully added! <a href="index.php">Go back to admin home page</a>';
}
mysql_close($connection);
?>
This page will give you a simple form. Create this page and then use it to create a few categories that we can use to help explain the rest of the tutorial later on. Once you have added a few records, parts of category.php will begin to work.
Now lets create edit_category.php. I think the name on this page is self explanatory, it edits the information for your categories. Add the following code to your edit_category.php page.
if (!isset($_POST['submit']))
{
if ((!isset($_GET['id']) || trim($_GET['id']) == ''))
{
die ('Missing record ID!');
}
$id = $_GET['id'];
$query = "SELECT * FROM category WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
if (mysql_num_rows($result) > 0)
{
$row = mysql_fetch_object($result);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<h1>Edit Catagoy #<?php echo $id; ?></h1>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<h2>Name</h2>
<input value="<?php echo stripslashes($row->name); ?>" name="name" type="text" size="50"><br>
<br>
<h2>Avatar</h2>
<input value="<?php echo stripslashes($row->avatar); ?>" name="avatar" type="text" size="50"><br>
<br>
<h2>Description</h2>
<textarea name="description" cols="50" rows="4"><?php echo stripslashes($row->description); ?></textarea>
<br>
<br>
<h2>Site</h2>
<input value="<?php echo stripslashes($row->site); ?>" name="site" type="text" size="50"><br>
<p>
<input name="submit" type="Submit" value="Submit">
<input type="reset">
</p>
</form>
<?php
}
else
{
echo 'News item not found!';
}
}
else
{
$id = $_POST['id'];
$name = $_POST['name'];
$avatar = $_POST['avatar'];
$description = $_POST['description'];
$site = $_POST['site'];
$query = "UPDATE category SET name = '".$name."', avatar = '".$avatar."', description = '".$description."', site = '".$site."' WHERE id = '".$id."'";
$result = mysql_query($query)or die(mysql_error());
echo 'Category successfully edited. <a href="index.php">Go back to admin home page</a>';
}
mysql_close($connection);
?>
Ok, this form uses the ?id= in the URL (when followed from a link) to fetch the id of the category you wish to edit. It then gets all the info from that category and outputs it in the correct places on the form. When you edit it and hit submit it will overwrite all the old details with the ones in the form. All the edit files will work the same way through this tutorial, so keep that in mind. If you wish to edit a category you have to go to category.php and follow the links otherwise it may not work.
Now our final page for this section of the tutorial, delete_category.php. Create the file and put this code into it.
if ((!isset($_GET['id']) || trim($_GET['id']) == ''))
{
die('Missing record ID!');
}
$id = $_GET['id'];
$query = "DELETE FROM category WHERE id = '$id'";
$result = mysql_query($query)
or die ("Error in query : $query. " . mysql_error());
$query = "SELECT id FROM tutorials WHERE category = '".$_GET['id']."'";
$result = mysql_query($query)
or die ("Error in query : $query. " . mysql_error());
while ($row = mysql_fetch_object($result))
{
$query = "DELETE FROM comments WHERE id = '".$row->id."'";
$result = mysql_query($query)
or die ("Error in query : $query. " . mysql_error());
}
$query = "DELETE FROM tutorials WHERE category = '$id'";
$result = mysql_query($query)
or die ("Error in query : $query. " . mysql_error());
echo 'Category records, Tutorial records and Comment records successfully deleted. <a href="index.php">Go back to admin home page</a>';
mysql_close($connection);
?>
The code in this document is quite long. Like edit_category.php it fetches the ?id= from the URL, but it then deletes any category records with that id. It also finds any tutorials with that category record, fetches the comments from it, deletes the comments and then deletes the tutorials. Its advised you move tutorials out of this category before you delete it.
That’s it, another section completed. Thank you for reading it and follow the links to the other sections of the tutorial.
« Previous [1] [2] [3] [4] [5] Next » | Tutorial System ZIP (124)
No comments yet.
RSS feed for comments on this post. TrackBack URL