Example of Wordpress datatables.net SQL Query using PHP Server side and Client side
Example of Wordpress datatables.net SQL Query using PHP Server side and Client side
Hi, I am struggling to find a working example of a datatable generation within Wordpress environment using MySQL query for server side or client based set-up.
I have successfully created multiple working PHP / MySQL tables/modals using the datatables editor, but I don't think my approach for building those tables where editing is not a requirement, is correct.
I can provide more detail and example of scripts I have set-up if required.
Sorry if above is a bit vague, but it maybe a starting point to point me in the right direction.
Many Thanks
Colin
This question has accepted answers - jump to:
Answers
Hi @cpshart ,
This blog post should help, it addresses how to install DataTables and Editor within WordPress.
Cheers,
Colin
Hi colin
Many Thanks for the link which I have read again, I have successfully installed the system on my server and created editable datatables within Wordpress using Woody PHP Snippets plugin.
I am however getting unusual results with one of my non editable tables as shown below (so I am not sure if I have done something fundamentally wrong in my approach).
I have removed all filtering, footer callbacks etc from the PHP snippet as they are not responsible for the error.
The error generated by the PHP Snippet displayed on the console is shown below
**Failed to load resource: the server responded with a status of 500 ()
**
If I run the SQL query through phpMyAdmin it works and generates the required output. If, however I simplify the quantity part of the SQL query in the PHP snippet from
SUM(IF(t.transaction_type="Buy",ANY_VALUE(t.quantity),ANY_VALUE(t.quantity) * -1)) AS quantity,
to
SUM(ANY_VALUE(t.quantity)) AS quantity,
Then it generates the datatable with no errors on the console, albeit the quantity is no longer correct !!
Failing PHP Snippet shown below
**Console ERROR Failed to load resource: the server responded with a status of 500 ()
**
Any help much appreciated.
Thanks Colin
You'll need to check the server's error log for this one. I don't immediately see a coding error in the above, but the error log will contain a message indicating what the problem is.
Allan
Hi allan
apologies for what is probably a silly question as I am relatively new to datatables, is this the file you are referring to public_html/Editor-PHP-1.8.1/controllers/error_log
and if so, do I need to turn error checking on, as it is not being updated when I refresh the webpage.
I am out for next couple of hours will check after that, thanks
Thanks Colin
Hi, fixed the problem
PHP Snippet SQL Query was enclosed by double quotes, (lines 37 to 57), and line 44 also has double quotes around "Buy"
```
$rows = $wpdb->get_results("
SELECT
etc...
SUM(IF(t.transaction_type="Buy",ANY_VALUE(t.quantity),ANY_VALUE(t.quantity) * -1)) AS quantity,
etc ending
GROUP BY
d.pdate, t.portfolio_id, s.symbol, s.name
");
```
the solution was to change "Buy" to 'Buy' single quotes.
Thanks for help.
Colin
Hi @cpshart ,
Excellent - glad that blog proved useful!
Cheers,
Colin