PHP
  Home arrow PHP arrow Page 4 - Building a Relational Content Management System in PHP/MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building a Relational Content Management System in PHP/MySQL
By: Roger Stringer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 31
    2005-12-20


    Table of Contents:
  • Building a Relational Content Management System in PHP/MySQL
  • The Rewrite
  • The Common Functions
  • Managing Articles
  • Displaying the Articles

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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>&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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT