Codeigniter搜索与分页不起作用

时间:2015-10-08 10:55:17

标签: php codeigniter search pagination

我有一个包含3个字段的搜索表单,cargo_oferta,nome_oferta和local_oferta,另外2个data_oferta和id_oferta是从数据库设置并输出到模板。

我的分页不起作用。我必须验证表单,所以当表单为false或null时,它会给出表中的所有值(通过“a”搜索,例如%。“a”。%),当它为true时,它会搜索3个表单字段。

问题是我第一次点击一个填充字段ex的分页(第2页):“xgf”它给出: post 我第二次点击分页(例如:第3页)它给出: http://localhost/site/index.php/main_controller/index/2/xgf/ 并继续将分页数放在32之前和索引之后,递增网址...?

我看到2个问题(最后一个)和“xfg”是第二个字段“nome_empresa”并且因为第一次点击它“通过”到网址中的第一个位置它应该是“某事” “/ xgf /”something“而不是”/ xgf /“

控制器:

public function index($cargo_oferta = null,$nome_empresa = null,$local_oferta = null) {

    $this->form_validation->set_rules('data_oferta','Cargo oferta','trim', 'xss_clean');
    $this->form_validation->set_rules('cargo_oferta','Cargo oferta','trim', 'xss_clean');
    $this->form_validation->set_rules('nome_empresa','Nome empresa','trim', 'xss_clean');
    $this->form_validation->set_rules('local_oferta','Local oferta','trim', 'xss_clean');
    $this->form_validation->set_rules('id_oferta','Local oferta','trim', 'xss_clean');

    if ($this->form_validation->run() == FALSE)
    {

        $data = array(
                'anuncio' => 'template/template'
        );
        $config['suffix'] = $cargo_oferta.'/'.$nome_empresa.'/'.$local_oferta;

        $config['base_url'] = base_url().'index.php/main_controller/index/';
        $config['total_rows'] = $this->main_model->count();
        $config['per_page'] = 5;

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        $data["info"] = $this->main_model->index($config["per_page"], $page, 
            $data_oferta = '', $cargo_oferta = 'a', 
            $nome_empresa = 'a', $local_oferta = 'a',
            $id_oferta = 'a');

    }
    else    // form validation true
    {

        $data_oferta = $this->input->post('data_oferta');

        $cargo_oferta = $this->input->get_post('cargo_oferta');
        $nome_empresa = $this->input->get_post('nome_empresa');
        $local_oferta = $this->input->get_post('local_oferta');

        $id_oferta = $this->input->post('id_oferta');

        $data = array(
                'anuncio' => 'template/template'
        );

        $config['suffix'] = $cargo_oferta.'/'.$nome_empresa.'/'.$local_oferta;

        $config['base_url'] = base_url().'index.php/main_controller/index/';

        $config['total_rows'] = $this->main_model_count->index(
            $data_oferta,
            $cargo_oferta,
            $nome_empresa,
            $local_oferta,
            $id_oferta);
        $config['per_page'] = 5;

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        $data["info"] = $this->main_model->index($config["per_page"], $page,
            $data_oferta, $cargo_oferta,
            $nome_empresa, $local_oferta,
            $id_oferta);

        $data["a_link"] = $config['base_url'];

    }       

    $config['num_links'] = 2;
    $config["uri_segment"] = 3;
    $config['use_page_numbers'] = TRUE;


    $this->pagination->initialize($config);

    $data["links"] = $this->pagination->create_links();

    $data["number"]=$this->main_model->count();

    $this->load->view('template/header_index');
    $this->load->view('template/template', $data);
    $this->load->view('template/footer_visual');
    $this->load->view('template/footer');

}   

// main_model_count counts the resulting rows for the search
// main_model gives the results and counts the total number of rows

public function search_anuncios(
        $limit,
        $start,

        $data_oferta,
        $cargo_oferta,
        $nome_empresa,
        $local_oferta,
        $id_oferta
){

    $sql = "SELECT
    empregos.id_oferta,

    empregos.data_oferta,
    empregos.imagem_oferta,
    empregos.cargo_oferta,
    empregos.descricao_oferta,
    empregos.observacoes_oferta,
    empregos.perfil_oferta,
    empregos.local_oferta,

    users.id,
    users.nome_empresa
    FROM trabalho.empregos
    JOIN trabalho.users
    ON empregos.id = users.id

    WHERE empregos.data_oferta LIKE ?
    OR empregos.cargo_oferta LIKE ?
    OR users.nome_empresa LIKE ?
    OR empregos.local_oferta LIKE ?
    OR empregos.id_oferta LIKE ?
    ORDER BY empregos.data_oferta DESC

    limit $start,$limit
    ;";


    $variaveis_form = array(
            $data_oferta,
            $cargo_oferta,
            $nome_empresa,
            $local_oferta,
            $id_oferta
    );

    $q = $this->db->query($sql,$variaveis_form);

    return $q->result();

}

0 个答案:

没有答案