【问题标题】:How to make pagination with Database category codeigniter?如何使用数据库类别 codeigniter 进行分页?
【发布时间】:2015-10-31 08:32:55
【问题描述】:

我是 codeigniter 的新手。我对 codeigniter 的分页有疑问。 我试图搜索 google、youtube 和 codeigniter 文档,但没有找到任何东西。 我可以实现一个简单的分页,但我需要在一个表中使用数据库创建多分页。

谁能帮帮我?

我的数据库:

product:
id category title image
1  burger   a     a.jpg
2  burger   b     b.jpg
3  burger   c     c.jpg
4  pizza    d     d.jpg
5  pizza    e     e.jpg
6  pizza    f     f.jpg

我想要的结果是在一页中分页 (按汉堡分页分类汉堡,按披萨比萨)

我的网址是http://localhost/test-ci1/

Model中的代码:

<?php
class Product_model extends CI_Model {
    function get_burgers($category="", $limit, $start) {
        $this->db->select('*');
        $this->db->from('product');

        if($category) {
            $this->db->where('category',$category);
        }
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        return $query->result();
    }

    function get_total($category="") {
        $this->db->select('count(*) AS num_row');
        $this->db->from('product');

        if($category) {
            $this->db->where('category',$category);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->row()->num_row;
    }
}

Controller:

     <?php
class Site extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('product_model','product');
    }

    public function index()
    {
        $this->load->model('Product_model');
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data = array();
        $per_page =2;
        $data["pizza"] = $this->product->get_burgers('pizza', $per_page, $page);
        $total_pizza = $this->product->get_total('pizza');
        $limit= 2;
        $link_pizza = 'http://localhost/test-ci1/pizza';
        $data['pagination_pizza'] = $this->pagination($total_pizza,$limit,$link_pizza);

       $data["burgers"] = $this->product->get_burgers('burger', $per_page, $page);
        $total_burger = $this->product->get_total('burger');
        $limit = 2;
        $link_burger = 'http://localhost/test-ci1/index/burger';
        $data['pagination_burger'] = $this->pagination($total_burger,$limit,$link_burger);

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

    private function pagination($total ,$per_page ,$link) {

        $config['base_url'] = $link;
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['page_query_string'] = TRUE;

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

        return $this->pagination->create_links();
    }

}

在我的View:

<body>

<h1>Pizza</h1>
<ul>
    <?php foreach($pizza as $val) { ?>
        <li><?php echo $val->title; ?></li>
    <?php } ?>
</ul>

<?php echo $pagination_pizza; ?>
<hr>

<h1>Burger</h1>
<ul>
    <?php foreach($burgers as $val) { ?>
        <li><?php echo $val->title; ?></li>
    <?php } ?>
</ul>

<?php echo $pagination_burger; ?>

</body>

我的结果:

Pizza

    pizza1
    burger2

12>
Burger

    burger1
    assasa

12>

【问题讨论】:

  • 您的 foreach 在视图输出中的 html 是什么?你可以编辑你的问题来添加这个吗?很可能错误出现在输出的 html 上
  • 好的,我现在编辑等待
  • 现在我已经在编辑了
  • 可以添加输出的标记代码吗?这将类似于&lt;ul&gt;&lt;li&gt;&lt;a href="blablah/20"&gt;2&lt;/a&gt;&lt;/li&gt; ...

标签: php codeigniter pagination codeigniter-2 codeigniter-3


【解决方案1】:

据我所知,您还没有开发模型分页。您应该添加一个为您执行分页的函数,同时仅从数据库中提取所需的行。

对于您的示例,您的模型中会执行以下操作:

function get_burgers($category="", $limit, $start) {
        $this->db->select('*');
        $this->db->from('product');

        if($category) {
            $this->db->where('category',$category);
        }
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        return $query->result();
    }

在您的控制器中,您应该获取一个汉堡,而不是只获取一个汉堡

$data["burgers"] = $this->burger_model->get_burgers($category, $config["per_page"], $page);

并更改视图中的 foreach 以遍历 burgers

【讨论】:

  • 嗨,兄弟,我尝试跟踪您的代码,但仍然有错误。你可以看到我更新你的代码。
  • $config 数组在分页函数之外不可见。尝试在类声明之后使用protected $config 将其添加为类变量,然后使用$this-&gt;config 在任何地方使用它。或者,您可以为索引函数上分配的 per_page 使用另一个变量
  • 嗨,兄弟,现在限制是正常的,问题是不变的 $page 并且当我点击分页上的链接时 404 Page Not Found 您请求的页面没有找到。我的网址是localhost/test-ci1
  • 你可以看到我的控制器更新和结果更新了
  • $per_page 是您的 $limit$page 是您查询的开始 ($start)。您应该先修复变量名称,然后再检查。
猜你喜欢
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 2016-01-08
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
相关资源
最近更新 更多