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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!