0

我得到了一个带有 3 个不同下拉列表的表单,其想法是对搜索结果进行分页,这在第一页上运行良好,但是当我获得两个或更多页面时,它会显示整个数据库,而不仅仅是我的搜索。

[编辑]:好的,我用 CI 的分页器类更新了我的代码。我仍然遇到类似问题的麻烦。现在它分页正确,但是当我单击链接时,它告诉我: Unknown column ' IS NULL LIMIT 9, 9' in 'where Clause'

[edit2]:以防万一有人发现这个问题,我可以在这里使用这种方法解决我的问题http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-查询字符串 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(); ?>
4

2 回答 2

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>

这应该工作!不要忘记加载库!;)

于 2013-04-02T03:13:13.093 回答
1

您应该使用 codeigniter 分页。奇迹般有效 ;)

更多信息在这里:

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

于 2013-03-14T01:43:57.260 回答