dongtao9887 2014-03-12 12:41
浏览 33
已采纳

CI MSSQL连接和Paginator类的问题

I seem to be having an issue with the pagination in codeigniter and my search query.

Code

public function index($page = 0){
    $this->db->select('a.UserID, a.Username, a.UserFullName, a.UserJoined, 
                      (Select Top 1 p.phoneNumber From vwSelectUserPhone p Where p.userID = a.userID) As PhoneNumber, 
                      (Select Top 1 e1.emailAddress From vwSelectUserEmail e1 Where e1.userID = a.userID) As EmailAddress,
                      (Select Top 1 c1.shipCompany From vwSelectOrderAddresses c1 Where c1.userID = a.userID) As UserCompany');
    //$this->db->order_by('UserJoined', 'DESC')
    $this->db->distinct();
    if($_POST){
        $usid = $this->input->post('UserID');
        $ustxt = $this->input->post('TextSearch');
        if(!empty($ustxt) || !empty($usid)){
            if(!empty($usid)){
                $this->db->where('a.UserID', $usid);
            }elseif(!empty($ustxt)){
                $this->session->set_userdata('USrch_Txt', $ustxt);
                $this->db->join('vwSelectUserAddress b', 'b.userID = a.userID', 'left');
                $this->db->join('vwSelectUserEmail c', 'c.userID = a.userID', 'left');
                $this->db->join('vwSelectUserPhone d', 'd.userID = a.userID', 'left');
                $this->db->join('vwSelectUserNotes e', 'e.userID = a.userID', 'left');
                $this->db->join('vwSelectUserVehicle f', 'f.userID = a.userID', 'left');
                $this->db->like('a.Username', $ustxt);
                $orlike = array('a.UserFullName'=>$ustxt,
                                'b.addressStreet1'=>$ustxt,
                                'b.addressStreet2'=>$ustxt,
                                'b.addressCity'=>$ustxt,
                                'b.addressState'=>$ustxt,
                                'b.addressPostalCode'=>$ustxt,
                                'c.emailAddress'=>$ustxt,
                                'd.phoneNumber'=>$ustxt,
                                'e.Note'=>$ustxt,
                                'f.vehMake'=>$ustxt,
                                'f.vehModel'=>$ustxt,
                                'f.vehYear'=>$ustxt,
                                'f.vehTran'=>$ustxt,
                                'f.vehMake'=>$ustxt,
                                'f.vehEngine'=>$ustxt,
                                'f.vehDescription'=>$ustxt,);
                $this->db->or_like($orlike);
            }
        }
    }
    $this->db->order_by('a.UserFullName', 'asc');
    $this->db->limit(12, $page);
    $qry = $this->db->get('vwSelectUser a');

    $rs = $qry->result_array();
    $config['base_url'] = '/users';
    $config['per_page'] = 12;
    $config['total_rows'] = $this->db->count_all('vwSelectUser');
    $config['num_links'] = 5;
    $config['first_link'] = '<span class="fa fa-angle-double-left page_num"></span>';
    $config['last_link'] = '<span class="fa fa-angle-double-right page_num"></span>';
    $config['cur_tag_open'] = '<span class="page_num bold">';
    $config['cur_tag_close'] = '</span>';
    $config['next_link'] = '<span class="fa fa-angle-right page_num"></span>';
    $config['prev_link'] = '<span class="fa fa-angle-left page_num"></span>';
    $config['uri_segment'] = 2;
    $config['num_tag_open'] = '<span class="page_num">';
    $config['num_tag_close'] = '</span>';
    $this->pagination->initialize($config);
    $data['rs'] = $rs;
    $data['paging'] = $this->pagination->create_links();
    $data['current_page'] = $page;
    $this->load->view('templates/header');
    $this->load->view('pages/users/list', $data);
    $this->load->view('templates/footer');
}

As you can see, I have some search parameters in here. I need to change the $config['total_rows'] to $this->db->count_all_results('vwSelectUser'), however when I do I am faced with the following error:

Column "vwSelectUser.UserFullName" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

How can I fix this, #1 so it will display the proper pagination links (if there should be any), #2 to get the proper count of the filtered return?

I had though that by doing this: $config['total_rows'] = ($_POST) ? $this->db->count_all_results('vwSelectUser a') : $this->db->count_all('vwSelectUser a'); that it should do the trick, but I get the error specified.

  • 写回答

1条回答 默认 最新

  • duancheng1955 2014-03-12 13:24
    关注

    I guess there is so much going on in the code I can not tell what you are trying to do.

    1 - Run a SQL profiler trace to capture what you are actually sending to the database engine.

    http://technet.microsoft.com/en-us/library/ms181091.aspx

    2 - All the joins can be done right in a TSQL statement. In fact, a user defined view in the database can simplify the calls in PHP.

    http://technet.microsoft.com/en-us/library/ms181091.aspx

    3 - The error code happens when you are grouping by one set of columns and try to do an order by with a column that is not in the list.

    When grouping, either the column has to be in the group by list or an aggregate function on another column.

    4 - Like any old style ASP code, comment out sections that do not work. Get it working and incrementally add back the code.

    In short, without the actual code and database, I can only give you the above guide lines. I think 1 & 2 might simplify the code and give you insight in what is wrong.

    Good Luck, J

    PS: Comment out $this->db->order_by('a.UserFullName', 'asc'); and see if the calculation works. A separate call to the database can figure out the total and might not cause this error. However, a view might definitely clean up the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写