Class+PHP+CMS

=CSS=

code border-bottom: 3px groove #cccccc; }
 * 1) header {

float: left; width: 250px; }
 * 1) navigation {

margin-left: 250px; padding: 15px 30px; border-left: 3px groove #cccccc; }
 * 1) page {

clear: both; padding: 50px 30px; border-top: 3px groove #cccccc; }
 * 1) footer {

.selected { font-weight: bold; } code

=Functions=

code <?php // this is where all the basic functions will go function confirm_query($result_set){ if(!$result_set){ die("Database query failed: " . mysql_error); } }

function get_all_subjects{ global $connection; $query = "SELECT *   FROM tblsubjects    ORDER BY position ASC"; $subject_set = mysql_query($query,$connection); confirm_query($subject_set); return $subject_set; }

function get_pages_for_subject($subject_id){ global $connection; $query = "SELECT *       FROM tblpages        WHERE subject_id={$subject_id["id"]}        ORDER BY position ASC"; $page_set = mysql_query($query,$connection); confirm_query($page_set); return $page_set; }

function get_subject_by_id($subject_id) { global $connection; $query = "SELECT * "; $query .= "FROM tblsubjects "; $query .= "WHERE id=". $subject_id. " ";   $query .= "LIMIT 1"; $result_set = mysql_query($query,$connection); confirm_query($result_set); // if no rows get returned return a NULL if ($subject = mysql_fetch_array($result_set))        { return $subject; } else { return NULL; }

}

function get_page_by_id($page_id) { global $connection; $query = "SELECT * "; $query .= "FROM tblpages "; $query .= "WHERE id=". $page_id. " ";   $query .= "LIMIT 1"; $result_set = mysql_query($query,$connection); confirm_query($result_set); // if no rows get returned return a NULL if ($page = mysql_fetch_array($result_set))        { return $page; } else { return NULL; }

}

?> code

Need to add these functions to functions.php code function find_selected_page { global $sel_subject; global $sel_page; if (isset($_GET['subj'])) { $sel_subject = get_subject_by_id($_GET['subj']); $sel_page = NULL; } elseif (isset($_GET['page'])) { $sel_subject = NULL; $sel_page = get_page_by_id($_GET['page']); } else { $sel_subject = NULL; $sel_page = NULL; }   }

function navigation($sel_subject, $sel_page) { $output = ""; $subject_set = get_all_subjects; while ($subject = mysql_fetch_array($subject_set)) { $output .= "{$subject["menu_name"]}"; $page_set = get_pages_for_subject($subject["id"]); $output .= ""; while ($page = mysql_fetch_array($page_set)) { $output .= "{$page["menu_name"]}"; }           $output .= ""; }       $output .= ""; return $output; }

// also these for create_subject.php

function mysql_prep($value) { $magic_quotes_active = get_magic_quotes_gpc; $new_enough_php_version = function_exists("mysql_real_escape_string"); // i.e. PHP v4.3.0 or greater

if($new_enough_php_version) { // undo magic quote effects and use mysql_real_escape_string if($magic_quotes_active) { $value = stripslashes($value); } $value = mysql_real_escape_string($value); } else { // before PHP v4.3.0 // if magic quotes are not on, add slashes if(!$magic_quotes_active) { $value = addslashes($value); } // do not need else - if magic quotes active, then have slashes already }       return $value; // this value is clean!!! }

function redirect_to( $location = NULL ) { if ($location != NULL) { header("Location: {$location}"); exit; }   }

code

=Constants=

code  code

=Connection=

code  code

=Footer=

code Copyright 2010, XYZ Org

 code

=Content Page (as of Feb 8)=

code  <?php require_once("includes/functions.php"); ?>

// Note clean up the following and create new function find_selected_page // then replace block with <?php find_selected_page; ?> <?php if(isset($_GET['subj'])) { $subj = $_GET['subj']; $sel_subject = get_subject_by_id($subj); $pg = ""; $sel_page = NULL; } elseif(isset($_GET['pg'])) { $pg = $_GET['pg']; $sel_page = get_page_by_id($pg); $sel_subject = NULL; $subj = ""; } else { $pg = ""; $subj = ""; $sel_subject = NULL; $sel_page = NULL; } ?> <?php require("includes/header.php"); ?>

// clean up the following navigation stuff // create a new function function navigation($sel_subject, $sel_page) // replace navigation stuff with // <?php echo navigation($sel_subject, $sel_page); ?>

<?php

$subject_set = get_all_subjects;

while($subject = mysql_fetch_array($subject_set)){ echo "<a href=\"content.php?subj={$subject["id"]}\">". $subject["menu_name"]. "</a></li>" ;

$page_set = get_pages_for_subject($subject["id"]); echo ""; while($page = mysql_fetch_array($page_set)){ echo "<a href=\"content.php?pg={$page["id"]}\">". $page["menu_name"]. "</a></li>" ; }   echo "</ul>"; } ?> </ul>

// ************************************************** // clean up page area and use following scripting

<?php if (!is_null($sel_subject)) { // subject selected ?> <?php echo $sel_subject['menu_name']; ?> <?php } elseif (!is_null($sel_page)) { // page selected ?> <?php echo $sel_page['menu_name']; ?> <?php echo $sel_page['content']; ?> <?php } else { // nothing selected ?> Select a subject or page to edit <?php } ?>

// **************************************************** <?php if($sel_subject == NULL) { echo "Content Area"; } else { echo $sel_subject['menu_name']; } ?>

<?php if(!$sel_page == NULL) { echo $sel_page['content']; } ?>

<?php require("includes/footer.php"); ?> code

final content.php to use with new_subject
code <?php require("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php    find_selected_page; ?> <?php require("includes/header.php"); ?>

<?php echo navigation($sel_subject, $sel_page); ?>

<a href="new_subject.php"> + Add a new subject</a>

<?php if (!is_null($sel_subject)) { // subject selected ?> <?php echo $sel_subject['menu_name']; ?> <?php } elseif (!is_null($sel_page)) { // page selected ?> <?php echo $sel_page['menu_name']; ?> <?php echo $sel_page['content']; ?> <?php } else { // nothing selected ?> Select a subject or page to edit <?php } ?>

<?php require("includes/footer.php"); ?>

code

=new_subject.php=

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php    find_selected_page; ?> <?php include("includes/header.php"); ?>

<?php echo navigation($sel_subject, $sel_page); ?>

Add Subject

<form action="create_subject.php" method="post"> Subject name: <input type="text" name="menu_name" value="" id="menu_name" /> Position: <?php $subject_set = get_all_subjects; $subject_count = mysql_num_rows($subject_set); // $subject_count + 1 b/c we are adding a subject for($count=1; $count <= $subject_count+1; $count++) { echo "<option value=\"{$count}\">{$count} "; }           ?>    Visible: <input type="radio" name="visible" value="0" /> No       <input type="radio" name="visible" value="1" /> Yes <input type="submit" value="Add Subject" />

<a href="content.php">Cancel</a>

<?php require("includes/footer.php"); ?>

code

=create_subject.php=

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php $errors = array;

// Form Validation $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) { $errors[] = $fieldname; }   }

if (!empty($errors)) { redirect_to("new_subject.php"); }

$fields_with_lengths = array('menu_name' => 30); foreach($fields_with_lengths as $fieldname => $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } }

if (!empty($errors)) { redirect_to("new_subject.php"); } ?> <?php $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); ?> <?php $query = "INSERT INTO tblsubjects (               menu_name, position, visible            ) VALUES (                '{$menu_name}', {$position}, {$visible}            )"; $result = mysql_query($query, $connection); if ($result) { // Success! header("Location: content.php"); exit; } else { // Display error message. echo " Subject creation failed. "; echo " ". mysql_error. " ";   } ?>

<?php mysql_close($connection); ?>

code

=edit_subject.php=

Important!!! for this page to work we need to change the link in the navigation function to edit_subject.php or it won't work

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php 	find_selected_page; ?> <?php include("includes/header.php"); ?>

<?php echo navigation($sel_subject, $sel_page); ?>

Edit Subject: <?php echo $sel_subject['menu_name']; ?>

<form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post"> Subject name: <input type="text" name="menu_name" value="" id="menu_name" /> Position: <?php $subject_set = get_all_subjects; $subject_count = mysql_num_rows($subject_set); // $subject_count + 1 b/c we are adding a subject for($count=1; $count <= $subject_count+1; $count++) { echo "<option value=\"{$count}\">{$count} "; }			?>	 Visible: <input type="radio" name="visible" value="0" /> No		<input type="radio" name="visible" value="1" /> Yes <input type="submit" value="Add Subject" />

<a href="content.php">Cancel</a>

<?php require("includes/footer.php"); ?>

code

.