Opa Galera! I'm starting with php and codeigniter, I have a question: I have two controllers, each with crud functions, but I would need create (form) views to be loaded on a 3rd page together, how do I do this? (As if they were a single form, but in reality they will be two), or a hint in another way to do this, please !! Model
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Eventos_model extends CI_Model
{
public $table = 'eventos';
public $id = 'idEvento';
public $order = 'DESC';
function __construct()
{
parent::__construct();
}
function get_all()
{
$this->db->order_by($this->id, $this->order);
return $this->db->get($this->table)->result();
}
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
function total_rows($q = NULL) {
$this->db->like('idEvento', $q);
$this->db->or_like('nome_evento', $q);
$this->db->or_like('data_inicio', $q);
$this->db->or_like('data_fim', $q);
$this->db->from($this->table);
return $this->db->count_all_results();
}
function get_limit_data($limit, $start = 0, $q = NULL) {
$this->db->order_by($this->id, $this->order);
$this->db->like('idEvento', $q);
$this->db->or_like('nome_evento', $q);
$this->db->or_like('data_inicio', $q);
$this->db->or_like('data_fim', $q);
$this->db->limit($limit, $start);
return $this->db->get($this->table)->result();
}
function insert($data)
{
$this->db->insert($this->table, $data);
}
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
function delete($id)
{
$this->db->where($this->id, $id);
$this->db->delete($this->table);
}
}
Model
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Categorias_inscricao_model extends CI_Model
{
public $table = 'categorias_inscricao';
public $id = 'idCategorias_Inscricao';
public $order = 'DESC';
function __construct()
{
parent::__construct();
}
function get_all()
{
$this->db->order_by($this->id, $this->order);
return $this->db->get($this->table)->result();
}
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
function total_rows($q = NULL) {
$this->db->like('idCategorias_Inscricao', $q);
$this->db->or_like('nome_categoria', $q);
$this->db->or_like('descricao_categoria', $q);
$this->db->or_like('valor_categoria', $q);
$this->db->from($this->table);
return $this->db->count_all_results();
}
function get_limit_data($limit, $start = 0, $q = NULL) {
$this->db->order_by($this->id, $this->order);
$this->db->like('idCategorias_Inscricao', $q);
$this->db->or_like('nome_categoria', $q);
$this->db->or_like('descricao_categoria', $q);
$this->db->or_like('valor_categoria', $q);
$this->db->limit($limit, $start);
return $this->db->get($this->table)->result();
}
function insert($data)
{
$this->db->insert($this->table, $data);
}
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
function delete($id)
{
$this->db->where($this->id, $id);
$this->db->delete($this->table);
}
}
Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Eventos extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Eventos_model');
$this->load->library('form_validation');
}
public function index()
{
$q = urldecode($this->input->get('q', TRUE));
$start = intval($this->input->get('start'));
if ($q <> '') {
$config['base_url'] = base_url() . 'eventos/index.html?q=' . urlencode($q);
$config['first_url'] = base_url() . 'eventos/index.html?q=' . urlencode($q);
} else {
$config['base_url'] = base_url() . 'eventos/index.html';
$config['first_url'] = base_url() . 'eventos/index.html';
}
$config['per_page'] = 10;
$config['page_query_string'] = TRUE;
$config['total_rows'] = $this->Eventos_model->total_rows($q);
$eventos = $this->Eventos_model->get_limit_data($config['per_page'], $start, $q);
$this->load->library('pagination');
$this->pagination->initialize($config);
$data = array(
'eventos_data' => $eventos,
'q' => $q,
'pagination' => $this->pagination->create_links(),
'total_rows' => $config['total_rows'],
'start' => $start,
);
$this->load->view('eventos_list', $data);
}
public function read($id)
{
$row = $this->Eventos_model->get_by_id($id);
if ($row) {
$data = array(
'idEvento' => $row->idEvento,
'nome_evento' => $row->nome_evento,
'data_inicio' => $row->data_inicio,
'data_fim' => $row->data_fim,
);
$this->load->view('eventos_read', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('eventos'));
}
}
public function create()
{
$data = array(
'button' => 'Create',
'action' => site_url('eventos/create_action'),
'idEvento' => set_value('idEvento'),
'nome_evento' => set_value('nome_evento'),
'data_inicio' => set_value('data_inicio'),
'data_fim' => set_value('data_fim'),
);
$this->load->view('eventos_form', $data);
}
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$data = array(
'nome_evento' => $this->input->post('nome_evento',TRUE),
'data_inicio' => $this->input->post('data_inicio',TRUE),
'data_fim' => $this->input->post('data_fim',TRUE),
);
$this->Eventos_model->insert($data);
$this->session->set_flashdata('message', 'Create Record Success');
redirect(site_url('template'));
}
}
public function update($id)
{
$row = $this->Eventos_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('eventos/update_action'),
'idEvento' => set_value('idEvento', $row->idEvento),
'nome_evento' => set_value('nome_evento', $row->nome_evento),
'data_inicio' => set_value('data_inicio', $row->data_inicio),
'data_fim' => set_value('data_fim', $row->data_fim),
);
$this->load->view('eventos_form', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('eventos'));
}
}
public function update_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->update($this->input->post('idEvento', TRUE));
} else {
$data = array(
'nome_evento' => $this->input->post('nome_evento',TRUE),
'data_inicio' => $this->input->post('data_inicio',TRUE),
'data_fim' => $this->input->post('data_fim',TRUE),
);
$this->Eventos_model->update($this->input->post('idEvento', TRUE), $data);
$this->session->set_flashdata('message', 'Update Record Success');
redirect(site_url('eventos'));
}
}
public function delete($id)
{
$row = $this->Eventos_model->get_by_id($id);
if ($row) {
$this->Eventos_model->delete($id);
$this->session->set_flashdata('message', 'Delete Record Success');
redirect(site_url('eventos'));
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('eventos'));
}
}
public function _rules()
{
$this->form_validation->set_rules('nome_evento', 'nome evento', 'trim|required');
$this->form_validation->set_rules('data_inicio', 'data inicio', 'trim|required');
$this->form_validation->set_rules('data_fim', 'data fim', 'trim|required');
$this->form_validation->set_rules('idEvento', 'idEvento', 'trim');
$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
}
}
Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Categorias_inscricao extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Categorias_inscricao_model');
$this->load->library('form_validation');
}
public function index()
{
$q = urldecode($this->input->get('q', TRUE));
$start = intval($this->input->get('start'));
if ($q <> '') {
$config['base_url'] = base_url() . 'categorias_inscricao/index.html?q=' . urlencode($q);
$config['first_url'] = base_url() . 'categorias_inscricao/index.html?q=' . urlencode($q);
} else {
$config['base_url'] = base_url() . 'categorias_inscricao/index.html';
$config['first_url'] = base_url() . 'categorias_inscricao/index.html';
}
$config['per_page'] = 10;
$config['page_query_string'] = TRUE;
$config['total_rows'] = $this->Categorias_inscricao_model->total_rows($q);
$categorias_inscricao = $this->Categorias_inscricao_model->get_limit_data($config['per_page'], $start, $q);
$this->load->library('pagination');
$this->pagination->initialize($config);
$data = array(
'categorias_inscricao_data' => $categorias_inscricao,
'q' => $q,
'pagination' => $this->pagination->create_links(),
'total_rows' => $config['total_rows'],
'start' => $start,
);
$this->load->view('categorias_inscricao_list', $data);
}
public function read($id)
{
$row = $this->Categorias_inscricao_model->get_by_id($id);
if ($row) {
$data = array(
'idCategorias_Inscricao' => $row->idCategorias_Inscricao,
'nome_categoria' => $row->nome_categoria,
'descricao_categoria' => $row->descricao_categoria,
'valor_categoria' => $row->valor_categoria,
);
$this->load->view('categorias_inscricao_read', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('categorias_inscricao'));
}
}
public function create()
{
$data = array(
'button' => 'Create',
'action' => site_url('categorias_inscricao/create_action'),
'idCategorias_Inscricao' => set_value('idCategorias_Inscricao'),
'nome_categoria' => set_value('nome_categoria'),
'descricao_categoria' => set_value('descricao_categoria'),
'valor_categoria' => set_value('valor_categoria'),
);
$this->load->view('categorias_inscricao_form', $data);
}
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$data = array(
'nome_categoria' => $this->input->post('nome_categoria',TRUE),
'descricao_categoria' => $this->input->post('descricao_categoria',TRUE),
'valor_categoria' => $this->input->post('valor_categoria',TRUE),
);
$this->Categorias_inscricao_model->insert($data);
$this->session->set_flashdata('message', 'Create Record Success');
redirect(site_url('categorias_inscricao'));
}
}
public function update($id)
{
$row = $this->Categorias_inscricao_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('categorias_inscricao/update_action'),
'idCategorias_Inscricao' => set_value('idCategorias_Inscricao', $row->idCategorias_Inscricao),
'nome_categoria' => set_value('nome_categoria', $row->nome_categoria),
'descricao_categoria' => set_value('descricao_categoria', $row->descricao_categoria),
'valor_categoria' => set_value('valor_categoria', $row->valor_categoria),
);
$this->load->view('categorias_inscricao_form', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('categorias_inscricao'));
}
}
public function update_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->update($this->input->post('idCategorias_Inscricao', TRUE));
} else {
$data = array(
'nome_categoria' => $this->input->post('nome_categoria',TRUE),
'descricao_categoria' => $this->input->post('descricao_categoria',TRUE),
'valor_categoria' => $this->input->post('valor_categoria',TRUE),
);
$this->Categorias_inscricao_model->update($this->input->post('idCategorias_Inscricao', TRUE), $data);
$this->session->set_flashdata('message', 'Update Record Success');
redirect(site_url('categorias_inscricao'));
}
}
public function delete($id)
{
$row = $this->Categorias_inscricao_model->get_by_id($id);
if ($row) {
$this->Categorias_inscricao_model->delete($id);
$this->session->set_flashdata('message', 'Delete Record Success');
redirect(site_url('categorias_inscricao'));
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('categorias_inscricao'));
}
}
public function _rules()
{
$this->form_validation->set_rules('nome_categoria', 'nome categoria', 'trim|required');
$this->form_validation->set_rules('descricao_categoria', 'descricao categoria', 'trim|required');
$this->form_validation->set_rules('valor_categoria', 'valor categoria', 'trim|required|numeric');
$this->form_validation->set_rules('idCategorias_Inscricao', 'idCategorias_Inscricao', 'trim');
$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
}
}