dotdx80642 2015-09-04 10:27
浏览 28
已采纳

如何在CodeIgniter中执行JOIN操作并获取结果?

for a CI System,i have a reseller table and Users tables. each user is mapped to its reseller by a field called KEY which is unique string. It's in both users table and reseller's table each user(record) has a reseller key mapped to it. I hope you understood the concept. Now i want to create a view for admin. Currently i show the list of reseller and list of users,Now how to create a view in which when i click on reseller i can see only his reseller?

here i fetch all the reseller in index function how to filter based on key

    public function index ()
        {
            $usertype=$this->session->userdata('usertype');
            if($usertype ==="admin")
                {
                    // Fetch all users
                    $this->data['users'] = $this->reseller_m->get();
                    // Load view
                    $this->data['subview'] = 'admin/reseller/index';
                    $this->load->view('admin/_layout_main', $this->data);
                }
            else
                {
                    $this->load->view('permission');
                }   
        }
  • 写回答

2条回答 默认 最新

  • dragonsun00000 2015-09-04 11:08
    关注

    You can use CI active records like this

    $this->db->select('*');
    $this->db->from('reseller');
    $this->db->join('users', 'reseller.KEY = users.KEY');
    $this->db->WHERE('reseller.key',$reseller_id);
    $result = $this->db->get();
    

    Just change the result input to the view. On first page you passing result of all the user result, so if the user clicks on the specific reseller then basically you have to pass the reseller id to the controller, right? using that id just switch the query result to the view by if else condition.

        public function index ()
            {
                $usertype=$this->session->userdata('usertype');
                $reseller_id=$this->input->get('reseller_id');
                if($usertype ==="admin")
                    {
    
                  if($reseller_id){
    
                        // Fetch only resellers users
    
                      $this->db->select('*');
                      $this->db->from('reseller');
                      $this->db->join('users', 'reseller.KEY = users.KEY');
                      $this->db->WHERE('reseller.key',$reseller_id);
                      $this->data['users'] = = $this->db->get();
    
                      }else{
                        // Fetch all users
                        $this->data['users'] = $this->reseller_m->get();
    
                     }
                        // Load view
                        $this->data['subview'] = 'admin/reseller/index';
                        $this->load->view('admin/_layout_main', $this->data);
                    }
                else
                    {
                        $this->load->view('permission');
                    }   
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解