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++)
{
switch($field_names[$i])
{
case "headline":
$xml_arr["headline"] = "<mainheading>".$_POST[$field_names[$i]].
"</mainheading>\n";
break;
case "day":
$xml_arr["day"] = "<day>".$_POST[$field_names[$i]]."</day>\n";
break;
case "month":
$xml_arr["month"] = "<month>".$_POST[$field_names[$i]].
"</month>\n";
break;
case "year":
$xml_arr["year"] = "<year>".$_POST[$field_names[$i]].
"</year>\n";
break;
case "time":
$xml_arr["time"] = "<time>".$_POST[$field_names[$i]].
"</time>\n";
break;
case "mainsubhead":
$xml_arr["mainsubhead"] = "<mainsubhead>".$_POST[$field_names[$i]].
"</mainsubhead>\n";
break;
case "maincontents":
$xml_arr["maincontents"] = "<maincontents>".$_POST[$field_names[$i]]."
</maincontents>\n";
break;
case "desc":
$xml_arr["desc"] = "<company>".$_POST[$field_names[$i]]."</company>\n";
break;
case "contactname":
$xml_arr["contactname"] = "<contactname>".$_POST[$field_names[$i]].
"</contactname>\n";
break;
case "contactmail":
$xml_arr["contactmail"] = "<contactmail .path=\"".$_POST["address"]."\">"
.$_POST[$field_names[$i]]."</contactmail>\n";
break;
case "contacturl":
$xml_arr["contacturl"] = "<contacturl .path=\"".$_POST["url"]."\">"
.$_POST[$field_names[$i]]."</contacturl>\n";
break;
}
}
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["day"].$xml_arr["month"].$xml_arr["year"].$xml_arr["time"].
"</date>\n<content>\n".$xml_arr["mainsubhead"].
$xml_arr["maincontents"].$xml_arr
["maincontent"].$xml_arr
["desc"]."</content>\n<contactinfo>\n".$xml_arr
["contactname"].
$xml_arr["contactmail"].$xml_arr["contacturl"]."</contactinfo> \n".
"</pressrelease>";
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))
fclose($xml_file);
header("location:".dirname($_SERVER['PHP_SELF'])."/".$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
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.
Next: A Word About Namespaces >>
More Web Hosting How-Tos Articles
More By Chris Root