Codeigniter - 调用控制器功能后,视图页面中的数据被删除

时间:2017-07-09 18:57:50

标签: php codeigniter

我刚刚开始学习codeigniter并且一直致力于创建一个虚拟的Job Portal应用程序。 我的默认控制器“Home”在首次访问Page时从数据库加载作业列表的数据。 但是每当我尝试注册新用户时,索引页面中的先前数据都会被删除。我收到一条错误消息“消息:未定义的变量:JOBS” 即使调用了新的控制器功能并且页面被重定向,也会删除以前的作业列表数据。

以下是来自codeigniter应用程序的MVC。

我的控制器

<?php
 class Home extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->model('HomeModel');

}

public function index() {

    $query = $this->HomeModel->getJobList();
    $data['JOBS'] = null;
    if ($query) {
        $data['JOBS'] = $query;
    }

    $this->load->view('index.php', $data);
}
public function new_user_registration() {
    // Check validation for user input in SignUp form
    $this->form_validation->set_rules('email', 'Email Id / Username', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');
    $this->form_validation->set_rules('fname', 'First Name', 'trim|required');
    $this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
    if (!$this->form_validation->run()) {
        $this->load->view('index.php');
    } else {
        $user_data = array(
            'email_id' => $this->input->post('email'),
            'password' => md5($this->input->post('password')),
            'firstname' => $this->input->post('fname'),
            'lastname' => $this->input->post('lname'),
        );
        $result = $this->HomeModel->registration_insert($user_data);
        if ($result == true) {
            $data['message_display'] = 'Registration Successful !';
            $this->load->view('index.php', $data);
        } else {
            $data['message_display'] = 'Username already exist!';
            $this->load->view('index.php', $data);
        }
    }
}
}
?>

我的模特

<?php
class HomeModel extends CI_Model {
    function __construct() {
        parent::__construct();
    }
    function getJobList() {
        $query = $this->db->get('job_details');
        return $query->result();
    }
// Function to Insert registration data in database
    public function registration_insert($data) {
// Query to check whether username already exist or not
        $condition = "email_id =" . "'" . $data['email_id'] . "'";
        $this->db->select('*');
        $this->db->from('user_profile');
        $this->db->where($condition);
        $this->db->limit(1);
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
// Query to insert data in database
            $this->db->insert('user_profile', $data);
            if ($this->db->affected_rows() > 0) {
                return true;
            }
        } else {
            return false;
        }
    }
}
?>

我的Index.php页面

<?php
$this->load->view('header/header.php');
?>
<div class="modal fade" id="logIn" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Log In</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <p>
                            <form>
                                <div class="form-group">
                                    <label for="exampleInputEmail1">Email address</label>
                                    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputPassword1">Password</label>
                                    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                                </div>
                                <button type="submit" class="btn btn-default">Submit</button>
                            </form>
                        </p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                Not Registered Yet... Click here to <a data-dismiss="modal" data-toggle="modal" href="#signUp">Sign Up !</a>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="signUp" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">New User Registration</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <p>
                            <!-- <form>
                                        <div class="form-group">
                                                    <label for="exampleInputEmail1">Email address</label>
                                                    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
                                        </div>
                                        <div class="form-group">
                                                    <label for="exampleInputPassword1">Password</label>
                                                    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                                        </div>
                                        <button type="submit" class="btn btn-default">Submit</button>
                            </form> -->
                            <form action="Home/new_user_registration" enctype="multipart/form-data" method="post" accept-charset="utf-8">
                                <div class="form-group">
                                    <label for="email">Email Id (Username): </label>
                                    <input name="email" id="email" class="form-control" placeholder="Email Id" type="email" required="required">
                                    <div class="error_msg"></div>
                                </div>
                                <div class="form-group">
                                    <label for="password">Password: </label>
                                    <input name="password" id="password" class="form-control" type="password">
                                </div>
                                <div class="form-group">
                                    <label for="fname">First Name: </label>
                                    <input name="fname" id="fname" class="form-control" type="text">
                                </div>
                                <div class="form-group">
                                    <label for="lname">Last Name: </label>
                                    <input name="lname" id="lname" class="form-control" type="text">
                                </div>
                                <button type="submit" class="btn btn-default">Sign Up</button>
                            </form>
                        </p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                Already Registered... Click here to <a data-dismiss="modal" data-toggle="modal" href="#logIn">Log In</a>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <?php
if (isset($message_display)) {
    echo "<div class='message'>";
    echo $message_display;
    echo "</div>";
}
?>
    <!-- <div class="row">
                <div class="col-md-2"><h3>Job Title</h3></div>
                <div class="col-md-3"><h3>Job Description</h3></div>
                <div class="col-md-2"><h3>Experience Required</h3></div>
                <div class="col-md-3"><h3>Skills Required</h3></div>
                <div class="col-md-2"></div>
    </div> -->
    <?php foreach ($JOBS as $job) {?>
    <div class="row">
        <div class="container-fluid">
            <div class="row bg-primary">
                <div class="col-md-6"><h4><?=$job->job_title;?></h4></div>
                <div class="col-md-6 text-right"><h5><?=$job->posting_date;?></h5></div>
            </div>
            <div class="row">
                <div class="col-md-push-1 col-md-10"><h5><strong>Job Description: </strong></h5></div>
                <div class="col-md-push-2 col-md-10"><p><?=$job->job_desc;?></p></div>
            </div>
            <div class="row">
                <div class="col-md-push-1 col-md-10"><h5><strong>Skills: </strong></h5></div>
                <div class="col-md-push-2 col-md-10"><p><?=$job->skills_required;?></p></div>
            </div>
            <div class="row">
                <div class="col-md-push-1 col-md-4"><p><strong>Exp:&emsp;</strong><?=$job->exp_reqd;?></p></div>
                <div class="col-md-push-1 col-md-4"><p><strong>Salary:&emsp;</strong><?=$job->salary;?></p></div>
                <div class="col-md-4 text-right"><p><button class="btn btn-primary btn-toolbar">Apply</button></p></div>
            </div>
        </div>
    </div>
    <hr>
    <?php }?>
</div>
<?php $this->load->view('footer/footer.php');?>

1 个答案:

答案 0 :(得分:0)

您的代码中有错误。您实际上正在加载index.php视图文件,而您尚未初始化index()函数。请检查下面描述的可能解决方案。

  1. 用户成功注册后,将其重定向到主页

    redirect('home');

  2. 根据您的上述代码,您不会在主页上重定向用户,因此在这种情况下,您需要将数据传递到您的视图,而您却错过了。请在下面查看相同内容。

    if (!$this->form_validation->run()) { // Your code } else { if ($result == true) { $data['message_display'] = 'Registration Successful !'; $query = $this->HomeModel->getJobList(); $data['JOBS'] = null; if ($query) { $data['JOBS'] = $query; } $this->load->view('index.php', $data); } else { $data['message_display'] = 'Username already exist!'; $this->load->view('index.php', $data); } }

  3. 如果不起作用,请告诉我。