SSP.Class & WHERE Clause

SSP.Class & WHERE Clause

dlynchdlynch Posts: 10Questions: 4Answers: 1
edited August 2015 in Free community support

I am tring to figure out searching on the server sider processing. I think I am close I just can't seem to get it right

I am using the page at: https://datatables.net/manual/server-side#Example-data for assistance.

From what I gather I can filter the columns before I even display anything in the json.

Below is the code I have and I only want to return results where memberWrkGrpCode = BuildingB

Can anyone show an example on how I would do this?

// DB table to use
$table = 'members';
// Table's primary key
$primaryKey = 'memberID';
// Array of database columns which should be read and sent back to DataTables.
// The db parameter represents the column name in the database, while the dt
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array('db' => 'memberNameFirst', 'dt' => 0),
    array('db' => 'memberSeniority', 'dt' => 1),
    array('db' => 'memberWrkGrpCode', 'dt' => 2),
    array('db' => 'memberSeniorityLastUpdate', 'dt' => 3)
);

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Answer ✓

    You need to use the complex method of the SSP class to perform a conditional query. There isn't really any examples of that, since the SSP class is really just a demo, but you can just pass the string you want to use as the query into the whereAll parameter.

    Allan

  • dlynchdlynch Posts: 10Questions: 4Answers: 1
    edited August 2015

    Thanks! That worked. I am going to leave some of my code here if any one wants to use it for a base.

    <?php 
    // example URL usage page.php?where=memberWrkGrpCode&equals=BDMOHX112
    if ((isset($_GET['where'])) && (isset($_GET['equals']))) {
        $where = $_GET['where'];
        $equals = $_GET['equals'];
    
        $whereAll = $_GET['where'] . '=\'' . $_GET['equals'].'\'';
    } else {
        $whereAll = NULL;
    }
    
    // more code in the middle such as DB connections
    
    require( 'ssp.class.php' );
    echo json_encode(
            SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, $whereResult = null, $whereAll)
    ); ?>
    

    I did also make a small change to the complex SPP Class. I removed the NULL on the function line:

    ```php
    <?php
    static function complex($request, $conn, $table, $primaryKey, $columns, $whereResult = null, $whereAll) // Used to be $whereAll = null
    {
    //function content went in here
    }

    <?php > ``` ?>
  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Nice one - thanks for posting this. One thing - PHP might throw warnings if you have a required parameter after an optional one.

    Allan

  • dlynchdlynch Posts: 10Questions: 4Answers: 1

    I did not know that, huh, I switched them around.

This discussion has been closed.