Hi, I've got two mysql tables "tbl_order_id" and "customers", the mysql tables have in common the field "customer_id", I want to join tables using customer_id for showing customer_name field in datatable (customer_name is in table customers)
This is my tbl_order_id table:
This is my customers mysql
table:
This my datatable outpout:
Now I just show "customer_id" in datatable, this the Query I do for populating datatable :
I want to do a query like below for joining mysql tables (but It doesn't work):
This is my current whole php fetch code for populating my datatable (works, but just show customer_id):
Please anyideas?
This is my tbl_order_id table:
This is my customers mysql
This my datatable outpout:
Now I just show "customer_id" in datatable, this the Query I do for populating datatable :
PHP:
$columns = ' customer_id , order_item, order_date, order_value ';
$table = ' tbl_order_id ';
$where = " WHERE customer_id !='' ".$date_range.$order_item;
$columns_order = array(
0 => 'customer_id',
1 => 'order_item',
2 => 'order_date',
3 => 'order_value'
);
$sql = "SELECT ".$columns." FROM ".$table." ".$where;
I want to do a query like below for joining mysql tables (but It doesn't work):
PHP:
$columns = ' customer_id , order_item, order_date, order_value, customer_name ';
$table = ' tbl_order_id, customers ';
$where = " WHERE tbl_order_id.customer_id=customers.customer_id OR customer_id !='' ".$date_range.$order_item;
$columns_order = array(
0 => 'customer_id',
1 => 'order_item',
2 => 'order_date',
3 => 'order_value',
4 => 'customer_name'
);
$sql = "SELECT ".$columns." FROM ".$table." ".$where;
This is my current whole php fetch code for populating my datatable (works, but just show customer_id):
PHP:
<?php
include 'config/db-config.php';
global $connection;
if($_REQUEST['action'] == 'fetch_data'){
$requestData = $_REQUEST;
$start = $_REQUEST['start'];
$initial_date = $_REQUEST['initial_date'];
$final_date = $_REQUEST['final_date'];
$order_item = $_REQUEST['order_item'];
//$total = $_REQUEST['total'];
if($order_item == 'Todos'){
$order_item = '' ;
}
if(!empty($initial_date) && !empty($final_date)){
$date_range = " AND order_date BETWEEN '".$initial_date."' AND '".$final_date."' ";
}else{
$date_range = "";
}
if($order_item != ''){
$order_item = " AND order_item = '$order_item' ";
}
$columns = ' customer_id , order_item, order_date, order_value ';
$table = ' tbl_order_id ';
$where = " WHERE customer_id !='' ".$date_range.$order_item;
$columns_order = array(
0 => 'customer_id',
1 => 'order_item',
2 => 'order_date',
3 => 'order_value'
);
$sql = "SELECT ".$columns." FROM ".$table." ".$where;
$result = mysqli_query($connection, $sql);
$totalData = mysqli_num_rows($result);
$totalFiltered = $totalData;
if( !empty($requestData['search']['value']) ) {
$sql.=" AND ( order_item LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR order_date LIKE '%".$requestData['search']['value']."%' )";
$sql.=" OR order_value LIKE '".$requestData['search']['value']."'";
}
$result = mysqli_query($connection, $sql);
$totalData = mysqli_num_rows($result);
$totalFiltered = $totalData;
$sql .= " ORDER BY ". $columns_order[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir'];
if($requestData['length'] != "-1"){
$sql .= " LIMIT ".$requestData['start']." ,".$requestData['length'];
}
$result = mysqli_query($connection, $sql);
$data = array();
$counter = $start;
$count = $start;
while($row = mysqli_fetch_array($result)){
$count++;
$nestedData = array();
$nestedData['counter'] = $count;
// $nestedData['tipodegasto'] = $row["tipodegasto"];
// $nestedData['codigo'] = $row["codigo"];
// $nestedData['nombregasto'] = '<a href="mailto:'.strtolower($row["nombregasto"]).'">'.strtolower($row["nombregasto"]).'</a>';
// $nestedData['nombregasto'] = $row["nombregasto"];
$nestedData['customer_id'] = $row["order_value"];
$nestedData['order_item'] = $row["order_item"];
// $time = strtotime($row["order_valuepago"]);
// $nestedData['order_valuepago'] = date('h:i:s A - d M, Y', $time);
$nestedData['order_value'] = $row["order_value"];
$nestedData['order_date'] = $row["order_date"];
// $nestedData['nombrepaciente'] = $row["nombrepaciente"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ),
"recordsTotal" => intval( $totalData),
"recordsFiltered" => intval( $totalFiltered ),
"records" => $data
);
echo json_encode($json_data);
}
?>
Please anyideas?