I started using DataTables Table plug-in for jQuery and got some problems. I am using example code from here.
I have MySQL table witch looks like that:
id | name | father_id
father_id
is id
value in same table only in different row. So if I want to know father name i have to search in same table WHERE id = father_id
. But what DataTable does it just show the contents of MySQL table as it is.
In my DataTable i want to show data like that:
id | name | father_name | father_id
So when DataTable takes data from MySQL table, but before it creates table I want to change column value which at that time is value of father_id
in the same row in MySQL. I want too add father_name
by searching for it with particular father_id
.
As PaulF pointed out, you need to use
JOIN
or sub-query to retrieve father's name from the same table.I assume you're using
ssp.class.php
to process your data on the server-side based on the example you've mentioned.Class
ssp.class.php
doesn't support joins and sub-queries, but there is a workaround. The trick is to use sub-query as shown below in$table
definition. Replacetable
with your actual table name in the sub-query.You also need to edit
ssp.class.php
and replace all instances ofFROM `$table`
withFROM $table
to remove backticks.Make sure all column names are unique otherwise use
AS
to assign an alias.NOTES
There is also github.com/emran/ssp repository that contains enhanced
ssp.class.php
supporting JOINs.LINKS
See jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php for more information.
Join the table with itself - you will need to use aliases
Recently I found myself using DataTables and requiring a more complex JOIN and WHERE clause that the original ssp.class.php did not support. So I modified the original file and changed the API a tiny bit to provide a table that gave me the flexibility I needed. I combined the functionality of the "SSP::simple" and "SSP::complex" into a single function named "SSP::process".
Due to the length of the script, I put it on pastebin.com here: ssp.class.php
And a quick example of how I use it:
The 'where' and 'whereResult' (SEE 'SSP::complex' for Details) clauses of the options array may also have an 'alias' to refer to a column in a joined table.
Example SQL Query passed to the server:
I took the structured array route because this enabled me to build the queries while maintaining the rigidity of queries with backticks and bound statement parameters. I'm putting up this post in hopes that others will find it as useful as I have.