CodeIgniter未定义变量错误

时间:2016-06-02 09:33:50

标签: php codeigniter

我是CodeIgniter的新手,我面对未定义变量的错误。我无法找到代码中的错误。任何人都可以看看这个并帮我找到错误吗?

视图/ edit_project.php

<?php
    $attribute = ['class' => 'cmxform'];
    echo form_open('projects/edit/' . $project_data->id . '', $attribute); // add $project_data->id here

    if ($this->session->flashdata('errors')) {
        echo $this->session->flashdata('errors');
    }
?>
<div class="col-md-12">
    <div class="form-group form-animate-text" style="margin-top:40px !important;">
        <?php
            $data = array(
                'type' => 'text',
                'class' => 'form-text',
                'name' => 'project_name',
                'value' => $project_data->project_name
            );
            echo form_input($data);
        ?>
        <span class="bar"></span>
        <?php echo form_label('Nazwa projektu'); ?>
    </div>

    <div class="form-group form-animate-text" style="margin-top:40px !important;">
        <?php
            $data = array(
                'type' => 'text',
                'class' => 'form-text',
                'name' => 'project_body',
                'rows' => '3',
                'value' => $project_data->project_body
            );
            echo form_textarea($data);
        ?>
        <span class="bar"></span>
        <?php echo form_label('Szczegóły projektu'); ?>
    </div>
    <div class="form-group form-animate-text" style="margin-top:40px !important;">
        <?php
            $data = array(
                'type' => 'submit',
                'class' => 'submit btn btn-primary pull-right',
                'name' => 'submit',
                'value' => 'Zapisz zmiany'
            );
            echo form_submit($data);
        ?>
    </div>
        <?php echo form_close(); ?>
</div>

Projects控制器中的Edit方法: 控制器/ projects.php

public function edit($project_id)
{
    $this->form_validation->set_rules('project_name', 'Nazwa projektu', 'trim|required');
    $this->form_validation->set_rules('project_body', 'Szczegóły projektu', 'trim|required');

    if ($this->form_validation->run() == false) {

        $data['project_data'] = $this->project_model->get_projects_info($project_id);

        $data = array('errors' => validation_errors('<strong><div class="bg bg-danger" style="color:#fff"><strong>'));
        $this->session->set_flashdata($data);

        $data['main_view'] = 'edit_project'; // this is my view with a form to update the existing project
        $this->load->view('member_view', $data);
    } else {
        $data = array(
            'project_user_id' => $this->session->userdata('user_id'),
            'project_name' => $this->input->post('project_name'),
            'project_body' => $this->input->post('project_body')
        );

        if ($this->project_model->edit_project($project_id, $data)) {
            $this->session->set_flashdata('project_updated', '<strong>Zapisano zmiany</strong>');
           redirect('projects');
        }

    }
}

与project_model中的编辑相关的方法 模型/ project_model.php

    public function edit_project($project_id, $data)
{
    $this->db->where('id', $project_id);
    $this->db->update('projects', $data);

    return true;
}

public function get_projects_info($project_id)
{
    $this->db->where('id', $project_id);
    $get_data = $this->db->get('projects');
    return $get_data->row();

}

视图/ member_view.php

     <div class="container-fluid mimin-wrapper">

        <?php require_once("application/views/includes/nav.php"); ?>


      <!-- start: content -->
        <div id="content">
                <?php $this->load->view($main_view); ?>
          </div>
      <!-- end: content -->

  </div><!-- end: container-fluid -->

<?php require_once("application/views/includes/footer.php"); ?> 

1 个答案:

答案 0 :(得分:0)

(代表OP发表回答):

问题解决了。项目控制器中的切换线结束错误。

$data = array('errors' => validation_errors('<strong><div class="bg bg-danger" style="color:#fff"><strong>'));
        $data['project_data'] = $this->project_model->get_projects_info($project_id);