Question: How to achieve this complex search using jQuery datatable plugin?

Question

How to achieve this complex search using jQuery datatable plugin?

Answers 1
Added at 2017-01-01 00:01
Tags
Question

I am trying to make a searchable interface and my backend includes three MySQL database tables:

tbl_country

mysql> select * from tbl_country;
+--------------+---------------+
| country_code | country       |
+--------------+---------------+
| AFG          | AFGHANISTAN   |
| AUS          | AUSTRALIA     |
| CAN          | CANADA        |
| GBR          | GREAT BRITAIN |
| IND          | INDIA         |
| USA          | USA           |
+--------------+---------------+
6 rows in set (0.00 sec)

tbl_state

mysql> select * from tbl_state;
+----------+------------------+--------------+
| state_id | state            | country_code |
+----------+------------------+--------------+
|        1 | Maharashtra      | IND          |
|        2 | Delhi            | IND          |
|        3 | West Bengali     | IND          |
............
|       51 | Queensland       | AUS          |
+----------+------------------+--------------+
33 rows in set (0.00 sec)

tbl_city

mysql> select * from tbl_city;
+---------+----------+--------------------+
| city_id | state_id | city               |
+---------+----------+--------------------+
|       1 |        1 | Mumbai (Bombay)    |
|       2 |        1 | Nagpur             |
.......
|      84 |       37 | Edinburgh          |
|     122 |       44 | Cardiff            |
|     127 |       46 | Melbourne          |
|     130 |       48 | Perth              |
|     131 |       49 | Adelaide           |
|     132 |       50 | Canberra           |
|     133 |       51 | Brisbane           |
|     134 |       51 | Gold Coast         |
+---------+----------+--------------------+
54 rows in set (0.00 sec)

I have my MySQL Database schema and values as shown here in SQL Fiddle

My search needs to get a single interface from where I can search all Country based on any given city or state details.

I am trying to use this query which correctly gives the output that I want to fetch:

SELECT 
-- tbl_country
c.country_code, c.country,

-- tbl_state
CAST(GROUP_CONCAT(s.state SEPARATOR ',') AS CHAR) as state,
-- tbl_city
CAST(GROUP_CONCAT(ct.city SEPARATOR ',') AS CHAR) as city

-- tbl_papers
FROM tbl_country c
LEFT JOIN tbl_state s ON s.country_code = c.country_code
LEFT JOIN tbl_city ct ON ct.state_id = s.state_id

GROUP BY (c.country_code);

I am using Datatable plugin which is a quite powerful and easy to use but whenever I tried to use search filter it will now work as the datatable's Server Side PHP class tries to create a dynamic query with where condition based on the bind columns.

The query bind the datatable as shown here in first row as no search is done : Correct Datatable Binding

The dynamic query is like this:

SELECT    sql_calc_found_rows `c`.`country_code`,
          `c`.`country`,
          group_concat(`s`.`state` separator "," ) AS state,
          group_concat(`ct`.`city` separator ",")  AS city
FROM      `tbl_country`                            AS `c`
LEFT JOIN `tbl_state`                              AS `s`
ON        (
                    `s`.`country_code` = `c`.`country_code`)
LEFT JOIN `tbl_city` AS`ct`
ON        `ct`.`state_id` = `s`.`state_id`
WHERE     (
                    `c`.`country_code` LIKE :binding_0
          OR        `c`.`country` LIKE :binding_1
          OR        group_concat(`s`.`state` separator ",") LIKE :binding_2
          OR        group_concat(`ct`.`city` separator ",") LIKE :binding_3)
GROUP BY  `c`.`country_code`
ORDER BY  `c`.`country_code` ASC limit 0,
          10

Which obviously gives error like shown here: Error due to adding where condition with GROUP_CONCAT keyword

Is there anyway, I can filter/ search based on given city and state and country details on above mentioned scenario using PHP and datatable plugin. May be I need to change my query structure!

Answers to

How to achieve this complex search using jQuery datatable plugin?

nr: #1 dodano: 2017-01-01 03:01

I assume you're using ssp.class.php to process your data on the server-side.

Class ssp.class.php doesn't support complex queries containing JOINs and sub-queries, but there is a workaround. The trick is to use sub-query as shown below in $table definition.

$table = <<<EOT
 (
    SELECT 
    c.country_code, 
    c.country,
    CAST(GROUP_CONCAT(s.state SEPARATOR ',') AS CHAR) as state,
    CAST(GROUP_CONCAT(ct.city SEPARATOR ',') AS CHAR) as city
    FROM tbl_country c
    LEFT JOIN tbl_state s ON s.country_code = c.country_code
    LEFT JOIN tbl_city ct ON ct.state_id = s.state_id
    GROUP BY (c.country_code)
 ) temp
EOT;

$primaryKey = 'id';

$columns = array(
   array( 'db' => 'country_code', 'dt' => 0 ),
   array( 'db' => 'country', 'dt' => 1 ),
   array( 'db' => 'state', 'dt' => 2 ),
   array( 'db' => 'city', 'dt' => 3 )
);

$sql_details = array(
   'user' => '',
   'pass' => '',
   'db'   => '',
   'host' => ''
);

require( 'ssp.class.php' );
echo json_encode(
   SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

You also need to edit ssp.class.php and replace all instances of FROM `$table` with FROM $table to remove backticks.

There is also github.com/emran/ssp repository that contains enhanced ssp.class.php supporting JOINs.

Source Show
◀ Wstecz