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
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.
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
I need an output
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*/
}