Jump to content

Please I need help with my project. Don't ignore me this please, if you cab help me.


chris17

Recommended Posts

<?php 
//File Name: form_functions.php
//Form Validation
//This is a raw alternative
/*if(!isset($_POST['menu_name']) || empty($_POST['menu_name'])){
	$error[] = 'menu_name';
	}
if(!isset($_POST['position']) || empty($_POST['position'])){
	$error[] = 'position';
	}
*/

//OR

function check_required_field($required_array){
$field_error = array();
//Required fields
foreach($required_fields as $fieldname) {
	if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) && ($_POST[$fieldname] != 0)){
		$error[] = $fieldname;
		}
	}
	return $field_error;
}
function check_max_field_length($field_length_array){
$field_error = array();
//Field Length
foreach($fields_length_array as $fieldname => $maxlength){
	if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){
		$error[] = $fieldname;
		}
	}
	return $field_error;
}

function display_errors($error_array){
						echo "<p class=\"errors\">";
					echo "Please review the following fields: <br />";
					foreach($error as $err) {
						echo "*" . $err . "<br />";
						}
						echo "</p>";

	}
?>
<?php
//File Name: functions.php
/***********************************************************/
/*This file contains all the functions used in this project*/
/**********************************************************/

//3. Perform DB query
//error handling for returning query
function mysql_prep($value){
	/*This function will help me handle issues of
	having special characters like "" as the values of the 		    insert into query*/
	$magic_qotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string");
	
	if ($new_enough_php){
		if($magic_qotes_active){$value = stripslashes($value);}
		$value = mysql_real_escape_string($value);
		} else {
			if(!$magic_qotes_active){$value = addslashes($value);}
			}
			return $value;
	}
	
function redirect_to($location = NULL){
	if ($location != NULL){
		header("Location: {$location}");
		exit;
		}
	}	
function confirm_query($result_set){
						if(!$result_set){
						die("Database query failed: " .mysql_error());
						}
					}
//return subjects					
function get_all_subjects(){

					$query = "SELECT * 
							FROM subjects 
							ORDER BY position ASC";
				$subject_set = mysql_query($query);
				confirm_query($subject_set);
				return $subject_set;
	}
//return pages
function get_pages_of_all_subjects($subject_id){

	$query = "SELECT * 
								FROM pages 
								WHERE subjects_id1 = {$subject_id} 
								ORDER BY position ASC";
					$page_set = mysql_query($query);
					confirm_query($page_set);			
					
					return $page_set;
	}

function get_subject_by_id($subject_id){

	$query = "SELECT * ";
	$query .= "FROM subjects ";
	$query .= "WHERE id = {$subject_id} ";
	$query .= "LIMIT 1";

	$result_set = mysql_query($query);
	confirm_query($result_set);

	if ($subject = mysql_fetch_array($result_set)){
		return $subject;
		} else {
			return NULL;
			}
	}
function get_page_by_id($page_id){
	
	$query = "SELECT * ";
	$query .= "FROM pages ";
	$query .= "WHERE id = {$page_id} " ;
	$query .= "LIMIT 1";

	$result_set = mysql_query($query);
	confirm_query($result_set);
	
	if ($page = mysql_fetch_array($result_set)){
		return $page;
		} else {
			return NULL;
			}
	}
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_subj = NULL;
		$sel_page = NULL;
		}
	}
function navigation ($sel_subject, $sel_page){
	
	$output =  "<ul class=\"subjects\">";
				$subject_set = get_all_subjects();
				//4. Use returned data
				while($subject = mysql_fetch_array($subject_set)){
					$output .= "<li";
					if ($subject["id"] == $sel_subject["id"]){
					$output .= " class=\"selected\"";
					}
					$output .= "><a href=\"edit_subject.php?subj=" .urlencode($subject["id"]). "\">{$subject["menu_name"]}</a></li>";
					
					//5. A loop in a loop: Pages in Subjects
				$page_set = get_pages_of_all_subjects($subject["id"]);				
				$output .= "<ul class=\"pages\">";
				//4 again. Use returned data
				
				while($pages = mysql_fetch_array($page_set)){
					$output .= "<li";
					if($pages["id"] == $sel_page["id"]){
					$output .= "class=\"selected\"";
					}
					$output .= "><a href=\"content.php?page=".urlencode($pages["id"])."\">{$pages["menu_name"]}</a></li>";
					} $output .= "</ul>";
					}
				
                $output .= "</ul>";
				return $output;

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

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

<p>Position: <select name="position"><?php 
if (!$new_page){
	$page_set = get_pages_of_all_subjects($sel_page['subject_id']);
	$page_count = mysql_num_rows($page_set);
	}
	else
	{
		$page_set = get_pages_of_all_subjects($sel_subject['id']);
		$page_count = mysql_num_rows($page_set)+1;
		}
		for($count=1; $count<=$page_count; $count++){echo"<option value=\"{$count}\">{$count}</option>";}
?></select></p>
<p>Visible: <input type="radio" name="visible" value="0"<?php 
if($sel_page['visible'] == 0) {echo " checked";}
?> />No   <input type="radio" name="visible" value="0"<?php 
if($sel_page['visible'] == 1) {echo " checked";}
?> />Yes</p>
<p>Content: <br />
<textarea name="content" rows="20" cols="80"> <?php 
echo $sel_page['content'];
?></textarea>
</p>
<?php //File Name: new_page.php ?>
<?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($error, check_required_field($required_fields, $_POST));
		
		$fields_with_lengths = array('menu_name' => 30);
		$errors = array_merge($error, check_max_field_length($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 pages (
						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 .= "<br />" . mysql_error();
			}
		} else {
			if (count($errors) == 1) {
				$message = "There was 1 error in the form.";
			} else {
				$message = "There were " . count($error) . " errors in the form.";
			}
		}
		// END FORM PROCESSING
	}
?>

<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
	<tr>
		<td id="navigation">
			<?php echo navigation($sel_subject, $sel_page, $public = false); ?>
			<br />
			<a href="new_subject.php">+ Add a new subject</a>
		</td>
		<td id="page">
			<h2>Adding New Page</h2>
			<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
			<?php if (!empty($error)) { display_errors($error); } ?>
			
			<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" />
			</form>
			<br />
			<a href="edit_subject.php?subj=<?php echo $sel_subject['id']; ?>">Cancel</a><br />
		</td>
	</tr>
</table>
<?php include("includes/footer.php"); ?>

<?php //File Name: edit_page.php ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>

<?php 
if (intval($_GET['subj']) == 0){
	redirect_to("content.php");
	}
	include_once("includes/form_functions.php");
//START FORM PROCESSING
//only excute the form processing if the form has been submitted
	if (isset($_POST['submit'])){
		//initialize array to hold errors
		$error = array();
// perform validation on form data
$required_fields = array('menu_name', 'position', 'visible', 'content');
$error = array_merge($error, check_required_fields($required_fields));

$fields_with_length = array('menu_name' => 30);
$error = array_merge($error, check_required_fields($required_fields));

//Form Variables
$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']);

$query = "UPDATE subjects SET 
			menu_name = '{$menu_name}',
			position = {$position},
			visible = {$visible},
			content = '{$content}'
			WHERE id = {$id}";
		$result = mysql_query($query);
		if(mysql_affected_rows() == 1){
			//Successful
			$message = "The Subject was successfully updated";
			
			} else {
				//Failed
				$message = "NO Update was made: " . mysql_error();
				}
		} else {
			//Errors Occured
			if(count($error)<2){
				$message = "There was an error in the form.";
				} else {
					$message = "There were " .count($error). " errors in the form.";
					}
			}




		 //end : if isset condition
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
    	<table id="structure">
        	<tr>
            	<td id="navigation">
                <ul class="subjects">
				<?php echo navigation ($sel_subject, $sel_page); ?>
                </ul>

                </td>
                
                <td id="page">
                <h2>Edit Page <?php echo $sel_page['menu_name']; ?></h2> <?php if(!empty($message)){echo"<p class=\"message\">".$message."</p>";} ?>
                <?php 
				if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";}
				?>
                                <?php 
				if (!empty($error)) {display_errors($error);}
				?>

                <form action="edit_page.php?page=<?php echo urlencode($sel_page['id']); ?>" method="post">
                <?php include "page_form.php" ?>
                <input type="submit" name="submit" value="Update Page" />
                   
                <a href="delete_page.php?subj=<?php echo urlencode($sel_page['id']); ?>" onClick="return confirm('Are you sure');">Delete Subject</a>
                </form>
                
                <br />
                <a href="content.php">Cancel</a>
                <div style="margin-top:2em; border-top:1px solid #000000;">
                <h3>Pages in this subject are: </h3>
                <ul>
                <?php 
				$subject_pages = get_pages_of_all_subjects($sel_subject['id']);
				?>
                </ul>
                </div>
                </td>
            </tr>
        </table>
<?php require("includes/footer.php"); ?>
<?php // File Name: new_subject.php ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php find_selected_page(); ?>

<?php include("includes/header.php"); ?>
    	<table id="structure">
        	<tr>
            	<td id="navigation">
                <ul class="subjects">
				<?php echo navigation ($sel_subject, $sel_page); ?>
                </ul>

                </td>
                
                <td id="page">
                <h2>Add Subject</h2>
                <form action="create_subject.php" method="post">
                <p>Subject name: <input type="text" name="menu_name" value="" id="menu_name" /></p>
                
                <p>
                Position: <select name="position">
                <?php 
				$subject_set = get_all_subjects();
				$subject_count = mysql_num_rows($subject_set);
				for($count=1; $count<=$subject_count; $count++){echo"<option value=\"{$count}\">{$count}</option>";}
				 ?>
                
                </select>
                </p>
                
                <p>Visible: <input type="radio" name="visible"  value="0" />No   <input type="radio" name="visible" value="1"  />Yes</p>
                
                <input type="submit" value="Add Subject" />
                </form>
                
                <br />
                <a href="content.php">Cancel</a>
                </td>
            </tr>
        </table>
<?php require("includes/footer.php"); ?>

Hi everyone, am a newbi and am using the above project to learn. Am halfway the project and am stuck because the files, new_page.php and edit_page.php are not working fine. I would appreciate every effort made to pull me through so as minimize my level of frustration in PHP. Thanks

 

The new_subject.php is working fine and am using functions.php, form_functions.php and page_form.php as include files.

Link to comment
Share on other sites

Ok tnx for your attention. For the new_page.php, it shows only the Page Name and Position, it leaves out, Visible and Content with the submit button too. And am not sure if there is a complete transaction b/w the form and the database

 

For the edit_page.php, it has the same issue with new_page.php and secondly an error above the form saying that the variable error and new_page are not defined.

Futhermore, an SQL error "Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY position ASC' at line 4".

 

Thanks Boss.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.