datatables does not display data with a query using JOIN. What to do?

Asked

Viewed 43 times

1

<?php
include('../conection/db.php');
include('function.php');
$query = '';
$output = array();
$query .= "SELECT i.Tipo as Tipo, c.Descricao as Descricao, c.Anexo as Anexo from tbl_instituicao as i JOIN tbl_conteudo  as c ON i.Id_conteudo = c.Id_conteudo";
if(isset($_POST["search"]["value"]))
{
    $query .= 'WHERE Descricao LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"]))
{
    $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
    $query .= 'ORDER BY Id_instituicao DESC ';
}
if($_POST["length"] != -1)
{
    $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
    $image = '';
    if($row["Anexo"] != '')
    {
        $image = '<img src="upload/'.$row["Anexo"].'" class="img-thumbnail" width="50" height="35" />';
    }
    else
    {
        $image = '';
    }
    $sub_array = array();
    $sub_array[] = $image;
    $sub_array[] = $row["Tipo"];
    $sub_array[] = $row["Descricao"];
    $sub_array[] = '<button type="button" name="update" id="'.$row["Id_instituicao"].'" class="btn btn-warning btn-xs update">Actualizar</button>';
    $sub_array[] = '<button type="button" name="delete" id="'.$row["Id_instituicao"].'" class="btn btn-danger btn-xs delete">Apagar</button>';
    $data[] = $sub_array;
}
$output = array(
    "draw"              =>  intval($_POST["draw"]),
    "recordsTotal"      =>  $filtered_rows,
    "recordsFiltered"   =>  get_total_all_records(),
    "data"              =>  $data
);
echo json_encode($output);
?>

Upshot:

No data available in table
Showing 1 to 3 of 3 entries (Filtered from 0 total Entries)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.