【问题标题】:How to create pagination with category from database?如何使用数据库中的类别创建分页?
【发布时间】:2016-01-26 23:46:50
【问题描述】:

大家好,我是一个新的 codeigniter,我对 codeigniter 中的分页有问题我尝试在 google youtube codeigniter 文档和其他网站上搜索,我跟随但我不能 我的代码中的问题在于我的 $limit I limt 1 它显示 3 burger 当我点击链接分页错误时说 404 Page Not Found 谁能帮助我: 我的数据库表:

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

我想要按汉堡分类的结果分页,按披萨分页披萨

我的型号代码:

<?php
class Product_model extends CI_Model {

    function get_product($category="") {
        $this->db->select('*');
        $this->db->from('product');

        if($category) {
            $this->db->where('category',$category);
        }
        $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;
    }
}

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Site extends CI_Controller {
     public function __construct()
    {
        parent::__construct();
        $this->load->model('product_model','product');
    }
 public function menu()
    {
        $data = array();
        $data['sides'] = $this->product->get_product('sides');
        $total_sides = $this->product->get_total('sides');
        $limit_sides = 1;
        $link_sides = 'http://localhost/mbl/burger';
        $data['pagination_sides'] = $this->pagination($total_sides,$limit_sides,$link_sides);

        $data['beverages'] = $this->product->get_product('beverages');
        $total_beverages = $this->product->get_total('beverages');
        $limit_beverages = 1;
         $id = $this->uri->segment(3);
        $link_beverages = 'http://localhost/mbl/beverages';
        $data['pagination_beverages'] = $this->pagination($total_beverages,$limit_beverages,$link_beverages);


        $data['burger'] = $this->product->get_product('burger');
        $total_burger = $this->product->get_total('burger');
        $limit_burger = 1;
        $link_burger = 'http://localhost/mbl/site/menu/burger';
        $data['pagination_burger'] = $this->pagination($total_burger,$limit_burger,$link_burger);

        $this->load->view('header_view');
        $this->load->view('nav_view');
        $this->load->view('content_view');
        $this->load->view('content_left_view',$data);
        $this->load->view('content_right_view');
        $this->load->view('footer_view');

    }
    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();
    }

我的看法:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<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($burger as $val) { ?>
        <li><?php echo $val->title; ?></li>
    <?php } ?>
</ul>

<?php echo $pagination_burger; ?>

</body>
</html>

谁能帮助我或编辑我的代码并为我准备一个新代码:( :(

【问题讨论】:

  • 究竟是什么,没有按预期工作??
  • 错误的链接和很多。请帮助我,我非常需要帮助
  • 你应该添加你得到什么样的错误链接,你还有什么其他问题?不要指望任何人只是猜测你的问题。或者,如果他这样做,那可能不是您的实际问题。
  • 好的,我会编辑更多。但是你能帮我吗?我非常需要帮助

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


【解决方案1】:

在你的控制器中替换函数内的代码

 $data = array();
    $config["base_url"] = base_url('My_controller/paginationExample');
    $config['total_rows'] = $this->product->count_all('sides');
    $config['per_page'] = 1;
    $config["uri_segment"] = 3;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["allData"] = $this->My_model->paginationExample($config["per_page"], $page);
    $data["pagination_beverages"] = $this->pagination->create_links();

   $config["base_url"] = base_url('controller/function');
    $config['total_rows'] = $this->product->count_all(burger);
    $config['per_page'] = 1;
    $config["uri_segment"] = 3;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["allData"] = $this->My_model->paginationExample($config["per_page"], $page);
    $data["pagination_burger"] = $this->pagination->create_links();

更多阅读本教程http://w3code.in/2015/10/how-to-do-pagination-in-codeigniter/

【讨论】:

  • 好的,我现在会测试你的代码
【解决方案2】:

在你的控制器中-

 $this->load->library("pagination");
         $config = array();
        $config['base_url'] = site_url().'Controller/function/'.$id;
        $config['total_rows']=$this->your_model->get_records_count($id);
        $config['per_page']=5;
        //$config['use_page_numbers'] = TRUE;
        $config["uri_segment"] = 4; // if passing values else replace 4 with 3

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

        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $this->data['record'] = $this->your_model->get_records($id,$config['per_page'],$page);
        $this->$data["links"] = $this->pagination->create_links();
        $this->load->view('your view page', $this->data);

在模型中-

function name($id,$limit, $start){

        $this->db->where('id',$id);
        $records=$this->db->get('table', $limit, $start)->result();
        return $records;
}

在你看来—— 要么你打电话-$this-&gt;pagination-&gt;create_links();echo "$links";

参考- How to create Pagination in Codeiginter?

how to create pagination for url with parameters in codeigniter?

【讨论】:

  • 什么是记录数?
  • 如果您有任何数据要传递提及该名称,记录数是我的模型中指定的函数名称,您可以指定您的函数名称,member_model 是我的模型名称。
  • 我尝试查看您的代码,但出现 undefine $id 错误。请帮帮我,我非常需要你的帮助?
  • 模型中只有一个函数名
  • 什么是get_records_count?
猜你喜欢
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2017-11-04
  • 2019-02-02
  • 1970-01-01
相关资源
最近更新 更多