{"id":3462,"date":"2018-06-12T16:07:06","date_gmt":"2018-06-12T16:07:06","guid":{"rendered":"https:\/\/max-drake.cc\/?p=3462"},"modified":"2018-06-18T11:00:43","modified_gmt":"2018-06-18T11:00:43","slug":"datatables-for-dynamic-database-queries-for-tables-on-web-pages-export-tools","status":"publish","type":"post","link":"https:\/\/max-drake.cc\/?p=3462","title":{"rendered":"DataTables for dynamic database queries for tables on web pages &#038; export tools"},"content":{"rendered":"<p>I have been interested in displaying tabulated data on a web page from a Database, exploring what is out there to use.<\/p>\n<p>WordPress has a couple of Free Table add-in&#8217;s but they are usually only available for uploading static data to your website (or from Excel\/CSV).<\/p>\n<p>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<a href=\"https:\/\/apriori.ml\/index.php\/property\/newa506\/\" target=\"_blank\" rel=\"noopener\"><strong> property demo website<\/strong><\/a>. The plugin is<\/p>\n<p class=\"plugin-title\"><strong><a href=\"https:\/\/wordpress.org\/plugins\/elisqlreports\/\">EZ SQL Reports Shortcode Widget and DB Backup<\/a><\/strong>. 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.<\/p>\n<p>I had come across<a href=\"https:\/\/datatables.net\/\" target=\"_blank\" rel=\"noopener\"><strong> dataTables<\/strong><\/a> before but had not used them. I decided to give them a try.<\/p>\n<p>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 <a href=\"https:\/\/apriori.ml\/index.php\/property\/mir\/\" target=\"_blank\" rel=\"noopener\"><strong>multiple tables in iFrames<\/strong><\/a>. ( The reason they are in iFrames is I haven&#8217;t tried to organise the code with WordPress setup at this time)<\/p>\n<p>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 <a href=\"http:\/\/ekar.dx.am\/data\/datatable\/datatables.php\" target=\"_blank\" rel=\"noopener\"><strong>HERE<\/strong><\/a>, it is still a work in progress.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-3464 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp01-1024x261.jpg\" alt=\"\" width=\"1446\" height=\"369\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp01-1024x261.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp01-300x76.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp01-768x196.jpg 768w\" data-sizes=\"(max-width: 1446px) 100vw, 1446px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1446px; --smush-placeholder-aspect-ratio: 1446\/369;\" \/><\/p>\n<p>Here is an example I am still working on. Mainly exploring the formatting issues currently.<\/p>\n<p>Things that I want the table to do:<\/p>\n<ol>\n<li>Update from database to show relevant information (query to database)<\/li>\n<li>Condense columns so i can get lots of LEGIBLE columns into the web page<\/li>\n<li>Basic ordering ( click on column header and it will do asc and dec ordering on a specific column (use search for more refined filtering))<\/li>\n<li>Have an idea of the data (bottom left number of rows shown &amp; bottom right Pagination)<\/li>\n<li>Be able to filter the data (using Search box in top right corner)<\/li>\n<li>After filtering data be able to export it (Copy, CSV, Excel, PDF &amp; Print buttons on Top Left)<\/li>\n<\/ol>\n<p>The dataTables calls jquery.js, and a few .css &amp; .js files. I had the experience when working with the Panoramas where I was calling files from another web page via CDN codes (<em>eg\/\/cdn.datatables.net\/1.10.16\/css\/jquery.dataTables.min.css<\/em>) that I had left them live on my webpage and they were changed\/updated and my webpages didn&#8217;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.<\/p>\n<p>I am finding I am doing a bit of fiddling with the filters on the tables.&nbsp; 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.<\/p>\n<p>My initial test was using a :<\/p>\n<p>&#8220;<strong>Select * From housing<\/strong>&#8221;<\/p>\n<p>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:<\/p>\n<p>$query=mysqli_query($conn, &#8220;<strong>SELECT * FROM housing WHERE `Property Code (uk)` like&#8221;A-NEWA506%&#8221; and `Type Code`= &#8220;CEI&#8221; ORDER BY `Property Code (uk)`<\/strong>&#8220;);<\/p>\n<p>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.<\/p>\n<h3>Issues<\/h3>\n<p>The issue with the Table&nbsp; 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.<\/p>\n<p>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 &#8216;0&#8217; to &#8217;37&#8217; so I can count which cell in the array that I want that is returned in the call to the database.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-3477 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp03-1024x275.jpg\" alt=\"\" width=\"1613\" height=\"433\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp03-1024x275.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp03-300x80.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp03-768x206.jpg 768w\" data-sizes=\"(max-width: 1613px) 100vw, 1613px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1613px; --smush-placeholder-aspect-ratio: 1613\/433;\" \/><\/p>\n<p><img decoding=\"async\" class=\"wp-image-3476 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp04-794x1024.jpg\" alt=\"\" width=\"833\" height=\"1074\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp04-794x1024.jpg 794w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp04-233x300.jpg 233w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp04-768x991.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp04.jpg 1051w\" data-sizes=\"(max-width: 833px) 100vw, 833px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 833px; --smush-placeholder-aspect-ratio: 833\/1074;\" \/><\/p>\n<p>The result below shows:<\/p>\n<ol>\n<li>The Sum column in wrong, not column 37 of the array,<\/li>\n<li>also the Cond columns are not showing at all. I am not sure why this is. It needs a bit more study.<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"wp-image-3478 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp05-1024x248.jpg\" alt=\"\" width=\"1430\" height=\"346\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp05-1024x248.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp05-300x73.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp05-768x186.jpg 768w\" data-sizes=\"(max-width: 1430px) 100vw, 1430px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1430px; --smush-placeholder-aspect-ratio: 1430\/346;\" \/>&nbsp;I have&nbsp; 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.<\/p>\n<p>Changing the array number&nbsp; from<strong> &lt;th&gt;&#8221;.$result[37].&#8221;&lt;\/th&gt;<\/strong> to<strong> &lt;th&gt;&#8221;.$result[&#8216;Value&#8217;].&#8221;&lt;\/th&gt;<\/strong> (the column header label works for that item, but doesn&#8217;t seem to work for the Cond columns after)<\/p>\n<p>There may be a maximum length of columns that it can read but the only&nbsp; 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&#8217;t want to display anything after column 7\/8.<\/p>\n<h3>Debugging<\/h3>\n<p>Datatables have a debugging process. Go to&nbsp; <strong>http:\/\/debug.datatables.net\/<\/strong> to get the javascript snippet.<br \/>\n<img decoding=\"async\" class=\"wp-image-3484 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp06-1024x658.jpg\" alt=\"\" width=\"1504\" height=\"967\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp06-1024x658.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp06-300x193.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp06-768x493.jpg 768w\" data-sizes=\"(max-width: 1504px) 100vw, 1504px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1504px; --smush-placeholder-aspect-ratio: 1504\/967;\" \/><\/p>\n<p><img decoding=\"async\" class=\"wp-image-3483 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp07-1024x666.jpg\" alt=\"\" width=\"1702\" height=\"1107\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp07-1024x666.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp07-300x195.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp07-768x500.jpg 768w\" data-sizes=\"(max-width: 1702px) 100vw, 1702px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1702px; --smush-placeholder-aspect-ratio: 1702\/1107;\" \/><\/p>\n<p><img decoding=\"async\" class=\"wp-image-3482 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp08-1024x557.jpg\" alt=\"\" width=\"1453\" height=\"791\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp08-1024x557.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp08-300x163.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp08-768x417.jpg 768w\" data-sizes=\"(max-width: 1453px) 100vw, 1453px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1453px; --smush-placeholder-aspect-ratio: 1453\/791;\" \/><\/p>\n<p><img decoding=\"async\" class=\"wp-image-3481 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp09-1024x710.jpg\" alt=\"\" width=\"1510\" height=\"1047\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp09-1024x710.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp09-300x208.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp09-768x532.jpg 768w\" data-sizes=\"(max-width: 1510px) 100vw, 1510px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1510px; --smush-placeholder-aspect-ratio: 1510\/1047;\" \/>&nbsp;&nbsp;&nbsp;<\/p>\n<p>As it was, this debugging process did not help me regarding column lengths but it is useful tool.<\/p>\n<h2>Videos and helpful information.<\/h2>\n<p>I followed this set of videos for setting up DB link and for buttons:<\/p>\n<p><iframe title=\"Jquery Datatable Tutorial\" width=\"678\" height=\"381\" data-src=\"https:\/\/www.youtube.com\/embed\/videoseries?list=PLh89M5lS1CIBDQE5NkIBRBfbTfXBf6xv3\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe><\/p>\n<p>I think some of the targeting of specific controls on specific columns in this video are useful (about 14 min in)<\/p>\n<p><iframe title=\"jQuery DataTable Tutorial With Bootstrap Design For Beginners\" width=\"678\" height=\"381\" data-src=\"https:\/\/www.youtube.com\/embed\/umWN2UFGeDU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe><\/p>\n<p>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.<\/p>\n<p><iframe title=\"jQuery Data Tables plugin in 7 minutes\" width=\"678\" height=\"509\" data-src=\"https:\/\/www.youtube.com\/embed\/aL2YAfQ7BSU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe><\/p>\n<p>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)<\/p>\n<p><iframe title=\"Styling DataTables\" width=\"678\" height=\"381\" data-src=\"https:\/\/www.youtube.com\/embed\/rYiQFuBf8O8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe><\/p>\n<h2>End Comments<\/h2>\n<p>My next challenge is to see how many columns I can get on the tables.<\/p>\n<p>I was not looking at this as a CRUD process (Create, Read, Update &amp; Delete) but rather just a way&nbsp; 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.<\/p>\n<p>The Database would need to be updated so that it remains current (or go off and read a database that is current).<\/p>\n<p>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.<\/p>\n<p id=\"5248\" class=\"graf graf--h3 graf--leading graf--title\">I came across this article: Series: <a href=\"https:\/\/medium.com\/@deepika.gunda\/series-datatables-with-nodejs-express-and-mongodb-part1-get-a-database-to-work-with-7ce78848023e\" target=\"_blank\" rel=\"noopener\">Datatables with Nodejs&nbsp;,Express and Mongodb. Part1: Get a database to work&nbsp;with<\/a>. Using MongoDB with datatables might be an interesting exercise that I may try in the future.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3471,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,36,12,13,29],"tags":[],"class_list":["post-3462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-analysis","category-databases","category-visualisation","category-visualise-building-condition","category-web"],"featured_image_src":"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp02.jpg","featured_image_src_square":"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/06\/imppp02.jpg","author_info":{"display_name":"Max Drake","author_link":"https:\/\/max-drake.cc\/?author=1"},"_links":{"self":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/3462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3462"}],"version-history":[{"count":0,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/3462\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/media\/3471"}],"wp:attachment":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}