Database Driven DataTable With No Includes

Database Driven DataTable With No Includes

dmm001dmm001 Posts: 8Questions: 5Answers: 0

Hi,

So, I have gone through some of the examples and it seems for a database driven datatable the recommended approach is to include DataTables.php. I would prefer to build the plumbing from scratch for various reasons, but when I have tried it seems that there are pieces that I am missing. Below is a breakdown of what I understand to be required. Maybe someone could clarify/fill in the gaps and/or answer the questions below?

Required Components
1. Build a page, i.e. index.html with the table tags and the thead tags.
2. Include any required/desired css styles and javascript libraries to index.html.
3. Add JavaScript to index.html that creates a new TableEditor instance, points to an ajax controller file which pulls the data from the db, and initiates the Editor instance with the fields that will be loaded from the db.
4. Create the PHP file/ajax controller that retrieves the data from the database.

Questions
1. In bootstrap.php there is a class loader (spl_autoload_register(function ($class)). Where is this being called from and is it necessary? Could anyone share a more bare bones example of loading a class file? The one in the bootstrap.php is a little sophisticated for my PHP level.
2. On a less important note, there is a named constant for DataTables and a Namespace for DataTables (define('DATATABLES', true); and namespace DataTables;) in DataTables.php and Bootstrap.php. It would be helpful to better understand how these are used? I know this is a really about PHP knowledge, but I would like to put together a less sophisticated example to work from including only what is required as I get more familiar with PHP, Ajax, etc.

I know that's a lot of questions. :) Thanks In Advance for any help.

Answers

  • allanallan Posts: 64,608Questions: 1Answers: 10,683 Site admin

    Your "Required Components" are correct (assuming you want to Ajax load from a PHP server - which you obviously do, but just highlighting this for anyone else reading it!).

    If you want to build it yourself and just load data - all you need to do is have the server respond with JSON that contains the data to display. See the manual here. DataTables itself doesn't "care" about what the server is, what it is running, etc, just that it gets valid JSON in response. So in the case of PHP, you can make a trivial PDO call to the database, do a ->fetchAll() on the result and json_encode() the array. Job done (add ajax.dataSrc as an empty string if you return the data as a top level array, not under a data property).

    The server-side libraries I publish are primarily to make life easier with Editor. Also server-side processing to a degree. If you don't need any of that, you'd be better just getting the data and firing it back yourself.

    1. In bootstrap.php there is a class loader (spl_autoload_register(function ($class)). Where is this being called from and is it necessary? Co

    If you want to use the DataTables PHP libraries, then yes it is necessary. It auto loads the files based on the class names, so you don't need to include every file yourself (I suppose you could do it manually rather than using the auto loader, but why? It is psr-4 structure compatible).

    1. On a less important note, there is a named constant for DataTables and a Namespace for DataTables (define('DATATABLES', true); and namespace DataTables;) in DataTables.php and Bootstrap.php.

    The namespace is just to help prevent naming collisions. See the PHP manual for details on that.

    The define variable is used to try and enforce a single entry point - e.g. see here. An end user shouldn't be able to make a GET request for the config.php file. Its a touch of security, if not much.

    Allan

Sign In or Register to comment.