第2页不工作分页

时间:2014-01-21 03:53:05

标签: php codeigniter pagination

我在使用codeigniter分页时遇到问题。问题是当我在分页期间打开第二页时显示错误404 page not found。我不知道为什么,但是第一页它正在运作。

型号:

    public function home($limit='',$segment=''){ 
    $data = array();
    $this->db->where(array('produk.status_produk'=>'1'))->limit($limit,$segment); 
    $i=0;
    foreach ($this->get_all() as $item){
        $data[$i] = $item;
        $this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
        if($this->db->get('foto_produk')->num_rows() > 0){
            $this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
            $foto = $this->db->get('foto_produk')->result();
            foreach ($foto as $pic) {
                $data[$i]->image = $pic->image;
                $data[$i]->thumb = $pic->thumb;
            }
        } else {
            $data[$i]->image = '';
            $data[$i]->thumb = '';
        }
        $i++;
    }
    return $data;
}

控制器:

            $cari = $this->input->post('search');
        $this->db->like('deskripsi_produk', $cari);
        $this->db->or_like('nama_produk', $cari);
        $this->db->order_by('id_produk','desc');
        $t = $this->produk_m->home();

        $this->data->search = $this->input->post('search');

        $this->load->library('pagination');
        $perPage = 2;
        $uri_segment = 2;
        $total = count($t); 
        $config['base_url'] = site_url('search');
        $config['total_rows'] = $total;
        $config['per_page'] = $perPage;
        $config['num_links'] = '2';
        $config['uri_segment'] = $uri_segment;

        $this->pagination->initialize($config);
        $this->data->pagination = $this->pagination->create_links();

        $this->db->like('deskripsi_produk', $cari);
        $this->db->or_like('nama_produk', $cari);
        $this->db->order_by('id_produk','desc');
        $this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));


        $this->template->set_judul('PT Bintang Sriwijaya')
        ->set_css('style')
        ->set_parsial('sidebar','sidebar_view',$this->data)
        ->set_parsial('topmenu','top_view',$this->data)
        ->render('search',$this->data);   
}

查看:

<h2>Hasil Pencarian</h2><br />
<?php if($produk):?>
<h3><center><?php echo $pagination;?></center></h3><br />
<?php foreach($produk as $item): ?>
    <div class="produk-wrap">
        <div class="image-wrap">
            <div class="image-iner">
                <a href="<?php echo base_url().'index.php/produk/'.$item->nama_kategori.'/'.$item->url_produk;?>">
                <?php if($item->thumb == ''): ?>
                    <div class="no-image">
                        <span>Belum ada Gambar</span>
                    </div>
                <?php else: ?>
                    <img src="<?php echo base_url().$item->thumb ?>" />
                <?php endif; ?>
                <?php if ($item->stok == 0): ?>
                    <div class="trans">
                        <span>Stok Habis</span>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        <div class="produk-name">
            <?php echo $item->nama_produk;?>
        </div>
        <?php if($item->harga_baru != 0): ?>
            <div class="harga-lama">
                Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
            </div>
            <div class="harga-jual">
                Rp. <?php echo $this->cart->format_number($item->harga_baru) ?>
            </div>
        <?php else: ?>
            <div class="harga-jual">
                Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
            </div>
        <?php endif; ?>
        </a>
    </div>
<?php endforeach; ?>
<?php else:
echo "Data yang dicari belum ada";
endif;?>

1 个答案:

答案 0 :(得分:0)

请更改这些行

$uri_segment = 2;

$uri_segment = 3;

$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));

$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(3));

并尝试因为段1用于控制器,2用于其中的方法,3可能是分页元素。