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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路