Welcome Guest, Not a member yet? Register   Sign In
Datatables and ORM Model
#3

(04-07-2022, 11:38 PM)JustJohnQ Wrote: // Output
$output = array ("sEcho" => intval ($_POST['sEcho']), "whichorders" => $member, "iTotalRecords" => $iTotal, "iTotalDisplayRecords" => $iFilteredTotal, "aaData" => $result['data']);
$out = json_encode ($output);
EDIT: It looks like the parameter names have been changed in the new datatables version but you get the idea.[/code]

Yes, the parameters and structure changed in latest versions

This is so far the code I could build to work with sqlsrv SQL Server, ysing its syntax, etc.
This is a controller which handles post request from Datatables

Code:
$request = \Config\Services::request();
        $post = $request->getPost();
        $db = \Config\Database::connect('sqlserver');

        $draw = $post['draw'];
        $row = $post['start'];
        $rowperpage = $post['length']; // Rows display per page
        $columnIndex = $post['order'][0]['column']; // Column index
        $columnName = $post['columns'][$columnIndex]['data']; // Column name
        $columnSortOrder = $post['order'][0]['dir']; // asc or desc
        $searchValue = $post['search']['value']; // Search value

        $searchQuery = " ";
        if ($searchValue != '') {
            $searchQuery = " AND (name like '%" . $searchValue . "%') ";
        }

        $totalRecords = $db->table('project')->countAllResults();

        $sqlcf = "
            SELECT idproject, name
            FROM schema.project
            WHERE (1=1) " . $searchQuery;

        $querysf = $db->query($sqlcf);
        $totalRecordwithFilter = count($querysf->getResultArray());

        if ($totalRecordwithFilter > 0) {
            $sqlfinal = "
                SELECT *
                FROM schema.project
                WHERE (1=1) " . $searchQuery . "
                ORDER BY " . $columnName . " " . $columnSortOrder;

            if ($rowperpage != -1) {
                $sqlfinal .= "
            OFFSET " . $row . " ROWS FETCH NEXT " . $rowperpage . " ROWS ONLY";
            }

            $queryfinal = $db->query($sqlfinal);
            $data = $queryfinal->getResultArray();

            foreach ($data as $keyd => $valdat) {
                $datos_finales[] = array(
                    "name" => $valdat['nombre'],
                    "logo" => $valdat['logo'],
                    "idproject" => $valdat['idproyecto'],
                );
            }

            ## Response
            $response = array(
                "draw" => intval($draw),
                "recordsTotal" => $totalRecords,
                "recordsFiltered" => $totalRecordwithFilter,
                "data" => $datos_finales
            );
        } else {
            $response = array(
                "draw" => intval($draw),
                "recordsTotal" => $totalRecords,
                "recordsFiltered" => $totalRecordwithFilter,
                "data" => []
            );
        }
        echo json_encode($response);

It is working so far
Reply


Messages In This Thread
Datatables and ORM Model - by kabeza - 04-07-2022, 08:49 AM
RE: Datatables and ORM Model - by JustJohnQ - 04-07-2022, 11:38 PM
RE: Datatables and ORM Model - by kabeza - 04-08-2022, 04:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB