Мое назначение - использовать CodeIgniter (инфраструктура MVC) и подключить форму, которая находится в представлении, к базе данных.Я сделал большую часть кода, и я верю, что большая часть кода верна.Я наконец-то получил страницу просмотра (форму) для загрузки, но не могу загрузить функцию Register
в контроллере.Кто-нибудь видит какие-либо ошибки в моем коде?Это очень мне поможет.
form.php (просмотр):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php //echo $title;?></title>
</head>
<body>
<h1>Register for a Dorm</h1>
<form method="post"
action="http://ispace.ci.fsu.edu/~trm07/assignment4/index.php/controller/register/">
<fieldset>
<label>First Name</label>
<input type="text" name="first_name"/>
<label>Last Name</label>
<input type="text" name="last_name"/>
</fieldset>
<label>Undergraduate Level:</label>
<?php
//dropdown list for level
echo '<select name="level">';
$level = array('Freshman','Sophomore', 'Junior', 'Senior');
foreach ($level as $selection) {
echo '<option value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
?>
<label>Gender:</label>
<?php
//dropdown list for gender
echo '<select name="gender">';
$gender = array('Male','Female');
foreach ($gender as $gend_selection) {
echo '<option
value="'.$gend_selection.'">'.$gend_selection.'</option>';
}
echo '</select>';
//radio buttons for dorms
$requested_dorm = array('Landis','Salley','DeGraff','Cawthon','Reynolds');
echo("<fieldset><legend>Requested Dorm</legend>");
foreach ($requested_dorm as $dorm_names){
echo "<input type='radio' name='dorm_name' value='$dorm_names' />
$dorm_names <br />";
}
?>
<br><input type="submit" value="Register">
</body>
</html>
controller.php
<?php
class Controller extends CI_Controller {
function index()
{ //$data['title'] = "Register for a Dorm";
$this->load->view('form');
}
function show()
{
$this->load->model('model');
$dorms = $this->model->get_dorms();
foreach($dorms as $dorm){
if($dorm['dorm_name'] == $dorm_name)
$chosen_dorm = $dorm['dorm_name'];
}
}
function register()
{
$this->load->library('form_validation');
$view_data = array('message' => '');
//If the form was submitted, process it
if (count($_POST) > 0)
{ $dorm_name = $this->input->post('dorm_name');
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$level = $this->input->post('level');
$gender = $this->input->post('gender');
{
//Validate the input
$this->form_validation->set_rules('first_name', 'First Name',
'required|strip_tags|trim');
$this->form_validation->set_rules('last_name', 'Last Name',
'required|strip_tags|trim');
$val_result = $this->form_validation->run();
//Add the data to the database
if ($val_result == TRUE)
{
$this->load->model('model');
$db_result = $this->model->add_student_to_dorm($_POST['dorm_name'],
$_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);
if ($db_result == TRUE)
{
$view_data['message'] = "Added student to the dorm!";
//$view_data['message'] = "Added" . "$_POST['first_name']" . " " .
"$_POST['last_name']". " to " . "$_POST['dorm_name']" . " hall.";
}
else
{
$view_data['message'] = "An error occured adding the student to the dorm!";
}
}
}
$this->load->view('form',$view_data);
//$this->load->model('model');
//$this->model->get_students();
}
}
}
?>
model.php:
<?php
class Model extends CI_Model {
//function to take student info posted from form, and adds to database
public function add_student_to_dorm()
{
$this->load->database($dorm_name, $first_name, $last_name, $level, $gender);
$data = array
(
'dorm_name' =>$dorm_name,
'student_fname' => $first_name,
'student_lname' => $last_name,
'student_level' => $level,
'student_gender' => $gender,
);
$result = $this->db->insert('student', $data);
return $result;
}
//get dorm table results in an array from database, return its rows
public function get_dorms()
{
$this->load->database();
$dorms = $this->db->get('dorm');
$rows = $dorms->result_array();
return $rows;
}
//get student table results in an array from database, return its rows
public function get_students()
{
$this->load->database();
$students = $this->db->get('student');
$stu_rows = $students->result_array();
return $stu_rows;
}
}
?>