douzhi4991 2018-07-03 08:17
浏览 43
已采纳

如何使用辅助成员详细信息显示主要成员列表

I am using CodeIgniter, I issue is regarding MySQL query. I have two table which is member and relation.

Member table

What I am doing is, I am adding the all the user in the member table according to the member_type. If member_type is 1 then that is a primary member if 2 then a secondary member

enter image description here

Relation table

In the relation table, I am defining the relation of the user. For example primary_member_id is 1 then secondary members are 3 and 6 and secondary member details are available in the member table.

enter image description here

Now I need a query to display the all the primary member in the list and related to their secondary member name and other details.

I am using data table with a child row. so that I can display the primary name in the list and on click to the plus sign to display secondary member.

I tried below join code.

SELECT * FROM `member` LEFT JOIN relation on member.id=relation.primary_member_id WHERE member.member_type=1

below is the output of the above query. In this, I am getting the primary member name twice and how to display the secondary member name enter image description here

I need an output

enter image description here

data table

     function format ( d ) {
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>Full name:</td>'+
            '<td>'+d.name+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Mobile number:</td>'+
            '<td>'+d.phone+'</td>'+
        '</tr>'+
        '<tr>'      
    '</table>';
}
$(document).ready(function() {
            var oTable =$('#all_list').DataTable( {
                "processing": true,
                // "serverSide": true,
                "pageLength": 10,
                "ajax": {
                    "url": baseUrl+ "/index.php/Search/Listdetails_ajax",
                    "type": "POST"
                },
                "columns": [
                   {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
                    },
                { "data": "profile_pic","className":"img_list_pic",
                "render": function (data, type, full, meta) {
                return '<img src='+baseUrl+'/uploads/images/'+data+' class="search_pic">';
                }
                 },
                { "data": "name" },
                { "data": "phone" }
            ],
               "order": [[1, 'asc']],


            } );
    $('#all_list tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = oTable.row( tr );

        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
        }
    } );

} );

ajax

public function Listdetails_ajax(){  
    $draw = intval($this->input->get("draw"));
    $start = intval($this->input->get("start"));
    $length = intval($this->input->get("length"));
    $books = $this->Search_model->getTotalList_of_primary();
    $books_of_secondary = $this->Search_model->getTotalList_of_secondary();

    $data['draw'] = 1;
    $data['recordsTotal'] = count($books);
    $data['recordsFiltered'] = count($books);
    foreach ($books as $key => $row) 
    {
        $arr_result = array(
                   "profile_pic" => $row->profile_pic,
                   "name" => $row->first_name.' ' .$row->last_name,
                    "phone" => $row->phone
        );
        $data['data'][] = $arr_result;
      }
      //print_r($arr_result);
    echo json_encode($data);
    exit;
}

/model/

public function getTotalList_of_primary(){
      $this->db->select('*');
      $this->db->from('member');
      $this->db->where('member_type',1);
      $query = $this->db->get();
      $res   = $query->result();        
     return $res;
}

public function getTotalList_of_secondary(){
    /*what query I have to use here*/
}
  • 写回答

2条回答 默认 最新

  • duanliao3826 2018-07-11 13:59
    关注

    Following the idea of using a details popup as seen in details row, the controller would look like this:

    public function Listdetails_ajax(){  
       $draw = intval($this->input->get("draw"));
        $start = intval($this->input->get("start"));
        $length = intval($this->input->get("length"));
        $books = $this->Search_model->getTotalList_of_primary();
    
        $data['draw'] = 1;
        $data['recordsTotal'] = count($books);
        $data['recordsFiltered'] = count($books);
        foreach ($books as $key => $row) 
        {
            //print_r($row->customer_id);
            $arr_result = array(
                        "member_id" => base64_encode($this->encryption->encrypt($row->member_id)),
                        "profile_pic" => $row->profile_pic,
                        "name" => $row->first_name.' ' .$row->last_name,
                        "phone" => $row->phone,
                        "chss_no" => $row->chss_no,
                        "emp_id" => $row->emp_id,
                        "address" => $row->address
                        // "member_type" => $row->member_type
            );
    
             $array_secondary = array();
             $books_of_secondary = $this->Search_model->getTotalList_of_secondary($row->customer_id);
             foreach ($books_of_secondary as $key => $row) 
                {
                    //print_r($row->customer_id);
                    $arr_result2 = array(
                                "s_member_id" => base64_encode($this->encryption->encrypt($row->member_id)),
                                "s_profile_pic" => $row->profile_pic,
                                "s_name" => $row->first_name.' ' .$row->last_name,
                                "s_phone" => $row->phone,
                                "s_chss_no" => $row->chss_no,
                                "s_emp_id" => $row->emp_id,
                                "s_address" => $row->address
                    );
    
    
                    $array_secondary[] = $arr_result2;
                }
             $arr_result['secondary'] =  $array_secondary;  
             $data['data'][] = $arr_result;
          }
        echo json_encode($data);
        exit;
    }
    

    And then in the format function you would access the secondary field to get an array of secondaries. Maybe like this:

    function format(d) {
        // d is the original data object for the row var val;
        if(d.secondary.length == 0) {
            return "There are no secondary members";
        }
    
        var display = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'; 
        for (val of d.secondary) { 
            display += '<tr>' + '<td>Full name:</td>' + '<td>' + val.s_name + '</td>' + '</tr>' + '<tr>' + '<td>Mobile number:</td>' + '<td>' + val.s_phone + '</td>' + '</tr>';
        }
        display += '</table>';
        return display;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动