Bonjour, j'ai deux tables mysql « tbl_order_id » et « customers », les tables mysql ont en commun le champ « customer_id », je souhaite joindre des tables en utilisant customer_id pour afficher le champ customer_name dans datatable (customer_name est dans la table customer)
Voici ma sortie datatable :
Maintenant, je montre simplement « customer_id » dans datatable, c'est la requête que je fais pour remplir 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;
Je veux faire une requête comme ci-dessous pour rejoindre des tables MySQL (mais cela ne fonctionne pas) :
<?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;
Ceci est mon code de récupération php complet actuel pour remplir ma table de données (fonctionne, mais affiche simplement customer_id) :
<?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="https://www.sitepoint.com/community/t/join-two-mysql-tables-for-populate-datatable/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);
}
?>
S'il vous plaît, des idées ?
Suivez-nous sur Facebook : https://web.facebook.com/mycamer.net/
Pour recevoir les dernières nouvelles sur votre téléphone grâce à l’application Telegram, cliquez ici : https://t.me/+KMdLTc0qS6ZkMGI0
Envoyez-nous un message par Whatsapp : Whatsapp +237 650 50 31 31
The post Rejoignez deux tables MySQL pour remplir la table de données – PHP – SitePoint Forums first appeared on Technique de pointe.