Building a Relational Content Management System in PHP/MySQL - Managing Articles (
Page 4 of 5 )
Now we need to build the management page so we can create new articles.
Let's create "login.php":
<?php
session_start();
$auid = isset($_POST['auid']) ? $_POST['auid'] : $_SESSION
['auid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION
['pwd'];
if(!isset($auid)) {
loginform();
exit;
}
if( $auid != $aduser || $pwd != $adpass ){
unset($_SESSION['auid']);
unset($_SESSION['pwd']);
loginform();
exit;
}else if( $auid == $aduser && $pwd == $adpass ){
$_SESSION['auid'] = $auid;
$_SESSION['pwd'] = $pwd;
}
?>
<?
function loginform(){
global $sitename;
?>
<CENTER>
<BR>
<FONT FACE=verdana color=#FFFFFF size=5><B><?=$sitename?
> Login</B></FONT>
<BR>
<TABLE cellspacing=0>
<FORM method=post>
<TR><TD>Username:
<TD><INPUT type=text name=auid size=16>
<TR><TD>Pass:
<TD><INPUT type=pass name=pwd size=16>
<TR><TD colspan=2><INPUT type=submit value="Log In
>>"></TD>
</FORM>
</TABLE>
<BR>
<?
}
?>
This uses the $aduser and $adpass variables that were set in the config.php file to let you log in to your management panel.
Next, we create a file called "admin.php." This file is the main article management page.
<?
include("common.php");
include("login.php");
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
switch($mode){
case "new":
case "edit":
include("cmsform.php");
break;
case "save":
if($_REQUEST['id']){
UpdatePage($_REQUEST['id']);
}else{
AddPage();
}
header("Location: admin.php");
break;
case "delete":
if($_REQUEST['id']){
DeletePage($_REQUEST['id']);
}
header("Location: admin.php");
break;
default:
include("cmslist.php");
break;
}
?>
Now, let's create "cmslist.php", where we display the articles:
<table width=100% border=1>
<?
echo listPages(0);
function listPages($parent=0){
$out = "";
$pages = getPages('parent',$parent);
$i = 1;
foreach($pages as $id=>$page){
$out .= "<tr>";
$out .= "<td>{$i}</td>";
$out .= "<td><a href='admin.php?mode=edit&id={$id}'>{$page
['title']}</a></td>";
if($page['id'] != 1){
$out .= "<td><a href='admin.php?mode=delete&id=
{$id}'></a></td>";
}
$out .= "</tr>";
$out .= "<tr>";
$out .= "<td> </td>";
$out .= "<td colspan=2><table width=100%>".listPages
($id)."</table></td>";
$out .= "</tr>";
$i++;
}
return $out;
}
?>
<tr>
<td colspan=2 align=center>
<a href='admin.php?mode=new'>Add New Article</a>
</td>
</tr>
</table>
We now have a page that lets us both list and manage articles.
Our next file is "cmsform.php", which is the add/edit form.
<table>
<form method="POST" action="account.php?mode=save<?=( isset
($_REQUEST['id']) ? "&id={$_REQUEST['id']}" : null )?>">
<?
$pageInfo = "";
if( isset($_REQUEST['id']) ){
$pageInfo = getPage('id',$_REQUEST['id']);
}
?>
<tr>
<td>Title:</td>
<td><input type='text' name='title' value='<?=$pageInfo
['title']?>'></td>
</tr>
<tr>
<td>Summary:</td>
<td><input type='text' name='summary' value='<?=$pageInfo
['summary']?>'></td>
</tr>
<tr>
<td>Body:</td>
<td><textarea name='body' rows=20 cols=80><?=$pageInfo['body']?
></textarea>
</tr>
<tr>
<td>Parent</td>
<td>
<select name='parent'>
<option value='0'>---
<?=buildDropDown(0,1,$_REQUEST['id']);?>
</select>
</td>
</tr>
<tr>
<td colspan=2><input type='submit' value='Save'></td>
</tr>
</form>
</table>
<?
function buildDropDown($parent,$level = 1,$articleId){
$sep = '';
if( $level > 0 ){
for ($i = 0; $i < $level; $i++) {
$sep = $sep.'...';
}
$sep = $sep.' ';
}
$kids = getPages('parent',$parent);
foreach($kids as $k=>$kid){
if( $kid['id'] == $articleId ) continue;
$sel = "";
if( isset($qr1[0]['parent']) && $kid['id'] == $qr1[0]
['parent'] ){$sel = "SELECTED";}
echo "<option value='".$kid['id']."' $sel>{$sep}{$kid
['title']}</option>";
echo buildDropDown($kid['id'],($level+1),$articleId);
}
}
?>
You may notice that I make use of a form creation trick here. I use it a lot. You simply create an array in the common.php file that contains the form fields and what type of fields they are, then display it here. It works nicely, and keeps the forms pretty straightforward and not as messy as they can get.
| | Discuss Building a Relational Content Management System in PHP/MySQL | | | | | | | This is a test comment. | | | | | | Hi, first of all thank you for this tutorial. I've just tried your codes but i think... | | | | | | Change:
function buildDropDown($parent,level = 1,$articleId){
To:
function... | | | | | | One more error:
$seoname = isset($_REQUEST['$seoname']) ? $_REQUEST['$seoname'] :... | | | | | | I forgot to mention, it's in index.php | | | | | | When i tried to enter an article, the following error occurred:
Fatal error: Call... | | | | | | Remove this line:
$_POST['akey'] = makeKey(10);
akey no longer gets used in... | | | | | | After i had made the last change, it worked.
Thank you. | | | | | | Thanks for Tutorial. I'm getting an error message saying
Fatal error: Call to... | | | | | | Hi there!
It was a nice tutorial and my hopes were high, but I was disappointed. I... | | | | | | It is possible that sessions in PHP in your case not work. | | | | | | i had the same "server encountered an internal error..."
the problem was a... | | | | | | Originally, I didn't have a line break in the .htaccess for this article.
Must have... | | | | | | Ive read all the above comments and made the necessary changes, but I still have... | | | | | | The header function is touchy. It must be called before anything is sent to the... | | | | | | Hi. I enjoyed your project. Everything works except getting the article itself to... | | | | | | Solution: Turn on (uncomment) mod_rewrite in the httpd.conf file.
Works great. | | | | | | Thank you very much for this tutorial. It works great for me with one exception: ... | | | | | | That's a nice tutorial. thank you. but what about deleting an article | | | | | | Anyone Knows how to use FCKeditor? I tried to do so but I can't. I am new to... | | | | | | I got your tutorial to work after reading all the comments and making that one last... | | | | | | I have just gone thru the tutorial and comments and have some aspects working but,... | | | | | | I am using your Relational CMS Tutorial as the basis of a content management system... | | | | | | I have a different database abstraction layer getting my MySQL commands to be... | | | | | | Hello everybody... someone have the code with the changes?
.. am still have the... | | | | | | Getting a "404" error. Not sure what the problem is, I'm using ubuntu with Apache2;... | | | | | | >>> Post your comment now! | | | | | |
|
 |