codeigniter- 3分页加载所有页面上的所有数据

时间:2015-04-28 17:21:19

标签: mysql database codeigniter codeigniter-3

我的模特

function get_all_events($limit = null, $offset = null) {

        if ($limit) {

            $this->db->limit($limit, $offset);
        }

        $this->db->order_by('events.added_on', 'desc');

        return $this->db->get('events');
    }

我的控制器

public function manage_events() {


        $this->securepage();

        $data['events'] = $this->events_model->get_all_events();


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

        $config['base_url'] = site_url('events/manage-events');

        $config['total_rows'] = $this->events_model->get_all_events()->num_rows();

        $config['per_page'] = 2;



        $config['uri_segment'] = 3;


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




        $data = array(
            'news_title' => 'Manage events | kanchan news.com' ,
            'Keywords' => 'manage',
            'url' =>'' ,
            'content'=>  $this->load->view('events/manage', $data, true)
        );



        $this->load->view('kanchan', $data);

    }

我的观点

<div class="container">
    <ol class="breadcrumb">
        <li class="active"><?php echo $this->session->userdata('full_name');?></li>
        <li class="active">Manage Events</li>


    </ol>
  <h1>Manage Events here.</h1>

   <?php echo $this->session->flashdata('msg'); ?>

        <div>
            <?php if ($events->num_rows()): ?>
                    <table class="table table-condensed">
                        <thead>
                            <tr>
                                <th>SN</th>
                                <th>Add Title</th>
                                <th>expire date</th>
                                <th>Added on</th>
                                <th>Action</th>
                            </tr>
                        </thead>

                        <tbody>

                            <?php foreach ($events->result() as $u): ?>
                            <tr>



                                <td><?php echo $u->id; ?></td>
                                <td><a href="<?php echo site_url('events/events-news/'. $u->id.'/'.$u->heading = str_replace(" ", '-', $u->heading));?>" style="text-decoration: none; color:#000;"><?php echo $u->heading = word_limiter($u->heading,10); ?></a></td>
                                <td><?php echo $u->venue; ?></td>
                                <td><?php echo date('Y-m-d | h:i:a', $u->added_on); ?></td>





                                <td>

                                    <a href="<?php echo site_url('events/delete-events/'.$u->id.'/'.$u->heading = str_replace(" ", '-', $u->heading)); ?>" onclick="return confirm('Are you sure want to delete this events?')"><span class="glyphicon glyphicon-trash"></span></a>
                                    &nbsp &nbsp <a href="<?php echo site_url('events/update-events/'.$u->id.'/'.$u->heading= str_replace(" ", '-', $u->heading)); ?>"><span class="glyphicon glyphicon-edit"></span></a>
                                </td>


                            </tr>

                            <?php endforeach; ?>
                        </tbody>                
                    </table>

            <?php else: ?>
                <p>No any Events
                <hr>
                <a href="<?php echo site_url('events/create-events');?>" class="btn btn-danger">Add New Events</a>
            </p>
            <?php endif; ?>
        </div>

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


</div>

它会创建分页&lt; 1 2但它会在第1页上加载所有结果,并且在第2页上也会得到相同的结果,我尝试了很多想法,但它不起作用。 我使用codeigniter 3和mysql。

1 个答案:

答案 0 :(得分:2)

这是因为您在每个页面都获得了所有记录。您没有通过$ limit和$ offset。在你的功能。

$data['events'] = $this->events_model->get_all_events();//getting all records every time.

尝试以这种方式编写manage_event函数

public function manage_events($page_num=1)
{

     $this->securepage();

    $data['events'] = $this->events_model->get_all_events(2,($page_num-1)*2);
    ///keep your rest code here
    //remember this 2 is from your config because you wanted view 2 record per page.You can replace it with your varible.

注意

 $config['base_url'] = site_url('events/manage-events');

这应该是

 $config['base_url'] = site_url('events/manage_events');//underscore not hypen