doufenyu7610 2015-07-27 09:59
浏览 79
已采纳

CodeIgniter:项目分页

I'm currently having problem with Codeigniter 2.2's Pagination. What I want is to Display the list of the items if it is included from another table.

To express what I mean, this would be logic.

<?php
    $x = 1;
    $this->db->group_by("item_id");
    $query = $this->db->get('item_inventory_inhouse');
    foreach ($query->result() as $row4)
    {
        $this->db->where('item_id', $row4->item_id);
        $query = $this->db->get('items');
        foreach ($query->result() as $row)
        {
            echo '<tr>';
            echo '<td>' . $x++ . '</td>';
            echo '<td><a href="">' . $row->item_id . '</a></td>';
            echo '<td><a href="">' . $row->item_name . '</a></td>';
            echo '<td>' . $row->item_category . '</td>';
            echo '<td>' . $row->item_costprice . '</td>';
            echo '<td>' . $row->item_retailprice . '</td>';
            echo '<td>' . $row->item_tax . '%</td>';
            echo '</tr>';
        }
    }
?>

Since I am doing pagination. I can't write the code above.

Well, I tried to formulate some code. Here is my Controller:

function pharmacy()
{
    if($this->session->userdata('logged_in'))
    {
        $total_row = $this->pharmpagination_model->record_count();
        $config['num_links'] = 2;
        $config['base_url'] = base_url().'index.php/inventory/pharmacy/';
        $config["total_rows"] = $total_row;
        $config['per_page'] = 30;
        $config['full_tag_open'] = '<nav><ul class="pagination">';          
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['full_tag_close'] = '</ul></nav>';          
        $config['next_tag_open'] = '<li><span aria-hidden="true">';
        $config['next_link'] = '&raquo;';
        $config['next_tag_close'] = '</span></li>';         
        $config['last_link'] = 'Last';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = 'First';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li><span aria-hidden="true">';
        $config['prev_link'] = '&laquo;';
        $config['prev_tag_close'] = '</span></li>'; 

        $this->pagination->initialize($config);
        if($this->uri->segment(3)){
        $page = ($this->uri->segment(3)) ;
          }
        else{
               $page = 1;
        }
        $data["results"] = $this->pharmpagination_model->fetch_data($config["per_page"], $page);
        $str_links = $this->pagination->create_links();
        $data["links"] = explode('&nbsp;',$str_links );

            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];   
            $data['title'] = 'Pharmacy Inventory';
            $this->load->view('inventory/pharmacy', $data);
    }
   else
   {
            $this->session->set_flashdata('message', 'Oops! You have to Login');
     //If no session, redirect to login page
            redirect('login', 'refresh');
   }      
}

and here is the Model

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

class PharmPagination_Model extends CI_Model {
      function __construct() {
        parent::__construct();
    }
    // Count all record of table "contact_info" in database.
      public function record_count() {
        return $this->db->count_all("item_inventory_inpharmacy");
    }

    // Fetch data according to per_page limit.
    public function fetch_data($limit, $id) {

        $this->db->group_by("item_id"); 
        $mainquery = $this->db->get('item_inventory_inpharmacy'); 
        foreach ($mainquery->result() as $row4)
        {

                $this->db->limit($limit, $id-1);
                $this->db->where('item_id', $row4->item_id);                
                $query = $this->db->get("items");
            }
                if ($query->num_rows() > 0) {
                    foreach ($query->result() as $row) {
                        $data[] = $row;
                    }

                    return $data;
                }


        return false;
   }
}
?>

It actually worked but it is only showing 1 row, where in fact, it should display 5 rows since there are five rows in the database.

Hope someone can help. Thanks

  • 写回答

1条回答 默认 最新

  • dongmeiwei0226 2015-07-27 11:19
    关注

    since you have not provided your data snapshot, it is going to be hard to tell why it's showing one row. i am assuming you have different item_id in the items table and if you look at your model code, that is where the problem is (you are querying item table for each item id but returning the $data array for last item only.)

    you can easily do this using join :)

    public function fetch_data($limit, $id) {
        $this->db->group_by("item_inventory_inpharmacy.item_id"); 
        $this->db->from("item_inventory_inpharmacy");
        $this->db->join("items","items.item_id=item_inventory_inpharmacy.item_id");
        $this->db->limit($limit, $id-1);
        $query = $this->db->get();        
        return $query->result();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退