Well hello there, I thought it was time for another tutorial, this time I'll 'try' to teach y'all how to write a 'decent' news cms! so let's get started shall we?
Contains:
Latest News
Full article view
Archive with fancy pagination
Admin panel including add, edit and delete functions
Requires:
a server with PHP enabled
a MySQL database
notepad
NOTE: if you intent to use this code on your website, take caution, this news cms comes with no security, so if you intend to use it, add a decent security to it, this script is NOT, I repeat not secure in any way!
Now we got that sorted out, let's finally start...
first of all, we need a MySQL table to let this work, so I'll just give you the code and just assume you know how to create it and stuff...!
CREATE TABLE `SW_News` ( `id` int(11) NOT NULL auto_increment, `author` varchar(25) NOT NULL, `title` varchar(255) NOT NULL default '', `category` varchar(100) NOT NULL, `date` varchar(20) NOT NULL, `content` TEXT NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;Ok, I think we are ready for some php
Open up your notepad, dreamweaver or whatever you’re using!
First of all, we need to connect to our database… so let’s do that, shall we?
<?PHP $host = "****"; $user = "****"; $pass = "****"; $db = "****"; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db, $connect); ?>That’s just easy connecting baby, note that I didn’t use or die (mysql_error()); simply because I already tested the whole code and there were no errors, if you’re reading to learn from this, which I hope you are, I advise you to use or die (mysql_error()); after each query so you’re sure there are no errors there!
Now we are going to display 3 links linking to the 3 sections (there are 4 sections, but 1 section doesn’t require a link), the link URL might confuse you now, but you’ll understand it ( hopefully ) after this tutorial
<?PHP print "<a href=\"index.php\" target=\"_self\">News Index</a> | "; print "<a href=\"index.php?v=nar\" target=\"_self\">News Archive</a> | "; print "<a href=\"index.php?v=aa\" target=\"_self\">News Admin</a> "; print "<br /><br />"; ?>The next thing we are going to do, is to make some fancy links, YAY, besides the INSANE COOL look, it also adds some functionality to our script
<?PHP
switch ($_GET[v]) {
default:
?> That piece is simple, we are ‘switching’ to the part displayed in the link after ‘?v=’, and if there is no ‘v’ set, the script will show the default part, which we are going to create now. First things first, what do we want to display in the default part? Right, you’re a smart boy/girl, the latest news! Woohooo!<?PHP
print "<strong>News Index:</strong>";
print "<br /><br />";
$sql = "SELECT * FROM SW_News ORDER BY date DESC LIMIT 3";
$rs = mysql_query($sql, $connect);
if(mysql_num_rows($rs) > 0) {
while($row = mysql_fetch_object($rs)) {
$content = nl2br($row->content);
if (strlen($content) > 750) {
$content = substr ("$content", 0, 500);
$content .= "...<br /><div align=\"center\"><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">Full article</a></div>";
} else {
$content .= "";
}
print "<div>";
print "<b><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">".$row->title."</a></b><br />";
print $content;
print "<div align=\"right\">";
print $row->author." | ".$row->category." | ".$row->date;
print "</div>";
print "<hr color=\"#CCCCCC\" size=\"1px\">";
print "</div>";
}
} else {
print "Sorry, no results found.";
}
break;
?> Ok that was a load of code wasn’t it, let’s explain what I’m doing! We started of by printing a bold text displaying where we are! The ‘News Index’! then we created a query selecting the latest 3 records from our SW_News table, you can change the LIMIT 3 to whatever floats your boat, customizing for the win baby! Then we checked if we the result of our query was greater then 0, because if we don’t have any records to display, we’ll have a problem, so with the else statement I printed if no results, ‘sorry, no results found!’. Neat huh! I used mysql_fetch_object instead of mysql_fetch_array, the only difference is that the MySQL table results are treated as a object instead of putted in a array, no biggie baby!<?PHP
$content = nl2br($row->content);
if (strlen($content) > 750) {
$content = substr ("$content", 0, 500);
$content .= "...<br /><div align=\"center\"><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">Full article</a></div>";
} else {
$content .= "";
}
?> Ok that might confused you. But it’s not that difficult. Using the n12br() function, we allow line breaks in our $content variable. After that it gets tricky!Here’s the final code, for the copy+pasters amongst us.
Using strlen() we check if the string, in this case $content, is longer that 750 characters, if it is, we use substr() to select only the first 500 characters to avoid our news index to get to lengthy, after that we use ‘.=’ to ‘add’ something to the $content variable. We add ‘… AND A LINK TO THE FULL ARTICLE’, if the $content variable is not longer then 750 characters we add nothing to our $content variable! Let’s continue!
We now arrived at a new ‘case’, the ‘fa’ case, which is short for ‘Full article’! here’s some code to look at!
<?PHP
case 'fa';
print "<strong>Full Article:</strong>";
print "<br /><br />";
$getTheId = $_GET[id];
if (!$getTheId) {
print "Please enter a article id to display!";
} else {
if (!is_numeric($_GET[id])) {
print "Sorry, invalid <strong>ID</strong> given!";
} else {
$newsId = $_GET[id];
$getNews = "SELECT * FROM SW_News WHERE id = $newsId";
$newsInbound = mysql_query($getNews, $connect);
while ($displayNews = mysql_fetch_object($newsInbound)) {
$content = nl2br($displayNews->content);
print "<div>";
print "<b>".$displayNews->title."</b><br />";
print $content;
print "<div align=\"right\">";
print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date;
print "</div>";
print "</div>";
}
}
}
break;
?> We are doing the same thing at the beginning here. We display in bold text where we are, then we are going to get some information from our url, the ‘id’ part, so if our url looked like index.php?v=fa&id=123 we’d be looking at id=123. Then we check if there is any information at all in the ‘id’ if not, we display an error, then we check if the ‘id’ is at least a number, if it isn’t, we display a error again, if it is, we continue! Then we create a query, select the news article with the corresponding id, and we display it, the display part is fully editable as long as you don’t touch the variables! YAY, we finished another case!Next up is the News archive. This will be a big chunk of code, so enjoy, I’ll explain it afterwards!
<?PHP
case 'nar':
print "<strong>News Archive:</strong>";
$pageNumber = (isset($_GET["p"]) && is_numeric($_GET["p"])) ? $_GET["p"] : 1;
$perPage = 10;
$padding = 3;
$startIndex = ($pageNumber * $perPage) - $perPage;
$totalCount = "SELECT COUNT(*) as 'Total' FROM SW_News";
$rsCount = mysql_query($totalCount, $connect);
$rowCount = mysql_fetch_object($rsCount);
$numOfPages = ceil($rowCount->Total / $perPage);
print "<div align=\"center\">";
print "<a href=\"index.php?v=nar&p=1\">«</a> ";
if(($pageNumber - $padding) > 1) {
print "... ";
$lowerLimit = $pageNumber - $padding;
for($i = $lowerLimit; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
} else {
for($i = 2; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) {
print "<b> - " . $pageNumber . " - </b>";
}
if(($pageNumber + $padding) < $numOfPages) {
$upperLimit = $pageNumber + $padding;
for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
print "... ";
} else {
for($i = ($pageNumber + 1); $i < $numOfPages; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>";
print "</div>";
$getAllNews = "SELECT * FROM SW_News ORDER BY date DESC LIMIT $startIndex, $perPage";
$newsAllInbound = mysql_query($getAllNews, $connect);
while ($displayAllNews = mysql_fetch_object($newsAllInbound)) {
print "<b><a href=\"index.php?v=fa&id=".$displayAllNews->id."\" target=\"_self\">".$displayAllNews->title."</a></b>";
print "<br />";
print $displayAllNews->author." | ".$displayAllNews->category." | ".$displayAllNews->date;
print "<br /><br />";
}
print "<div align=\"center\">";
print "<a href=\"index.php?v=nar&p=1\">«</a> ";
if(($pageNumber - $padding) > 1) {
print "... ";
$lowerLimit = $pageNumber - $padding;
for($i = $lowerLimit; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
} else {
for($i = 2; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) {
print "<b> - " . $pageNumber . " - </b>";
}
if(($pageNumber + $padding) < $numOfPages) {
$upperLimit = $pageNumber + $padding;
for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
print "... ";
} else {
for($i = ($pageNumber + 1); $i < $numOfPages; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>";
print "</div>";
break;
?> Ok well, we start as usual, we display where we are , then we start our navigation! NOTE: I didn’t write the navigation code myself, I was being lazy and I used the code found on www.sampsonresume.com to add pagination to the archive, so the credit for the pagination goes to ‘sampson’, the pagination is a bit hard to explain in text, so I suggest you take a look at the video tutorial that ‘sampson’ wrote on his website, this is the link http://www.sampsonresume.com/2007/02/01/sc...php-pagination/ . one thing left about this case, you should take a look at<?PHP print "<b><a href=\"index.php?v=fa&id=".$displayAllNews->id."\" target=\"_self\">".$displayAllNews->title."</a></b>"; ?>
We are linking to the full article case there, to view that article, so all the cases are ‘connected’ so to say with each other! Buh, I’m tired of writing already, and the hardest part is incoming as I’m writing… the ADMIN panel… WOOoooHOoooo! If you’re already lost, don’t worry, I’ll post the whole WORKING code at the bottom of this tutorial.
Oh boy, here we go, let’s start a new case!
<?PHP
case 'ad':
$adminPage = $_GET[aa];
if (!$adminPage) {
print "<strong>News Admin:</strong>";
print "<br /><br />";
print "<a href=\"index.php?v=ad&aa=add\" target=\"_self\">Add News</a> <br />";
print "<a href=\"index.php?v=ad&aa=edit\" target=\"_self\">Edit News</a> <br />";
print "<a href=\"index.php?v=ad&aa=del\" target=\"_self\">Delete News</a>";
}
?> We begin, as usual, with our bolded text where we are, then we check if ‘aa’ (short for admin action) was inserted in the url, and if it was not, we display our admin index which contains 3 links to the 3 sections inside the admin panel, add, edit and delete. Let’s continue.<?PHP
if ($adminPage == "add") {
print "<strong>News Admin:</strong>";
print "<br /><br />";
$checkButton = $_POST[addnews];
if (!$checkButton) {
print "<form method=\"post\">";
print "<strong>Title:</strong> <br />";
print "<input type=\"text\" name=\"title\"> <br />";
print "<strong>Author:</strong> <br />";
print "<input type=\"text\" name=\"author\"> <br />";
print "<strong>Category:</strong> <br />";
print "<select name=\"category\">
<option title=\"yourcategory0\">Yourcategory0</option>
<option title=\"yourcategory1\">Yourcategory1</option>
<option title=\"yourcategory2\">Yourcategory2</option>
<option title=\"yourcategory3\">Yourcategory3</option>
<option title=\"yourcategory4\">Yourcategory4</option>
</select><br />";
print "<strong>Content:</strong> <br />";
print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> <br />";
print "<input type=\"submit\" name=\"addnews\" value=\"Add News\">";
print "</form>";
} else {
$username = $_POST[author];
$title = $_POST[title];
$category = $_POST[category];
$content = $_POST[content];
if (!$username || !$title || !$category || !$content) {
print "You forgot a field! Please go back and fill all the fields!";
} else {
$ndate = date('H:i d-m-Y');
$addingNews = "INSERT INTO SW_News (author, title, category, date, content)
VALUES('$username', '$title', '$category', '$ndate', '$content')";
$processNew = mysql_query($addingNews, $connect);
print "News item added successfully!";
}
}
}
?> Well that’s the code to add news to our database, let’s break it down shall we!, first we check if the ‘aa’ in the link says ‘ add’ and if it does we start, we check if the form has been submitted, if it isn’t, we display the form, if it is, we process the form. I won’t explain that part to good, I don’t think it’s that hard php, but if you really don’t get what I’m doing here, feel free to ask, I’m always in to help people! Let’s get going with the edit part!<?PHP
if ($adminPage == "edit") {
print "<strong>News Admin:</strong>";
print "<br /><br />";
$editId = $_GET[ei];
if (!$editId || !is_numeric($editId)) {
$getNews = "SELECT * FROM SW_News ORDER BY date DESC";
$newsInbound = mysql_query($getNews, $connect);
while ($displayNews = mysql_fetch_object($newsInbound)) {
$content = nl2br($displayNews->content);
print "<div>";
print "<b><a href=\"index.php?v=ad&aa=edit&ei=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />";
print $content;
print "<div align=\"right\">";
print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date;
print "</div>";
print "</div>";
}
} else {
if (!$_POST[editnews]) {
$getTheIdNews = "SELECT * FROM SW_News WHERE id = $editId";
$newsInbound1 = mysql_query($getTheIdNews, $connect);
while ($displayEditNews = mysql_fetch_object($newsInbound1)) {
print "<form method=\"post\">";
print "<strong>Title:</strong> <br />";
print "<input type=\"text\" value=\"".$displayEditNews->title."\" name=\"title\"> <br />";
print "<strong>Author:</strong> <br />";
print "<input type=\"text\" value=\"".$displayEditNews->author."\" name=\"author\"> <br />";
print "<strong>Category:</strong> <br />";
print "<select name=\"category\">
<option title=\"yourcategory\" selected>Yourcategory</option>
<option title=\"yourcategory1\">Yourcategory1</option>
<option title=\"yourcategory2\">Yourcategory2</option>
<option title=\"yourcategory3\">Yourcategory3</option>
<option title=\"yourcategory4\">Yourcategory4</option>
</select><br />";
print "<input type=\"hidden\" name=\"newsid\" value=\"".$displayEditNews->id."\">";
print "<strong>Content:</strong> <br />";
print "<textarea name=\"content\" cols=\"60\" rows=\"20\">".$displayEditNews->content."</textarea> <br />";
print "<input type=\"submit\" name=\"editnews\" value=\"Edit News\">";
print "</form>";
}
} else {
$title = $_POST[title];
$author = $_POST[author];
$category = $_POST[category];
$content = $_POST[content];
$editIdNews = $_POST[newsid];
$updateTheNews = "UPDATE SW_News SET title = '$title', author = '$author', category = '$category', content = '$content' WHERE id = $editIdNews";
$fini****Up = mysql_query($updateTheNews, $connect);
print "The article <strong>".$title."</strong> has successfully been edited!";
}
}
}
?> First of all, we need to grab an id from a list to know which article we’ll be updating, so we first display a list of all the news articles, then if you click on one of them, we retrieve the id of the corresponding article and we select the data from that article! We then display all those data in a form, so that is it ready for editing. Once you pressed the submit button, we do the same as in the add case only this time we UPDATE instead of INSERT. Easy huh? Anyways here comes the delete part,
<?PHP
if ($adminPage == "del") {
print "<strong>News Admin:</strong>";
print "<br /><br />";
$delId = $_GET[di];
if (!$delId || !is_numeric($delId)) {
$getNews = "SELECT * FROM SW_News ORDER BY date DESC";
$newsInbound = mysql_query($getNews, $connect);
while ($displayNews = mysql_fetch_object($newsInbound)) {
$content = nl2br($displayNews->content);
if (strlen($content) > 750) {
$content = substr ("$content", 0, 500);
$content .= " ...";
} else {
$content .= "";
}
print "<div>";
print "<b><a href=\"index.php?v=ad&aa=del&di=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />";
print $content;
print "<div align=\"right\">";
print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date;
print "</div>";
print "</div>";
}
} else {
if (!$_POST[delnews]) {
$getTheIdNews = "SELECT * FROM SW_News WHERE id = $delId";
$newsInbound1 = mysql_query($getTheIdNews, $connect);
while ($displayDelTitle = mysql_fetch_object($newsInbound1)) {
print "Are you sure you want to delete the article <strong>".$displayDelTitle->title."</strong>?";
print "<form method=\"post\">";
print "<input type=\"submit\" value=\"Delete!\" name=\"delnews\">";
print "</form>";
}
} else {
$updateTheNews = "DELETE FROM SW_News WHERE id = '$delId'";
$fini****Up = mysql_query($updateTheNews, $connect);
print "The article <strong>".$title."</strong> has succesfully been deleted!";
}
}
}
break;
?> The delete part is actually quiet easy, we do the same as in the edit case, we create a list of all the articles with clickable titles, if you click on a news title, you will get a question ‘ are you sure you want to delete’ then if you are sure it deletes the article from our table.That’s it, oh wait, we forgot to close some things!
<?PHP } mysql_close($connect); ?>We just closed the ‘switch’ statement from the top of the page, and we closed our mysql connection, and we’re done,
I hope you learned something, if you encounter any problems or just have a question, feel free to comment below. I realize I didn’t explain all there is to it what I’ve just shown you, but if you want me to explain it more thoroughly, feel free to ask!
Here’s the full code btw:
<?php
$host = "****";
$user = "****";
$pass = "****";
$db = "****";
$connect = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db, $connect) or die(mysql_error());
print "<a href=\"index.php\" target=\"_self\">News Index</a> | ";
print "<a href=\"index.php?v=nar\" target=\"_self\">News Archive</a> | ";
print "<a href=\"index.php?v=ad\" target=\"_self\">News Admin</a> ";
print "<br /><br />";
switch ($_GET[v]) {
default:
print "<strong>News Index:</strong>";
print "<br /><br />";
$sql = "SELECT * FROM SW_News ORDER BY date DESC LIMIT 3";
$rs = mysql_query($sql, $connect);
if(mysql_num_rows($rs) > 0) {
while($row = mysql_fetch_object($rs)) {
$content = nl2br($row->content);
if (strlen($content) > 750) {
$content = substr ("$content", 0 , 500);
$content .= "...<br /><div align=\"center\"><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">Full article</a></div>";
} else {
$content .= "";
}
print "<div>";
print "<b><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">".$row->title."</a></b><br />";
print $content;
print "<div align=\"right\">";
print $row->author." | ".$row->category." | ".$row->date;
print "</div>";
print "<hr color=\"#CCCCCC\" size=\"1px\">";
print "</div>";
}
} else {
print "Sorry, no results found.";
}
break;
case 'fa';
print "<strong>Full Article:</strong>";
print "<br /><br />";
$getTheId = $_GET[id];
if (!$getTheId) {
print "Please enter a article id to display!";
} else {
if (!is_numeric($_GET[id])) {
print "Sorry, invalid <strong>ID</strong> given!";
} else {
$newsId = $_GET[id];
$getNews = "SELECT * FROM SW_News WHERE id = $newsId";
$newsInbound = mysql_query($getNews, $connect);
while ($displayNews = mysql_fetch_object($newsInbound)) {
$content = nl2br($displayNews->content);
print "<div>";
print "<b>".$displayNews->title."</b><br />";
print $content;
print "<div align=\"right\">";
print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date;
print "</div>";
print "</div>";
}
}
}
break;
case 'nar':
print "<strong>News Archive:</strong>";
$pageNumber = (isset($_GET["p"]) && is_numeric($_GET["p"])) ? $_GET["p"] : 1;
$perPage = 10;
$padding = 3;
$startIndex = ($pageNumber * $perPage) - $perPage;
$totalCount = "SELECT COUNT(*) as 'Total' FROM SW_News";
$rsCount = mysql_query($totalCount, $connect);
$rowCount = mysql_fetch_object($rsCount);
$numOfPages = ceil($rowCount->Total / $perPage);
print "<div align=\"center\">";
print "<a href=\"index.php?v=nar&p=1\">«</a> ";
if(($pageNumber - $padding) > 1) {
print "... ";
$lowerLimit = $pageNumber - $padding;
for($i = $lowerLimit; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
} else {
for($i = 2; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) {
print "<b> - " . $pageNumber . " - </b>";
}
if(($pageNumber + $padding) < $numOfPages) {
$upperLimit = $pageNumber + $padding;
for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
print "... ";
} else {
for($i = ($pageNumber + 1); $i < $numOfPages; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>";
print "</div>";
$getAllNews = "SELECT * FROM SW_News ORDER BY date DESC LIMIT $startIndex, $perPage";
$newsAllInbound = mysql_query($getAllNews, $connect);
while ($displayAllNews = mysql_fetch_object($newsAllInbound)) {
print "<b><a href=\"index.php?v=fa&id=".$displayAllNews->id."\" target=\"_self\">".$displayAllNews->title."</a></b>";
print "<br />";
print $displayAllNews->author." | ".$displayAllNews->category." | ".$displayAllNews->date;
print "<br /><br />";
}
print "<div align=\"center\">";
print "<a href=\"index.php?v=nar&p=1\">«</a> ";
if(($pageNumber - $padding) > 1) {
print "... ";
$lowerLimit = $pageNumber - $padding;
for($i = $lowerLimit; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
} else {
for($i = 2; $i < $pageNumber; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) {
print "<b> - " . $pageNumber . " - </b>";
}
if(($pageNumber + $padding) < $numOfPages) {
$upperLimit = $pageNumber + $padding;
for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
print "... ";
} else {
for($i = ($pageNumber + 1); $i < $numOfPages; $i++) {
print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> ";
}
}
print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>";
print "</div>";
break;
case 'ad':
$adminPage = $_GET[aa];
if (!$adminPage) {
print "<strong>News Admin:</strong>";
print "<br /><br />";
print "<a href=\"index.php?v=ad&aa=add\" target=\"_self\">Add News</a> <br />";
print "<a href=\"index.php?v=ad&aa=edit\" target=\"_self\">Edit News</a> <br />";
print "<a href=\"index.php?v=ad&aa=del\" target=\"_self\">Delete News</a>";
}
if ($adminPage == "add") {
print "<strong>News Admin:</strong>";
print "<br /><br />";
$checkButton = $_POST[addnews];
if (!$checkButton) {
print "<form method=\"post\">";
print "<strong>Title:</strong> <br />";
print "<input type=\"text\" name=\"title\"> <br />";
print "<strong>Author:</strong> <br />";
print "<input type=\"text\" name=\"author\"> <br />";
print "<strong>Category:</strong> <br />";
print "<select name=\"category\">
<option title=\"yourcategory\">Yourcategory</option>
<option title=\"yourcategory1\">Yourcategory1</option>
<option title=\"yourcategory2\">Yourcategory2</option>
<option title=\"yourcategory3\">Yourcategory3</option>
<option title=\"yourcategory4\">Yourcategory4</option>
</select><br />";
print "<strong>Content:</strong> <br />";
print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> <br />";
print "<input type=\"submit\" name=\"addnews\" value=\"Add News\">";
print "</form>";
} else {
$username = $_POST[author];
$title = $_POST[title];
$category = $_POST[category];
$content = $_POST[content];
if (!$username || !$title || !$category || !$content) {
print "You forgot a field! Please go back and fill all the fields!";
} else {
$ndate = date('H:i d-m-Y');
$addingNews = "INSERT INTO SW_News (author, title, category, date, content)
VALUES('$username', '$title', '$category', '$ndate', '$content')";
$processNew = mysql_query($addingNews, $connect);
print "News item added succesfully!";
}
}
}
if ($adminPage == "edit") {
print "<strong>News Admin:</strong>";
print "<br /><br />";
$editId = $_GET[ei];
if (!$editId || !is_numeric($editId)) {
$getNews = "SELECT * FROM SW_News ORDER BY date DESC";
$newsInbound = mysql_query($getNews, $connect);
while ($displayNews = mysql_fetch_object($newsInbound)) {
$content = nl2br($displayNews->content);
print "<div>";
print "<b><a href=\"index.php?v=ad&aa=edit&ei=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />";
print $content;
print "<div align=\"right\">";
print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date;
print "</div>";
print "</div>";
}
} else {
if (!$_POST[editnews]) {
$getTheIdNews = "SELECT * FROM SW_News WHERE id = $editId";
$newsInbound1 = mysql_query($getTheIdNews, $connect);
while ($displayEditNews = mysql_fetch_object($newsInbound1)) {
print "<form method=\"post\">";
print "<strong>Title:</strong> <br />";
print "<input type=\"text\" value=\"".$displayEditNews->title."\" name=\"title\"> <br />";
print "<strong>Author:</strong> <br />";
print "<input type=\"text\" value=\"".$displayEditNews->author."\" name=\"author\"> <br />";
print "<strong>Category:</strong> <br />";
print "<select name=\"category\">
<option title=\"yourcategory\" selected>Yourcategory</option>
<option title=\"yourcategory1\">Yourcategory1</option>
<option title=\"yourcategory2\">Yourcategory2</option>
<option title=\"yourcategory3\">Yourcategory3</option>
<option title=\"yourcategory4\">Yourcategory4</option>
</select><br />";
print "<input type=\"hidden\" name=\"newsid\" value=\"".$displayEditNews->id."\">";
print "<strong>Content:</strong> <br />";
print "<textarea name=\"content\" cols=\"60\" rows=\"20\">".$displayEditNews->content."</textarea> <br />";
print "<input type=\"submit\" name=\"editnews\" value=\"Edit News\">";
print "</form>";
}
} else {
$title = $_POST[title];
$author = $_POST[author];
$category = $_POST[category];
$content = $_POST[content];
$editIdNews = $_POST[newsid];
$updateTheNews = "UPDATE SW_News SET title = '$title', author = '$author', category = '$category', content = '$content' WHERE id = $editIdNews";
$fini****Up = mysql_query($updateTheNews, $connect);
print "The article <strong>".$title."</strong> has succesfully been edited!";
}
}
}
if ($adminPage == "del") {
print "<strong>News Admin:</strong>";
print "<br /><br />";
$delId = $_GET[di];
if (!$delId || !is_numeric($delId)) {
$getNews = "SELECT * FROM SW_News ORDER BY date DESC";
$newsInbound = mysql_query($getNews, $connect);
while ($displayNews = mysql_fetch_object($newsInbound)) {
$content = nl2br($displayNews->content);
if (strlen($content) > 750) {
$content = substr ("$content", 0, 500);
$content .= " ...";
} else {
$content .= "";
}
print "<div>";
print "<b><a href=\"index.php?v=ad&aa=del&di=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />";
print $content;
print "<div align=\"right\">";
print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date;
print "</div>";
print "</div>";
}
} else {
if (!$_POST[delnews]) {
$getTheIdNews = "SELECT * FROM SW_News WHERE id = $delId";
$newsInbound1 = mysql_query($getTheIdNews, $connect);
while ($displayDelTitle = mysql_fetch_object($newsInbound1)) {
print "Are you sure you want to delete the article <strong>".$displayDelTitle->title."</strong>?";
print "<form method=\"post\">";
print "<input type=\"submit\" value=\"Delete!\" name=\"delnews\">";
print "</form>";
}
} else {
$updateTheNews = "DELETE FROM SW_News WHERE id = '$delId'";
$fini****Up = mysql_query($updateTheNews, $connect);
print "The article <strong>".$title."</strong> has succesfully been deleted!";
}
}
}
break;
}
mysql_close($connect);
?> not one of my best tutorials, but have fun with it.
Wildo
Help
















