简体   繁体   中英

CodeIgniter undefined variable error

I'm new to CodeIgniter and I'm facing the errors of undefined variables. I can't find out what is wrong in my code. Could anybody look at this and help me to locate the bug?

views/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>

Edit method in the Projects controller: controllers/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');
        }

    }
}

And the edit related methods in the project_model models/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();

}

views/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"); ?> 

(Posted answer on behalf of the OP):

Problem solved. Switching lines in the projects controller ends the error.

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM