How can I fetch multiple table data with multiple queries to one jquery dataTable
.
I want to fetch multiple table data of multiple queries to one data-table. can i do it with one query according to my code, I tried MySQL UNION, Not suitable for me.
Fetch.php
$query1 = "SELECT SUM(score) AS marks, from_date FROM table1 WHERE stat = 0 GROUP BY name ";
$statement = $db->prepare($query1);
$statement->execute();
$output = array('data' => array());
$count = $statement->rowCount();
if($count > 0) {
while($row = $statement->fetch(PDO:: FETCH_OBJ)) {
$score = $row->marks;
$date = $row->from_date;
$Team = "Team 1";
$output['data'][] = array( $score,$date,$Team);
} // /while
}// if num_rows
$query2 = "SELECT SUM(marks) AS marks, from_date FROM table2 WHERE stat = 0 GROUP BY name ";
$statement = $db->prepare($query2);
$statement->execute();
$output = array('data' => array());
$count = $statement->rowCount();
if($count > 0) {
while($row = $statement->fetch(PDO:: FETCH_OBJ)) {
$score = $row->marks;
$date = $row->from_date;
$Team = "Team 2";
$output['data'][] = array( $score,$date,$Team);
}
}// if num_rows
echo json_encode($output);
html
<table id="Table">
<thead>
<tr>
<th>Score</th>
<th>Start Date</th>
<th>Team</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
var score;
$(document).ready(function(){
score = $('#Table').DataTable({
'ajax': 'fetch.php',
'order': []
});
});
</script>