Codeigniter分页没有显示?

时间:2019-01-03 09:17:44

标签: php codeigniter

我尝试了Codeigniter分页,但到目前为止分页似乎不起作用?我引用了许多以前的答案,但没有帮助。任何帮助将不胜感激。我已经发布了我的控制器,模型和视图页面。希望有人帮助纠正错误..i未引用其他网站。仅显示数据

控制器:

class Events extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
         $this->load->model("admin_model");
         $this->load->model("Home_model");
              $this->load->model("Banners_model");
       $this->load->library('Pagination');
    }
public function index($page = 1){
       //$data['title'] = "All Events"; 
        $data = array();
        $data['title'] = "All Events";
        $where = array();
        $orderby = array('ban.bId', 'desc');
        $join = array();
        $groupby = array();
        $like = array();
        $or_like = array();
        $or_where = array();
        $where_in = array();
        $where_not = array();
        $where = array('ban.act' =>0,'ban.categoryid' =>9,'ban.status' => 1);
        //$or_where['ban.act'] = 0;
        $data['filterevents'] = $get = $this->input->get();
        $select = ('ban.*');

        $all = $this->Home_model->geteventdetail($where, $orderby, $select, $join, "", "", "", "", $like, $or_like, $or_where, $where_in, $where_not);
        //echo $this->db->last_query();exit;
        if (!empty($all)) {
            $config['total_rows'] = count($all);
        } else {
            $config['total_rows'] = 0;
        }
        $config["uri_segment"] = "3";
        $limit = 5;
        $choice = $config["total_rows"] / $limit;
        $config["num_links"] = floor($choice);
        $config['base_url'] = base_url() . 'Events/index';
        $config["cur_page"] = $page;
        if (isset($get['sel']) && !empty($get['sel'])) {
           $config["per_page"] = $get['sel'];
        } else {
           $config["per_page"] = $this->config->item('per_page');
        }
        $this->pagination->initialize($config);
        $offset = $limit * ($page - 1);
        $data["events"] = $this->Home_model->geteventdetail($where, $orderby, $select, $join, $groupby, $limit, $offset, "", $like, $or_like, $or_where, $where_in, $where_not);
         //$data['events'] = $this->Home_model->Geteventdetailsdata();
            $data["links"] = $this->pagination->create_links();
          // $data["links"] = explode(' ',$str_links );
        load_view_frond_template('home/events', $data, $this);
        echo $this->template->render("", true);
    }

型号:

function geteventdetail($where = array(), $orderby = array(), $select = "", $join = array(), $group_by = "", $limit = "", $offset = 0, $row = false, $like = array(), $or_like = array(), $or_where = array(), $where_in = array(), $where_not = array()) {
        $this->db->select($select);
        $this->db->from('hel_banners as ban');
        if (is_array($join) && !empty($join)) {
            foreach ($join as $k => $v) {
                if (is_array($v))
                    $this->db->join($k, $v[0], $v[1]);
                else
                    $this->db->join($k, $v);
            }
        }
        if (is_array($where_in) && !empty($where_in)) {
            foreach ($where_in as $k => $v) {
                if (is_array($v))
                    $this->db->where_in($k, $v);
                else
                    $this->db->where_in($k, $v);
            }
        }
        if (is_array($where_not) && !empty($where_not)) {
            foreach ($where_not as $k => $v) {
                if (is_array($v))
                    $this->db->where_not_in($k, $v);
                else
                    $this->db->where_not_in($k, $v);
            }
        }
        if (is_array($where)) {
            if (!empty($where))
                $this->db->where($where);
        } elseif ($where != "") {
            $this->db->where($where);
        }
        if (is_array($or_where)) {
            if (!empty($or_where))
                $this->db->or_where($or_where);
        } elseif ($or_where != "") {
            $this->db->where($or_where);
        }
        if (is_array($like)) {
            if (!empty($like)) {
                $this->db->like($like);
                $this->db->or_like($or_like);
            }
        } elseif ($like != "") {
            $this->db->like($like);
        }

        if (is_array($orderby) && !empty($orderby)) {
            foreach ($orderby as $k => $v) {
                $this->db->order_by($k, $v);
            }
        }
        if ($group_by)
            $this->db->group_by($group_by);
        if ((int) $limit != 0)
            $this->db->limit($limit, $offset);

        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            if (!$row)
                return $query->result();
            return $query->row();
        }
    }

观看次数:

                       <div class="row">
                 <div class="col-md-8" style="margin-bottom: 20px;padding-top: 20px;border: 1px solid #abd520;">      
                                <?php 

                                foreach ($events as $eve)

                                    { ?>
                                <div class="entry-meta mb-10" style="padding-top: 20px;">

                                      <i class="fa fa-calendar-o"></i> <?php 
                                       $timestamp = strtotime($eve->created_date);
                                          $new_date = date('d-m-Y', $timestamp);
                                          echo $new_date;
                                        ?>

                                </div>
                                <div class="port-info mt-20 mb-20">
                                    <p style="text-align:justify;">
                                    <h4> <a href="<?php echo base_url();?>Events/event_view/<?php echo $eve->bId;?>"><?php echo $eve->bannersTitle; ?></a></h4><br>
                                    <p><?php echo substr($eve->content, 0, 150); ?> ....<a href="<?php echo base_url();?>Events/event_view/<?php echo $eve->bId;?>">Read More</a></p>
                                    </p>
                                </div>
                                  <hr style="color:black;">
                                <?php } ?>

                            </div>      

                        <!--blog comment -->
                        <div id="pagination">
<ul class="tsc_pagination">
 <p><!-- Show pagination links -->
<?php 
echo "". $links."";
 ?></p>

            </div>

当我在控制器中替换此行时,仅显示一条记录并显示分页

if (isset($get['sel']) && !empty($get['sel'])) {
           $config["per_page"] = $get['sel'];
        } else {
           $config["per_page"] = $this->config->item('per_page');
        }

TO

if (isset($get['sel']) && !empty($get['sel'])) {
           $config["per_page"] = 1;
        } else {
           $config["per_page"] = 1;
        }

1 个答案:

答案 0 :(得分:0)

if (isset($get['sel']) && !empty($get['sel'])) {
           $config["per_page"] = $get['sel'];
        } else {
           $config["per_page"] = $this->config->item('per_page');
        }

将其更改为

$config["per_page"] = (isset($get['sel']) && is_numeric($get['sel']) )?$get['sel']:10;

$config["per_page"] = (isset($get['sel']) && is_numeric($get['sel']) )?$get['sel']:($this->config->item('per_page') && is_numeric($this->config->item('per_page')))?$this->config->item('per_page'):10;

并且如果您具有的自动加载库比之前的加载库

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

替换

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

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

您正在使用get方法。因此,如果您选择了任何一个选项,并且获得了url ..基本url以达到配置效果

之后

$config['base_url'] = base_url() . 'Events/index';

添加

if(isset($_GET))
            {
                $config['first_url']        = $config['base_url'].'/1'.'?'.http_build_query($_GET, '', "&");    
            }