Codeigniter MVC - loading images without losing controller data and form validation
It doesn't make sense to me how MVC works in the coder. I have a download "controller / Company.php" 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);
}
Loads 'views / company.php' and displays the 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
Then I 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 problems:
- If I just load the form view again, I lose my title, nav, footer, etc.
- If I update all 4 of these views, I lose the original data variables: $ title, $ page
- If I use redirect ('company'); to load the controller again I would lose the provided data and form_error ('company_name'); will be empty
Hope I'm missing something big because I've been looking at this all day and looking for answers, but can't find a tutorial on how it should all work in the "real world". thank
source to share
Yes of course. The controller method acts as a single function across your project.
if i explain it more
<?php
public function one()
{
echo '1';
}
public function two()
{
echo '2';
}
Here the function one
doesn't know what a function is two
. So, both functions act independently .
On your question
you load these views into 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 only download
$this->load->view('company');
so there is no header, nav and footer in the second function.
The answer to your question
<?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 the second question
you can use this like
<?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
}
source to share