zzz+special+page+CRUD

Page CRUD

CSS File

code add to css

margin-top: 13px; }
 * 1) pages_for_subject {

code

constants.php (in includes folder)

code  code

connection.php (in includes folder)

code  code

form_functions.php (in includes folder)

code  $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname; } }   return $field_errors; }

function display_errors($error_array) { echo ""; echo "Please review the following fields: "; foreach($error_array as $error) { echo " - ". $error. " ";   }    echo " "; }

?> code

content.php

code    



 + Add a new subject

    

">Edit page</a> <?php } else { // nothing selected ?> Select a subject or page to edit <?php } ?>

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

code

edit_subject.php

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

if(intval($_GET['subj']) == 0) { // if subj is not a valid integer go back to content.php redirect_to("content.php"); }

if(isset($_POST['submit'])) {

$errors = array;

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

$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)) { // perform update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']);

$query = "UPDATE tblsubjects SET                   menu_name = '{$menu_name}',                    position = {$position},                    visible = {$visible}                WHERE id = {$id}";

$result = mysql_query($query,$connection);

if(mysql_affected_rows == 1) { // Success $message = "The subject was updated successfully."; } else { // Falled $message = "The subject update failed."; $message .= " ". mysql_error; }

} else { // form validation errors occured $message = "There were ". count($errors). " errors in the form."; }

} // end of if(isset($_POST['submit')) ?> <?php    find_selected_page; // note if this is above the update stuff, the value in the form field will not update ?> <?php include("includes/header.php"); ?>

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

 + Add a new subject</a>

Edit Subject: <?php echo $sel_subject['menu_name']; ?> <?php if(!empty($message)) { echo "<p class=\"message\">". $message. " "; } ?> <?php // list of errors if(!empty($errors)) { echo "<p class=\"errors\">"; echo "Please check the following fields: "; foreach($errors as $error) { echo " - ". $error. " ";   }    echo " "; } ?>

<form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post"> Subject name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" 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}\""; if($sel_subject['position'] == $count) { echo " selected"; } // do not forget the space before selected echo ">{$count} "; }           ?>    Visible: <input type="radio" name="visible" value="0"<?php if($sel_subject['visible'] == 0) { echo " checked"; } ?> /> No       <input type="radio" name="visible" value="1"<?php if($sel_subject['visible'] == 1) { echo " checked"; } ?> /> Yes <input type="submit" name="submit" value="Edit Subject" />

" onclick="return confirm('Are you sure?');">Delete this Subject</a>

Cancel</a>

<div id="pages_for_subjects"> Pages in this subject: <ul> <?php $subject_pages = get_pages_for_subject($sel_subject['id']); while($page = mysql_fetch_array($subject_pages)) { echo "<li>       {$page['menu_name']}</a></li>"; } ?>               </ul>

+ ">Add a new page to this subject</a>

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

page_form.php (redundant page form markup)

code <?php // this page is included by new_page.php and edit_page.php ?> <?php if (!isset($new_page)) {$new_page = false;} ?>

Page name: <input type="text" name="menu_name" value="<?php echo $sel_page['menu_name']; ?>" id="menu_name" />

Position: <?php if (!$new_page) { $page_set = get_pages_for_subject($sel_page['subject_id']); $page_count = mysql_num_rows($page_set); } else { $page_set = get_pages_for_subject($sel_subject['id']); $page_count = mysql_num_rows($page_set) + 1; }       for ($count=1; $count <= $page_count; $count++) { echo "<option value=\"{$count}\""; if ($sel_page['position'] == $count) { echo " selected"; } echo ">{$count} "; }   ?> Visible: <input type="radio" name="visible" value="0"<?php if ($sel_page['visible'] == 0) { echo " checked"; } ?> /> No   <input type="radio" name="visible" value="1"<?php if ($sel_page['visible'] == 1) { echo " checked"; } ?> /> Yes Content:

<textarea name="content" rows="20" cols="80"><?php echo $sel_page['content']; ?> code

new_page.php

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php // make sure the subject id sent is an integer if (intval($_GET['subj']) == 0) { redirect_to('content.php'); }

include_once("includes/form_functions.php");

// START FORM PROCESSING // only execute the form processing if the form has been submitted if (isset($_POST['submit'])) { // initialize an array to hold our errors $errors = array;

// perform validations on the form data $required_fields = array('menu_name', 'position', 'visible', 'content'); $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

$fields_with_lengths = array('menu_name' => 30); $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

// clean up the form data before putting it in the database $subject_id = mysql_prep($_GET['subj']); $menu_name = trim(mysql_prep($_POST['menu_name'])); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $content = mysql_prep($_POST['content']);

// Database submission only proceeds if there were NO errors. if (empty($errors)) { $query = "INSERT INTO tblpages (                       menu_name, position, visible, content, subject_id                    ) VALUES (                        '{$menu_name}', {$position}, {$visible}, '{$content}', {$subject_id}                    )"; if ($result = mysql_query($query, $connection)) { // as is, $message will still be discarded on the redirect $message = "The page was successfully created."; // get the last id inserted over the current db connection $new_page_id = mysql_insert_id; redirect_to("content.php?page={$new_page_id}"); } else { $message = "The page could not be created."; $message .= " ". mysql_error; }       } else { if (count($errors) == 1) { $message = "There was 1 error in the form."; } else { $message = "There were ". count($errors). " errors in the form."; }       }        // END FORM PROCESSING } ?> <?php find_selected_page; ?> <?php include("includes/header.php"); ?>

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

+ Add a new subject</a> Adding New Page <?php if (!empty($message)) {echo "<p class=\"message\">". $message. " ";} ?>   <?php if (!empty($errors)) { display_errors($errors); } ?>

<form action="new_page.php?subj=<?php echo $sel_subject['id']; ?>" method="post"> <?php $new_page = true; ?> <?php include "page_form.php" ?> <input type="submit" name="submit" value="Create Page" />

">Cancel</a>

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

edit_page.php

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php // make sure the subject id sent is an integer if (intval($_GET['page']) == 0) { redirect_to('content.php'); }

include_once("includes/form_functions.php");

// START FORM PROCESSING // only execute the form processing if the form has been submitted if (isset($_POST['submit'])) { // initialize an array to hold our errors $errors = array;

// perform validations on the form data $required_fields = array('menu_name', 'position', 'visible', 'content'); $errors = array_merge($errors, check_required_fields($required_fields));

$fields_with_lengths = array('menu_name' => 30); $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));

// clean up the form data before putting it in the database $id = mysql_prep($_GET['page']); $menu_name = trim(mysql_prep($_POST['menu_name'])); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $content = mysql_prep($_POST['content']);

// Database submission only proceeds if there were NO errors. if (empty($errors)) { $query =    "UPDATE tblpages SET                            menu_name = '{$menu_name}',                            position = {$position},                            visible = {$visible},                            content = '{$content}'                        WHERE id = {$id}"; $result = mysql_query($query); // test to see if the update occurred if (mysql_affected_rows == 1) { // Success! $message = "The page was successfully updated."; } else { $message = "The page could not be updated."; $message .= " ". mysql_error; }       } else { if (count($errors) == 1) { $message = "There was 1 error in the form."; } else { $message = "There were ". count($errors). " errors in the form."; }       }        // END FORM PROCESSING } ?> <?php find_selected_page; ?> <?php include("includes/header.php"); ?>

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

+ Add a new subject</a> Edit page: <?php echo $sel_page['menu_name']; ?> <?php if (!empty($message)) {echo "<p class=\"message\">". $message. " ";} ?>   <?php if (!empty($errors)) { display_errors($errors); } ?>

<form action="edit_page.php?page=<?php echo $sel_page['id']; ?>" method="post"> <?php include "page_form.php" ?> <input type="submit" name="submit" value="Update Page" /> " onclick="return confirm('Are you sure you want to delete this page?');">Delete page</a>

">Cancel</a>

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

delete_page.php

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php // make sure the subject id sent is an integer if (intval($_GET['page']) == 0) { redirect_to('content.php'); }

$id = mysql_prep($_GET['page']); // make sure the page exists (not strictly necessary) // it gives some extra security and allows use of   // the page's subject_id for the redirect if ($page = get_page_by_id($id)) { // LIMIT 1 isn't necessary but is a good fail safe $query = "DELETE FROM tblpages WHERE id = {$page['id']} LIMIT 1"; $result = mysql_query ($query); if (mysql_affected_rows == 1) { // Successfully deleted redirect_to("edit_subject.php?subj={$page['subject_id']}"); } else { // Deletion failed echo " Page deletion failed. "; echo " ". mysql_error. " ";           echo "Return to Main Site</a>"; }   } else { // page didn't exist, deletion was not attempted redirect_to('content.php'); } ?> <?php // because this file didn't include footer.php we need to add this manually mysql_close($db); ?> code

staff.php

code <?php include("includes/header.php"); ?>

Staff Menu Welcome to the staff area. <ul> <li>Manage Website Content</a></li> <li>Add Staff User</a></li> <li>Logout</a></li> </ul>

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

code

.