Using WHERE
clause
Class ssp.class.php comes with two methods – SSP::simple()
and SSP::complex()
.
Usage of SSP::simple()
method is demonstrated in the server_processing.php script.
Method SSP::complex()
has two additional arguments $whereResult
and $whereAll
that allow to add WHERE
clause to a query.
$whereResult
– Condition to apply to result set onlyIf you need to apply condition to your dataset and let user know that larger dataset is available, supply your condition using
$whereResult
argument. jQuery DataTables will show something likeShowing 1 to X of X entries (filtered from X total entries)
in the information panel.$whereAll
– Condition to apply to all queriesIf you need to apply condition to your dataset without letting user know that larger dataset is available, supply your condition using
$whereAll
argument. jQuery DataTables will show something likeShowing 1 to X of X entries
in the information panel.
Most of the time you need to apply your condition using $whereAll
argument and use null
as a value for $whereResult
argument.
For example, the following excerpt shows code that produces JSON response while adding WHERE position='Software Developer'
clause to the query.
echo json_encode(
SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns, null, "position='Software Developer'" )
);
See below excerpt from ssp.class.php for more information on SSP::complex()
method.
/**
* The difference between this method and the `simple` one, is that you can
* apply additional `where` conditions to the SQL queries. These can be in
* one of two forms:
*
* * 'Result condition' - This is applied to the result set, but not the
* overall paging information query - i.e. it will not effect the number
* of records that a user sees they can have access to. This should be
* used when you want apply a filtering condition that the user has sent.
* * 'All condition' - This is applied to all queries that are made and
* reduces the number of records that the user can access. This should be
* used in conditions where you don't want the user to ever have access to
* particular records (for example, restricting by a login id).
*
* @param array $request Data sent to server by DataTables
* @param array|PDO $conn PDO connection resource or connection parameters array
* @param string $table SQL table to query
* @param string $primaryKey Primary key of the table
* @param array $columns Column information array
* @param string $whereResult WHERE condition to apply to the result set
* @param string $whereAll WHERE condition to apply to all queries
* @return array Server-side processing response array
*/
static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null )
Using JOIN
or GROUP BY
clauses
Solution #1
Workaround described below can be used to add support for any complex query not just JOIN
or GROUP BY
clauses. However it requires modification of original ssp.class.php class.
-
Edit ssp.class.php and replace all instances of
FROM `$table`
withFROM $table
to remove backticks. You will be responsible for escaping your table names if they contain reserved keywords. - Use sub-query as table name
$table
and use any complex query withJOIN
,GROUP BY
or any other clauses.<?php $table = <<<EOT ( SELECT a.id, a.name, b.position FROM table a LEFT JOIN positions b ON a.position_id = b.id ) temp EOT; $primaryKey = 'id'; $columns = array( array( 'db' => 'id', 'dt' => 0 ), array( 'db' => 'name', 'dt' => 1 ), array( 'db' => 'position', 'dt' => 2 ) ); $sql_details = array( 'user' => '', 'pass' => '', 'db' => '', 'host' => '' ); require( 'ssp.class.php' ); echo json_encode( SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns ) );
Solution #2
Alternatively you can try forked version of ssp.class.php created by Emran Ul Hadi, see emran/ssp repository on GitHub.
However the file was forked in May 2014, and hasn’t been kept in sync with minor updates to ssp.class.php
since then.
Les dejo este ejemplo que me resultó funcional.
Algo importante a mencionar es que en la tabla “derecha” para el left join (o segunda tabla como yo lo explicaría) es vital poner un “alias” es decir un “AS” algo…
Aquí usé coordinador.nombre AS `cn` y dentro de mi tabla
array( ‘db’ => ‘cn’, ‘dt’ => 4, ‘field’ => ‘coordinador’), // usé este ‘cn’. Eso me permitió imprimir los valores como quería…
Espero sea útil. Perdí muchas horas en encontrar esta solución.
Saludos!
<?php
$table = “( SELECT obras.*, coordinador.nombre as `cn` FROM obras LEFT JOIN coordinador ON coordinador.id = obras.coordinador ) AS temp”;
$primaryKey = ‘id’;
$columns = array(
array( ‘db’ => ‘id’, ‘dt’ => 0, ‘field’ => ‘id’),
array( ‘db’ => ‘sucursal’, ‘dt’ => 1, ‘field’ => ‘sucursal’),
array( ‘db’ => ‘fechaAsignacion’, ‘dt’ => 2, ‘field’ => ‘fechaAsignacion’),
array( ‘db’ => ‘tipoObra’, ‘dt’ => 3, ‘field’ => ‘tipoObra’),
array( ‘db’ => ‘cn’, ‘dt’ => 4, ‘field’ => ‘coordinador’),
array( ‘db’ => ‘numTicket’, ‘dt’ => 5, ‘field’ => ‘numTicket’),
array( ‘db’ => ‘servicio’, ‘dt’ => 6, ‘field’ => ‘servicio’),
array( ‘db’ => ‘estado’, ‘dt’ => 7, ‘field’ => ‘estado’),
array( ‘db’ => ‘fechaTermino’, ‘dt’ => 8, ‘field’ => ‘fechaTermino’),
array( ‘db’ => ‘numFactura’, ‘dt’ => 9, ‘field’ => ‘numFactura’),
array( ‘db’ => ‘subtotalFactura’, ‘dt’ => 10, ‘field’ => ‘subtotalFactura’),
array( ‘db’ => ‘fechaPago’, ‘dt’ => 11, ‘field’ => ‘fechaPago’),
array( ‘db’ => ‘actaFirmada’, ‘dt’ => 12, ‘field’ => ‘actaFirmada’),
array( ‘db’ => ‘garantiaGenerada’, ‘dt’ => 13, ‘field’ => ‘garantiaGenerada’),
array( ‘db’ => ‘valorPresupuesto’, ‘dt’ => 14, ‘field’ => ‘valorPresupuesto’),
array( ‘db’ => ‘reporteFotografico’, ‘dt’ => 15, ‘field’ => ‘reporteFotografico’),
array( ‘db’ => ‘id’,’dt’ => 16,
‘formatter’ => function( $d, $row ) {
return (‘
<button type=”button” class=”btn btn-link btn-sm” onclick=”window.open(\’actualizar_obra.php?id=’.$row[‘id’]. ‘\’, \’actualizarObra\’)”><span class=”fa fa-pencil”></button>
<button type=”button” class=”btn btn-link btn-sm” onclick=”window.open(\’ver_obra.php?id=’.$row[‘id’]. ‘\’, \’verObra\’)”><span class=”fa fa-eye”></button>
<button type=”button” class=”btn btn-link btn-sm” onclick=”window.open(\’borrar_obra.php?id=’.$row[‘id’]. ‘\’, \’borrarObra\’)”><span class=”fa fa-trash”></button>
‘);
}, ‘field’ => ‘Accion’
),
);
// Datos de conexión MySQL
$sql_details = array(
‘user’ => ‘root’,
‘pass’ => ”,
‘db’ => ‘db’,
‘host’ => ‘localhost’
);
require( ‘../vendor/datatables/ssp.class.php’ );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
Unfortunately, solution 1 is not working for me.