如何从数据库创建分类分类?

时间:2015-10-27 14:03:42

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

大家好我是一个新的codeigniter我在codeigiter中有分页问题我尝试搜索google youtube codeigniter文档和其他网站,但我不能 我的代码中的问题是我的限额我限制1它显示3汉堡 当我点击链接分页时,错误表示404找不到页面  谁可以帮助我: 我的数据库表:

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>

谁可以帮助我或编辑我的代码并为我提供新代码:( :(

2 个答案:

答案 0 :(得分:0)

在你的控制器中替换函数

中的代码
 $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/

答案 1 :(得分:0)

在你的控制器中 -

 $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->pagination->create_links();echo "$links";

Refer- How to create Pagination in Codeiginter?

how to create pagination for url with parameters in codeigniter?