HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [360]
‘,
1
);
INSERT INTO cmsBlock VALUES (
null,
4,
‘Book II - Styling with CSS’,
‘
- Coloring Your World
- Styling Text
- Selectors, Class, and Style
- Borders and Backgrounds
- Levels of CSS
‘,
1
);
INSERT INTO cmsBlock VALUES (
null,
5,
null,
‘see aharrisbooks.net for more information’,
1
);
This structure has three tables and a view:
♦ The cmsPage table: Represents the data about a page, which currently isn’t much. A fuller version might put menu information in the page data so that the page would “know” where it lives in a menu structure.
♦ The cmsBlock table: Represents a block of information. Each block is the element that would be in a miniature HTML page in the other systems described in this chapter. This table is the key table in this structure because most of the content in the CMS is stored in this table.
♦ The blockType table: Lists the block types. This simple table describes the various block types.
♦ The pageView view: Ties together all the other information. After all the data is loaded, the pageView view ties it all together, as shown in Figure 5-3.
Figure 5-3: This view describes all the data needed to build a page.
Most of the data is being read as HTML, but it’s still text data. I included the entire SQL file, including the INSERT statements, on the CD-ROM as buildCMS.sql.
Writing a PHP page to read from the table
The advantage of using a data-based approach is scalability. In using all the other models in this chapter, I had to keep copying the template page. If you decide to make a change in the template, you have to change hundreds of pages. If you use data, you can write one PHP program that can produce any page in the system. All this page needs is a page-number parameter. Using that information, it can query the system, extract all the information needed for the current page, and then display the page. Here’s the (simplified) PHP code for such a system:
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
type = ”text/css”
href = ”csStd.css” />
//get pageID from request if possible
$pageID = $_REQUEST[”pageID”];
$pageID = mysql_real_escape_string($pageID, $conn);
if ($pageID == ””){
$pageID = 1;
} // end if
//read current page information from the db
$conn = mysql_connect(”localhost”, ”xfd”, ”xfdaio”);
mysql_select_db(”xfd”);
$sql = ”SELECT * FROM pageView WHERE pageID = 1”;
$result = mysql_query($sql, $conn);
//populate local variables from db result
while ($row = mysql_fetch_assoc($result)){
if ($row[”block”] == ”head”){
$head = $row[”title”];
} else if ($row[”block”] == ”menu”){
$menu = $row[”content”];
} else if ($row[”block”] == ”content1”){
$c1Title = $row[”title”];
$c1Text = $row[”content”];
} else if ($row[”block”] == ”content2”){
$c2Title = $row[”title”];
$c2Text = $row[”content”];
} else if ($row[”block”] == ”footer”){
$footer = $row[”content”];
} // end if
} // end while
?>