DataTables for dynamic database queries for tables on web pages & export tools

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

I have been interested in displaying tabulated data on a web page from a Database, exploring what is out there to use.

WordPress has a couple of Free Table add-in’s but they are usually only available for uploading static data to your website (or from Excel/CSV).

There is one WordPress Add-in that read from a database but it was quite hard to format. I used it in this example on my property demo website. The plugin is

EZ SQL Reports Shortcode Widget and DB Backup. As you can see from the above the formatting is not very elegant. There was also only a certain number that you could use on one website before they became unmanageable. There are other add-ins but you pay for them.

I had come across dataTables before but had not used them. I decided to give them a try.

My Contabo VPS has been giving me grief in not being able to load these tables properly, for some reason they block the data going through from the PHP part of the code. I found that the table was corrupt. When I tested on another table it worked fine. I have an example on my Property Site with multiple tables in iFrames. ( The reason they are in iFrames is I haven’t tried to organise the code with WordPress setup at this time)

In cases like these I use an alternative site, the AwardSpace web hosting service, and have got the tables up and running on that. The link to the table is HERE, it is still a work in progress.

Here is an example I am still working on. Mainly exploring the formatting issues currently.

Things that I want the table to do:

  1. Update from database to show relevant information (query to database)
  2. Condense columns so i can get lots of LEGIBLE columns into the web page
  3. Basic ordering ( click on column header and it will do asc and dec ordering on a specific column (use search for more refined filtering))
  4. Have an idea of the data (bottom left number of rows shown & bottom right Pagination)
  5. Be able to filter the data (using Search box in top right corner)
  6. After filtering data be able to export it (Copy, CSV, Excel, PDF & Print buttons on Top Left)

The dataTables calls jquery.js, and a few .css & .js files. I had the experience when working with the Panoramas where I was calling files from another web page via CDN codes (eg//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css) that I had left them live on my webpage and they were changed/updated and my webpages didn’t work anymore so now I create a sub-directory and put the minimised files in there to be referenced. That ensures that they will continue to operate even if there are updates.

I am finding I am doing a bit of fiddling with the filters on the tables.  There ae a lot of styling tools that can be used to be able to style the data on your page, including putting multiple tables on the same page.

My initial test was using a :

Select * From housing

which basically went through the whole of the Database Table. This can be very slow, so I am now using more targeted SQL queries to speed up the search, such as:

$query=mysqli_query($conn, “SELECT * FROM housing WHERE `Property Code (uk)` like”A-NEWA506%” and `Type Code`= “CEI” ORDER BY `Property Code (uk)`“);

So if I want to display lots of information, maybe having low search overhead by making the SQL query more accurate will speed up the table load time.

Issues

The issue with the Table  on the Contabo site was a bit of a pain, but after sorting that out (with a bit of hair pulling) I was back on track.

My current issue is length of table and what its reading from the database. I uploaded the following CSV for it to read. I have numbered the columns from ‘0’ to ’37’ so I can count which cell in the array that I want that is returned in the call to the database.

The result below shows:

  1. The Sum column in wrong, not column 37 of the array,
  2. also the Cond columns are not showing at all. I am not sure why this is. It needs a bit more study.

 I have  run out of patience on this at the moment as I have spent too much time on it. I may visit it later to try and debug that part of it.I have altered column numbers, made the first few 22, 26, 27 and they worked fine, so there may be a limit on reading column headers, but this does not explain why Column 37 is not read, unless there is an upper limit on number of columns being read.

Changing the array number  from <th>”.$result[37].”</th> to <th>”.$result[‘Value’].”</th> (the column header label works for that item, but doesn’t seem to work for the Cond columns after)

There may be a maximum length of columns that it can read but the only  preliminary searching I have found is regarding a previous version that has a limit of 8 columns. As it calls the last column (values) so it must have, in the PHP $result the data from Years 2016 onwards, it just doesn’t want to display anything after column 7/8.

Debugging

Datatables have a debugging process. Go to  http://debug.datatables.net/ to get the javascript snippet.

   

As it was, this debugging process did not help me regarding column lengths but it is useful tool.

Videos and helpful information.

I followed this set of videos for setting up DB link and for buttons:

I think some of the targeting of specific controls on specific columns in this video are useful (about 14 min in)

And the one below has some smart coding, about 4.28 in to the video on Show/Hide columns which would be really useful for tables with a large amount of columns to display.

There is this one on formatting but he is directly editing the .css file, whereas the video before he added a app.js file to do his customisation. Maybe you have to go directly into the .CSS file to control the customisation but I like leaving the original files as is where possible (mainly because I have forgotten what I did in the main file)

End Comments

My next challenge is to see how many columns I can get on the tables.

I was not looking at this as a CRUD process (Create, Read, Update & Delete) but rather just a way  to read the database and display the information in a useful manner on a website. Maybe the CRUD process needs to be explored a bit more.

The Database would need to be updated so that it remains current (or go off and read a database that is current).

I am pleased with the formatting control on the tables, they seem much more versatile than the plugins for WordPress, and the DataTables are free too.

I came across this article: Series: Datatables with Nodejs ,Express and Mongodb. Part1: Get a database to work with. Using MongoDB with datatables might be an interesting exercise that I may try in the future.

 

Add a Comment

Your email address will not be published. Required fields are marked *