Home arrow PHP arrow Page 4 - Building a Relational Content Management System in PHP/MySQL

Managing Articles - PHP

You may be familiar with relational databases, but what is a relational content management system? Read on to learn how to build this system, which helps you create a search engine friendly site fairly quickly.

TABLE OF CONTENTS:
  1. Building a Relational Content Management System in PHP/MySQL
  2. The Rewrite
  3. The Common Functions
  4. Managing Articles
  5. Displaying the Articles
By: Roger Stringer
Rating: starstarstarstarstar / 39
December 20, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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>&nbsp;</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.



 
 
>>> More PHP Articles          >>> More By Roger Stringer
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: