Web Hosting How-Tos

  Home arrow Web Hosting How-Tos arrow Page 3 - Building a CMS, part 2: Taking it to t...
Web Hosting Articles  
Web Hosting FAQs  
Web Hosting How-Tos  
Web Hosting News  
Web Hosting Reviews  
Web Hosting Security  
Weekly Newsletter 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Contact Us 
Site Map 
Privacy Policy 
  >>> SIGN UP!  
  Lost Password? 

Building a CMS, part 2: Taking it to the server
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 1

    Table of Contents:
  • Building a CMS, part 2: Taking it to the server
  • Generating the XML
  • Creating the Markup
  • A Word About Namespaces

  • Rate this Article: Poor Best 
      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




    Building a CMS, part 2: Taking it to the server - Creating the Markup

    (Page 3 of 5 )

    We will now need to loop through all the form field values in the $_POST array. This array contains all the values from the form in a http post request. The POST array is an associative array. Each element of the array has a key and a value. There are no numerical indexes in $_POST, so we will be using the array_keys function to get a numerically indexed array of the keys in $_POST to loop through it. We could use a for each loop rather than a for loop to loop through $_POST without using array_keys(); you can use either method as long as the keys in the array can be used as below.

    $field_names = array_keys($_POST);
    for($i = 0;$i < count($_POST);$i++)
          case "headline":
             $xml_arr["headline"] = "<mainheading>".$_POST[$field_names[$i]].
          case "day":
             $xml_arr["day"] = "<day>".$_POST[$field_names[$i]]."</day>\n";
          case "month":
             $xml_arr["month"] = "<month>".$_POST[$field_names[$i]].
          case "year":
             $xml_arr["year"] = "<year>".$_POST[$field_names[$i]].
          case "time":
             $xml_arr["time"] = "<time>".$_POST[$field_names[$i]].
          case "mainsubhead":
             $xml_arr["mainsubhead"] = "<mainsubhead>".$_POST[$field_names[$i]].
          case "maincontents":
             $xml_arr["maincontents"] = "<maincontents>".$_POST[$field_names[$i]]."
          case "desc":
             $xml_arr["desc"] = "<company>".$_POST[$field_names[$i]]."</company>\n";
          case "contactname":
             $xml_arr["contactname"] = "<contactname>".$_POST[$field_names[$i]].
          case "contactmail":
             $xml_arr["contactmail"] = "<contactmail .path=\"".$_POST["address"]."\">"
          case "contacturl":
             $xml_arr["contacturl"] = "<contacturl .path=\"".$_POST["url"]."\">"

    The markup that we had previously done in Javascript is wrapped in a "company" tag and stored with all the other markup we have added. Line breaks are added in the appropriate places to ensure that our final source code is not one long line. The line breaks make the raw XML more readable, but will be considered nodes in the document. There is a way to deal with whitespace like this in XSLT, but if you would rather not have them there, they are optional.

    Once we have all our data marked up the way it should be, it's time to put it together in a string with any other markup that will be needed. This includes putting in the root XML element which is, in our case, "pressrelease."

    $xml_str = $xml_arr["doctop"]."<pressrelease>\n".$xml_arr["headline"]."<date>\n".
    $xml_arr["contactmail"].$xml_arr["contacturl"]."</contactinfo> \n".

    The variable $xml_str now contains a string that represents our XML document. The next step is to save the document. You can use whatever convention you want for naming the file. In this example I have used the value from the main sub heading, trimmed and with spaces replaced by underscores.

    $f = str_replace(" ","_",trim($_POST["mainsubhead"])).".xml";

    Next create a new file with the fopen function.

    $xml_file = fopen($f,w) or die("<h1>Trouble writing $f</h1>");

    The server may need some time before you can send the new XML file to the browser, so let's check to see if it exists before redirecting the browser to the file.

    $set_time_limit(20);//just in case something goes wrong
    do {
       //Nothing at all until the file is written;
    } while(!file_exists($f))

    This will display the finished XML file in the browser with any CSS styling that you specified in the above mentioned CSS file. Once we know that the XML file is structured the way it should be, it's time to do an XSL style sheet.


    XSLT (Extensible Stylesheet Language Transformation) uses templates to reformat XML, or to do transformations from XML to other text based formats such as HTML, RTF (Rich Text Format) and PDF (Portable Document Format). XML formats are making their way into office and print publishing software as well. You can put whatever HTML or other text you need in your document inside of the template. Data from each XML tag in a source document is inserted into the result document by using the XSL value-of tag, as well as a host of other tags, and utilizing XPath, which is a standard for addressing information in an XML document.

    An XSL document is an XML document, and therefore must conform to the syntax rules of the XML standard. This inculdes using what is called a namespace to identify tags as belonging to XSL.

    More Web Hosting How-Tos Articles
    More By Chris Root


    - Phishing Scams: An Overview and How to Detec...
    - Tips for Safe Downloading Online
    - How To Avoid Spam
    - How to Get Into Ethical Hacking
    - How to Prevent Drive-by Downloads
    - Facebook Timeline Tips and Tricks
    - How to Keep Up with Facebook`s Changes
    - Wi-Fi Network Security Tips
    - Tips for Safe Online Holiday Shopping
    - Facebook Privacy: Keeping Up with the Const...
    - Tips for Facebook Privacy
    - How to Cover Your Tracks on the Web
    - SSH Keys for FileZilla and Putty in Cpanel
    - How to Create a Filezilla FTP User
    - How to Install FileZilla Server

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap