codeigniter搜索与分页问题

时间:2014-01-30 08:51:19

标签: php codeigniter

我在搜索功能中有一些分页问题

这是代码

控制器:

if(isset($_POST['search']))
            {
                $data['item'] = mysql_real_escape_string($this->input->post('search'));
                $this->session->set_userdata('item', $data['item']);
            } else {
                $data['item'] = $this->session->userdata('item');
            }

            $page=$this->uri->segment(3);
            $limit=16;
            if(!$page):
            $offset = 0;
            else:
            $offset = $page;
            endif;  
            //$data['barang'] = $this->input->post('search');
            $data['search'] = $this->main_model->search($data['branchid'],$data['item'],$offset,$limit);
            $total_search   = $this->main_model->total_search($data['branchid'],$data['item'],$offset,$limit);
            $config['base_url'] = base_url() . 'product/search/';
            $config['total_rows'] = $total_search->num_rows();
            $config['per_page'] = $limit;
            $config['uri_segment'] = 3;
            $config['first_link'] = 'First';
            $config['last_link'] = 'Last';
            $config['next_link'] = '>>';
            $config['prev_link'] = '<<';
            $config['use_page_numbers'] = TRUE;
            $this->pagination->initialize($config);
            $data['paginator'] =$this->pagination->create_links();

观点:

   <div id="wrap">
        <div id="main">
           <a href="<?= base_url();?>dashboard" style="text-decoration:none; font-size:10px; color:black;"> HOME</a> > <a style="text-decoration:none; font-size:10px; color:black; text-transform:uppercase"> SEARCH</a> > <a style="text-decoration:none; font-size:10px; color:black; text-transform:uppercase"> <?= $barang ;?></a>
            <br><br>
          <?php if(!$search->result_array()):
                echo "bla bla.<br><br>";
                else:
            ?>
           <?php foreach ($search->result_array() as $c) : ?>
           <ul class="wipproducts">
           <li>
                <?php $img_str=$c['g']?>
                <div style="height: 25px;">
                <a style="text-decoration:none; color:black;"   href="<?php echo base_url();?>product/detail/<?php echo $c['a']?>"><h3><?php echo $c['n']; ?></h3></a>
                </div>
                <?php 

                if (empty($img_str))
                    {
                        echo "<img src='".base_url()."assets/image/image_kosong.jpg' width='100' height='100'>";
                    }
                    else
                    {
                        echo '<a><img src="data:../../assets/image/jpg;base64,'.$img_str.'" width="100" height="100" alt="" /></a>';
                    }
                ?>
                </br>
                <small><b><?php 

                if($upcountry == 0)
                {

                    if($c['price'] > $c['price2'])
                    {
                    echo 'IDR', ' ', '<a style="text-decoration:line-through; color:red;">',number_format($c['price'],0,",","."),'</a>','</br>';
                    echo 'IDR', ' ',number_format($c['price2'], 0, ",", ".");
                    }
                    else
                    {
                    echo 'IDR', ' ',number_format($c['price2'], 0, ",", ".");
                    }
                }
                else if ($upcountry == 1)
                {
                    {
                        if($c['price'] > $c['UNITPRICEUC'])
                        {
                        echo 'IDR', ' ', '<a style="text-decoration:line-through; color:red;">',number_format($c['price'],0,",","."),'</a>','</br>';
                        echo 'IDR', ' ',number_format($c['UNITPRICEUC'], 0, ",", ".");
                        }
                        else
                        {
                        echo 'IDR', ' ',number_format($c['UNITPRICEUC'], 0, ",", ".");
                        }
                    }
                }
                else if($upcountry == 2)
                {
                        if($c['price'] > $c['UNITPRICEFUC'])
                        {
                        echo 'IDR', ' ', '<a style="text-decoration:line-through; color:red;">',number_format($c['price'],0,",","."),'</a>','</br>';
                        echo 'IDR', ' ',number_format($c['UNITPRICEFUC'], 0, ",", ".");
                        }
                        else
                        {
                        echo 'IDR', ' ',number_format($c['UNITPRICEFUC'], 0, ",", ".");
                        }

                }
                ?></b></small>
                <small>
                <?php
                    if($c['STOK']==0)
                    {
                        echo '<a style="color:red; font-size:12;">','Out Of Stock','</a>';
                    }
                    else
                    {
                        echo 'Available';
                    }

                ?>
                </small>

                <?php echo form_open('cart/add'); ?>
                    <fieldset>

                        <?php
                                if($c['STOK']==0)
                                {
                                    echo '';
                                }
                                else
                                {
                                    echo '<label>Quantity</label>';
                                    echo form_input('quantity', '1', 'maxlength="5"');
                                }

                            ?>


                        <?php echo form_hidden('id', $c['a']); ?>
                        <?php echo form_hidden('color',$c['color']);?>
                            <?php
                                    if($c['STOK']==0)
                                    {
                                        echo '';

                                    }
                                    else
                                    {
                                        echo '<button type="submit" class="button_product" <?php echo form_submit(); ?> Add</button>';
                                    }

                                ?>


                    </fieldset>
                <?php echo form_close(); ?>
                </li>
            </ul>
           <?php 
           endforeach; 
           endif;
           ?>
            <br/>
                <div id="tnt_pagination">
                    <span class="pagination clearfix"><a><?php echo $paginator; ?></span></a>
                </div>
            <br>
        </div>
    </div>
    <br>

模型中的查询工作正常, 并且视图也返回查询。 但分页不会出现。 有什么建议吗?


编辑,我发现了问题。 似乎问题在于查询,因为 我把LIMIT $ offset,$ limit 在查询结束时。 我将$ offset定义为$ page = $ this-&gt; uri-&gt; segment(3)


固定

1 个答案:

答案 0 :(得分:0)

检查库是否正确加载, $this->load->library('pagination');