Doubt codeigniter

Asked

Viewed 73 times

0

Opa Galera! I’m starting with php and codeigniter, I have a question: I have two controllers, each with crud functions, but would need the create(form) views to be loaded on a 3rd page together, how to do that? (As if they were a single form, but in reality they will be two) ,or a tip otherwise p 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>');
    }

}
  • I think you should be mixing the attributes. The ideal thing for you is to put the Ruds in the model. After this, Voce must create the controller and call the view. After you pass the form_validation validation, ai Voce must instantiate the models and use their functions.

  • @Israelzebulon edited the post with the models and controllers to see ,then I have two views of Formularios that are the create and I need to call them both in one page, as I do?

  • I don’t know if I get it. There are 2 different views, each one contains a form, you want to call and display the 2 in 1 only html document, that’s it?

  • That @Lfziron, only when I try to do it through $this->load->view('view1'); $this->load->view('view2'); gives error

  • What kind of mistake?

  • @Lfziron An Error Was Encountered Unable to load the requested file: events.php

  • This means that the system cannot read the application/views/events file.php. Have you checked the existence of this file? Do you really have that name? The system has read permission in the file?

  • You want to load the view $this->load->view('categorias_subscricao_form', $data); and $this->load->view('eventos_form', $data); at the same time? is that?

  • That @Israelzebulon

  • I sent a reply to Voce to test. If there is error, send it here.

  • What? It worked?

  • Yes @Israelzebulon thanks!!

Show 7 more comments

1 answer

0


In view events:

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'),
        );

        $data2 = 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', $data2);
        $this->load->view('eventos_form', $data);

    }

 Tente chamar as views dessa forma.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.