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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。