Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter MVC - Loading views without losing controller data and form validation

This doesn't make sense to me with how MVC works in codeigniter. I have a 'controller/Company.php' loads using example.com/company/

function index() {
    $page = uri_string();
    $data['title'] = ucfirst($page);

    $this->load->view('templates/header', $data);
    $this->load->view('templates/top_nav', $data);
    $this->load->view($page, $data);
    $this->load->view('templates/footer', $data);
}

This loads 'views/company.php' and displays form:

<h1 class="page-title"><?php echo $title; ?></h1>
    <?php echo form_open('company/update', 'class="form-horizontal" role="form"'); ?>
<input name="company_name" type="text">
    <?php echo form_error('company_name'); ?> //if empty display error
         //rest of form and submit button

I then have an update function inside the Company controller:

 function update() {
    $this->form_validation->set_rules('company_name', 'Company Name', 'trim|required');
       if ($this->form_validation->run() == FALSE)
       {  
           $this->load->view('company');
       } else {
           //update db, load model, post data, success, etc
       }
}

My issues are:

  1. If I just load the form view again then I lose my header, nav, footer, etc.
  2. If I redeclare all 4 of those views, I lose the original data variables: $title, $page
  3. If I use redirect('company'); to load the controller again then I would lose the submitted data and the form_error('company_name'); would be empty

I hope I'm missing something big because I have been staring at this all day and search for answers but can't find a tutorial of how all of this is suppose to work in the "real world" Thanks

like image 385
Chad Priddle Avatar asked Aug 06 '26 13:08

Chad Priddle


1 Answers

Yes of course. Controller method act as single function in your project.

if i explain it more

<?php
    public function one()
    {
        echo '1';
    }

    public function two()
    {
        echo '2';
    }

In here function one don't know what is function two. So both functions are acting as Independent.

According to your question

you load this views in index()

$this->load->view('templates/header', $data);
$this->load->view('templates/top_nav', $data);
$this->load->view($page, $data);
$this->load->view('templates/footer', $data);

but in update() you load only

$this->load->view('company');

so in second function there are no header, navigation, and footer.

Answer for your Question is

<?php
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('templates/header', $data);
        $this->load->view('templates/top_nav', $data);
        $this->load->view('company');
        $this->load->view('templates/footer', $data);

    } else
    {
        //update db, load model, post data, success, etc
    }

For question Two

you can use like this

<?php
    if ($this->form_validation->run() == FALSE)
    {
        $page = uri_string();
        $data['title'] = ucfirst($page);

        $this->load->view('templates/header', $data);
        $this->load->view('templates/top_nav', $data);
        $this->load->view('company');
        $this->load->view('templates/footer', $data);

    } else
    {
        //update db, load model, post data, success, etc
    }
like image 172
Abdulla Nilam Avatar answered Aug 09 '26 04:08

Abdulla Nilam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!