如何在PHP中从选择表单中分页搜索结果

时间:2013-03-13 21:59:27

标签: php codeigniter pagination

我得到了一个包含3个不同下拉列表的表单,其目的是对搜索结果进行分页,这在第一页上工作正常,但是当我得到两个或更多页面时,它会显示整个数据库而不仅仅是我的搜索。

[edit]:好吧我用CI的paginator类更新了我的代码。我仍遇到类似问题的困难。现在它正确分页,但当我点击链接时它会告诉我:'where子句'中的未知列'IS NULL LIMIT 9,9'

[edit2]:万一有人在那里找到了这个,我能够在这里使用这种方法解决我的问题http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-query-strings-2/

这是我的代码。

模型:(我有两个函数,一个用于回馈整个搜索结果,另一个用于返回带限制和偏移的结果)

function buscarHotel($dpto, $ciudad, $id_precio, $limit = 10, $offset = 0)
{

    $w=array();
        if (!empty($dpto)) 
        {
             $w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE departamento_id = $dpto)"; 
        }
        if (!empty($ciudad)) 
        {
             $w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE cod_ciudad = $ciudad)"; 
        }
        if (!empty($id_precio)) 
        {
             $w[]="des_precio_referencial IN (SELECT rango FROM preciohotel WHERE id = $id_precio)"; 
        }

    if (count($w)) $where="WHERE ".implode(' AND ',$w); else $where='';
    $query = $this->db->query("select id, nom_comercial, cod_ciudad, des_foto 
                               FROM hotel 
                               $where");
                               #ORDER BY id desc
                               #LIMIT $limit
                               #OFFSET $offset ");

    return $query->result();

}

function buscarHotelLimit($dpto, $ciudad, $id_precio, $limit)
{

    $w=array();
        if (!empty($dpto)) 
        {
             $w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE departamento_id = $dpto)"; 
        }
        if (!empty($ciudad)) 
        {
             $w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE cod_ciudad = $ciudad)"; 
        }
        if (!empty($id_precio)) 
        {
             $w[]="des_precio_referencial IN (SELECT rango FROM preciohotel WHERE id = $id_precio)"; 
        }

    if (count($w)) $where=implode(' AND ',$w); else $where='';
    /*$query = $this->db->query("select id, nom_comercial, cod_ciudad, des_foto 
                               FROM hotel 
                               $where");*/
                $this->db->select('id, nom_comercial, cod_ciudad, des_foto');
                $this->db->where("$where");
                $query = $this->db->get('hotel', $limit, $this->uri->segment(3));

    return $query->result();

}

控制器:

function buscar()
{
    $dpto = (int)$this->input->post('departamentos');
    $ciudad = (int)$this->input->post('ciudades');
    $precio = (int)$this->input->post('precios');   

    $this->load->model('mhotel');
            $this->load->model('mciudad');

            $total = (int)count($this->mbuscador->buscarHotel($dpto, $ciudad, $precio));
    $limit = 9;


    $this->load->library('pagination');

    $config['base_url'] = 'http://infohotelperu.com/buscador/buscar';
    $config['total_rows'] = $total;
    $config['per_page'] = $limit;
    $config['num_links'] = 5;

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


    $info = $this->mbuscador->buscarHotelLimit($dpto, $ciudad, $precio, $limit);        

    $results['info'] = $info;

    $this->load->view('resultados', $results);
}

在我看来,我得到了

<?php echo $this->pagination->create_links(); ?>

2 个答案:

答案 0 :(得分:2)

以下是我的控制器的代码:

$this->load->model("entries");

//pagination
$this->load->library('pagination');

$config['full_tag_open'] = '<div class="full_pagination">';
$config['full_tag_close'] = '</div>';

$config['next_tag_open'] = '<span class="prev_next_pagination">';
$config['next_tag_close'] = '</span>';

$config['prev_tag_open'] = '<span class="prev_next_pagination">';
$config['prev_tag_close'] = '</span>';

$config['num_tag_open'] = '<span class="num_pagination">';
$config['num_tag_close'] = '</span>';

$config['cur_tag_open'] = '<span class="cur_pagination">';
$config['cur_tag_close'] = '</span>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Vorige';
$config['base_url'] = base_url().'index.php/blog/index';

$config['total_rows'] = $this->entries->countAll();
$config['per_page'] = 3;
$config['num_links'] = 10;

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

//end pagination config

$entries = $this->entries->get_all($config['per_page']);

$data['entries'] = $entries;
$data['titel']="Blog - Frederik";
$data['main_content']='index';
$this->load->view('includes/template', $data);

这在我看来:

foreach($entries as $entry){
     // display entry info here
}

<div id="pagination" align="center">
    <?php echo $this->pagination->create_links(); ?>
</div>

这应该有效!不要忘记加载库! ;)

答案 1 :(得分:1)

您应该使用codeigniter分页。像魅力一样工作;)

更多信息:

http://ellislab.com/codeigniter/user-guide/libraries/pagination.html