JavaScript
  Home arrow JavaScript arrow Page 4 - Handling Contents with MySQL for a Content...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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? 
JAVASCRIPT

Handling Contents with MySQL for a Content Management System Built with Prototype
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-04-30

    Table of Contents:
  • Handling Contents with MySQL for a Content Management System Built with Prototype
  • The full client-side code of the CMS application
  • Inserting, updating and deleting articles
  • Assembling the modules of the application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Handling Contents with MySQL for a Content Management System Built with Prototype - Assembling the modules of the application


    (Page 4 of 4 )

    As I expressed in the previous section, here is the complete source code corresponding to this Prototype-based content management system:

    (definition of ajax_cms.htm file)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
     
    <title>Prototype-based CMS</title>
     
    <style type="text/css">
       
    body{
          
    padding: 0;
           
    margin: 0;
          
    background: #fff;
       
    }

        h1{
          
    width: 550px;
          
    padding: 10px;
          
    background: #ffc;
           
    margin-left: auto;
          
    margin-right: auto;
          
    margin-bottom: 10px;
          
    font: bold 22px Arial, Helvetica, sans-serif;
          
    color: #000;
          
    text-align: center;
          
    border: 1px solid #999;
       
    }

        h2{
          
    font: bold 14px Arial, Helvetica, sans-serif;
          
    color: #900;
       
    }

        #contents{
          
    width: 550px;
          
    height: 400px;
          
    background: #ffc;
           
    padding: 10px;
          
    margin-left: auto;
           
    margin-right: auto;
          
    margin-bottom: 10px;
          
    border: 1px solid #999;
          
    overflow: auto;
       
    }

        #article{
          
    background: #eee;
          
    padding: 10px;
          
    margin-bottom: 10px;
          
    border: 1px solid #999;
       
    }

        #contents p{
          
    font: normal 11px Verdana, Arial, Helvetica, sans-serif;
          
    color: #000;
       
    }

        #contents a:link,#contents a:visited{
          
    font: bold 11px Verdana, Arial, Helvetica, sans-serif;
          
    color: #00f;
          
    text-decoration: underline;
       
    }

        #contents a:hover{
          
    color: #c30;
       
    }

        #formcontainer{
          
    width: 550px;
          
    height: 300px;
          
    background: #ffc;
          
    padding: 10px;
          
    margin-left: auto;
          
    margin-right: auto;
          
    border: 1px solid #999;
        
    }

        #formcontainer p{
          
    text-align: right;
          
    margin-right: 100px;
          
    font: bold 11px Verdana, Arial, Helvetica, sans-serif;
          
    color: #000;
       
    }

        .inputbox{
          
    width: 300px;
          
    padding: 2px;
          
    background: #eee;
          
    font: normal 11px Verdana, Arial, Helvetica, sans-serif;
          
    color: #000;
          
    border: 1px solid #999;
       
    }

        .submitbox{
          
    font: normal 11px Verdana, Arial, Helvetica, sans-serif;
          
    color: #000;
          
    padding: 2px;
       
    }

        textarea{
          
    width: 300px;
          
    height: 150px;
          
    padding: 2px;
          
    background: #eee;
          
    font: normal 11px Verdana, Arial, Helvetica, sans-serif;
          
    color: #000;
          
    border: 1px solid #999;
       
    }
     
    </style>
     
    <script language="javascript" src="prototype-
    1.4.0.js"></script>
     
    <script language="javascript">

        // add new article to database table
       
    function uploadArticle(e){
          
    var params='command='+$F('command')+'&id='+$F('id')
    +'&title='+$F('title')+'&author='+$F('author')
    +'&content='+escape($F('content'));
          
    var xmlobj=new Ajax.Updater
    ('contents','handle_articles.php',{method: 'get',parameters:
    params, evalScripts: true});

           // prevent form from submitting
          
    Event.stop(e);

           // reset header message
          
    Element.update('header','PROTOTYPE-BASED CMS');
          
    $('command').value='upload';
       
    }

        // update article into database table
       
    function editArticle(id,title,author,content){
          
    Element.update('header','EDITING ARTICLE...');
          
    $('title').value=title;
          
    $('author').value=author;
          
    $('content').value=content;
          
    $('id').value=id;
          
    $('command').value='update';
       
    }

        // delete article from database table
       
    function deleteArticle(id){
          
    var params='command=delete&id='+id;
          
    var xmlobj=new Ajax.Updater
    ('contents','handle_articles.php',{method: 'get',parameters:
    params, evalScripts: true});
       
    }

        // attach handler to window object
       
    Event.observe(window,'load',initializeCMS,false);

        // initialize CMS application
       
    function initializeCMS(){

           // attach handler to article form
          
    Event.observe('articleform','submit',uploadArticle);

           // display list of articles when web page is loaded
          
    var xmlobj=new Ajax.Updater
    ('contents','handle_articles.php',{method: 'get'});
       
    }
     
    </script>
    </head>
    <body>
     
    <h1 id="header">PROTOTYPE-BASED CMS</h1>
     
    <div id="contents"></div>
     
    <div id="formcontainer">
       
    <form id="articleform">
         
    <p>Article Title <input type="text" id="title"
    class="inputbox" title="Enter article's title" /></p>
         
    <p>Article Author <input type="text" id="author"
    class="inputbox" title="Enter article's author" /></p>
         
    <p>Enter contents of article below</p><p><textarea
    id="content" title="Enter the contents of the article"></textarea></p>
         
    <p><input type="submit" value="Upload Article"
    class="submitbox" title="Upload article" /></p>
         
    <input type="hidden" id="id" />
         
    <input type="hidden" id="command" value="upload" />
       
    </form>
     
    </div>
    </body>
    </html>

    (definition of handle_articles.php file)

    <?php

      // define 'MySQL' class
     
    class MySQL{
        
    private $conId;
        
    private $host;
        
    private $user;
        
    private $password;
        
    private $database;
        
    private $result;
        
    const OPTIONS=4;
        
    public function __construct($options=array()){
          
    if(count($options)!=self::OPTIONS){
            
    throw new Exception('Invalid number of connection
    parameters');
          
    }
          
    foreach($options as $parameter=>$value){
            
    if(!$value){
              
    throw new Exception('Invalid parameter '.$parameter);
            
    }
            
    $this->{$parameter}=$value;
          
    }
          
    $this->connectDB();
         
    }

         // connect to MySQL
        
    private function connectDB(){
          
    if(!$this->conId=mysql_connect($this->host,$this-
    >user,$this->password)){
            
    throw new Exception('Error connecting to the server');
          
    }
           
    if(!mysql_select_db($this->database,$this->conId)){
            
    throw new Exception('Error selecting database');
          
    }
         
    }

         // run query
        
    public function query($query){
          
    if(!$this->result=mysql_query($query,$this->conId)){
            
    throw new Exception('Error performing query '.$query);
          
    }
          
    return new Result($this,$this->result);
        
    }

         // escape string
        
    public function escapeString($string){
          
    return mysql_escape_string($string);
        
    }
      }

      // define 'Result' class
     
    class Result {
        
    private $mysql;
        
    private $result;
         
    public function __construct($mysql,$result){
          
    $this->mysql=$mysql;
          
    $this->result=$result;
        
    }

         // fetch row
        
    public function fetchRow(){
          
    return mysql_fetch_assoc($this->result);
        
    }

         // count rows
        
    public function countRows(){
          
    if(!$rows=mysql_num_rows($this->result)){
            
    throw new Exception('No rows were found to display!');
          
    }
          
    return $rows;
        
    }

         // count affected rows
        
    public function countAffectedRows(){
          
    if(!$rows=mysql_affected_rows($this->mysql->conId)){
            
    throw new Exception('Error counting affected rows');
          
    }
          
    return $rows;
        
    }

         // get ID form last-inserted row
        
    public function getInsertID(){
          
    if(!$id=mysql_insert_id($this->mysql->conId)){
            
    throw new Exception('Error getting ID');
          
    }
          
    return $id;
        
    }

         // seek row
        
    public function seekRow($row=0){
          
    if(!is_int($row)||$row<0)
            
    throw new Exception('Invalid result set offset');
          
    }
          
    if(!mysql_data_seek($this->result,$row)){
            
    throw new Exception('Error seeking data');
          
    }
        
    }
     
    }
     
    try{

         // prevent browser caching
        
    header('Cache-Control: no-cache');
        
    header('Pragma: no-cache');

         // connect to MySQL
        
    $db=new MySQL(array
    ('host'=>'host','user'=>'user','password'=>'password',
    'database'=>'database'));
        
    $command=$db->escapeString($_GET['command']);

         // add new article to database table
        
    if(!$command||$command=='upload'){
          
    if($_GET['title']&&$_GET ['author']&&$_ GET['content']){
            
    $title=$db->escapeString($_GET['title']);
            
    $author=$db->escapeString($_GET['author']);
            
    $content=$db->escapeString($_GET['content']);
            
    $db->query("INSERT INTO articles
    (id,title,author,content) VALUES
    (NULL,'$title','$author','$content')");
          
    }
         
    }

         // update article into database table
        
    elseif($command=='update'){
          
    $id=$db->escapeString($_GET['id']);
          
    $title=$db->escapeString($_GET['title']);
          
    $author=$db->escapeString($_GET['author']);
          
    $content=$db->escapeString($_GET['content']); 
           
    $db->query("UPDATE articles SET
    title='$title',author='$author',content='$content' WHERE
    id='$id'");
         
    }

          // delete article from database table
         
    elseif($command=='delete'){
           
    $id=$db->escapeString($_GET['id']);
           
    $db->query("DELETE FROM articles WHERE id='$id'");
         
    }
         
    $result=$db->query("SELECT * FROM articles ORDER BY id");

          // display list of articles
         
    while($row=$result->fetchRow()){
           
    echo '<div id="article"><h2>'.$row
    ['title'].'</h2><p>Author: '.$row['author'].'</p><p>'.$row
    ['content'].'</p><a href="#" onclick="editArticle(''.$row
    ['id'].'',''.$row['title'].'',''.$row['author'].'',''.$row
    ['content'].'')";>Edit</a> <a href="#" onclick="deleteArticle
    ('.$row['id'].')";>Delete</a><p></p></div>';
          
    }
     
    }
     
    catch(Exception $e){
       
    echo $e->getMessage();
       
    exit();
     
    }
    ?>

    As usual with many of my articles on web development, feel free to modify all the source files shown here, so you can adapt the functionality of the content management system to your personal requirements.

    Final thoughts

    Finally, this series has ended. In these three tutorials I showed you how to build a basic content management system that uses the Prototype JavaScript framework to insert, update and delete articles from a MySQL database table.

    Naturally, you can use the basic structure of the system to handle different content to suit your personal requirements.

    See you in the next web development tutorial!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over this last article of the series, the set of PHP classes, aimed at interacting...
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT