I'm not familiar with PHP so won't be much help there. You can probably use the SUM() function to sum the results of the query to return as the data for the table.
Stack Overflow is a good resource to search for and ask about general PHP and SQL questions.
@kthorngren Thanks Kevin! LOL, I think we need @allan or @colin reinforcement for PHP script. Anyways, could you please explain the idea of what I should do in server script, so I can ask clearly in stack overflow? Remember, I need when I load the page I just need to show the 2 date inputs. The table must show only when I choose between 2 dates, with only the salary. So, I don't need to show all records from the database in the page when on load.
Take a look at the SUM() link I gave. Expand upon its example:
SELECT SUM(column_name)
FROM table_name
WHERE condition;
The WHERE condition will be the date range and SUM(column_name) is the column you want to sum. You might be able to specify a string for the first column. Maybe something like this:
SELECT 'Total sales', SUM(final_total)
FROM invoice
WHERE invoice_date >='$minDate' AND invoice_date <= '$maxDate'";
You may or may not be able to get all the rows and columns you want in one sql statement. Depends on your specific data structure and what you are trying to get from the tables. You might need multiple queries which need combined for a single response back to Datatables.
I would start with an admin tool for your DB server to experiment with SQL queries before trying to get them to work with Datatables. Once you get the queries working then do the PHP and Datatables coding. Otherwise you will be fitting a bunch of different issues if you try making them all work at once.
To be honest Allan and Colin's priorities are with answering Datatables specific questions. Their time is limited - they may help with your PHP questions but instead of waiting on them, I would suggest using Stack Overflow as there are more people there to help with both.
Once you get the queries working we'll guide you to package it in a Datatables support format. Make sure to read through the Ajax docs for details. Probably the easiest for the simple table you have is array structure like this example but you can use objects if preferred like this example.
@kthorngren Thanks Kevin! After a lot of work, I finally made server script work successfully and I am getting the total appear in the table. But, I have an important thing to do. I need to remove this HTML code:
and add it to jQuery code using prepend(). So, I need to say if there is a valid date in the database show the table with the total, without using show()hide(). I don't want the table to appear empty when the page is loaded. Here is the full code:
There are lots of Stack Overflow threads, like this one, that provide techniques to dynamically add tables using jQuery. You should be able to find a solution that meets your needs. You will need the table inserted before initializing Datatables. And you will need thead inserted or you can use columns.title. See the HTML docs for the requirements.
@kthorngren Thanks Kevin! I successfully get the data and showed it in the table 100% finally. But I have a little issue. I need to have a messageTop in the print page, I did it but it does not show... Also, same thing for PDF, Here is the code:
@kthorngren The print button in the test case you post is not working at all. I am using ajax to get data from the datatabse so I can't post a test case... I'll post the whole html and javascript code:
When I click the print button I get this so it is working:
What exactly happens when you click the print button in my example?
It looks like you have messageTop commented out in lines 51 and 63.
// messageTop: 'test',
The problem you are reporting is not dependent on your data nor that you are using ajax. We just need you to show the problem you are having. You can use fake data. You can use one of the ajax sources here if you want ajax data for the fake data.
@kthorngren Yeah sorry I forgot to remove the comment from both lines when pasted here, but in my real project they are both not commented. When I click on print button nothing shows at all... For PDF, I am getting this error "Uncaught TypeError: Cannot set properties of undefined (setting 'widths')" and I can't download PDF file. But if I commented the messageTop, I can download the PDF but without the messageTop...
@kthorngren Ok so this test case that you sent http://live.datatables.net/levahacu/1/edit
worked when I click on "Run with JS" button, if I don't click on this button, the print button won't work. And yes the messageTop appeared in your test case but it didn't appear on mine...
worked when I click on "Run with JS" button, if I don't click on this button, the print button won't work.
Are you saying that the Print button doesn't appear until you click Run with JS? That is expected with JS Bin. You need to click the Run with JS to execute the page.
You are using single quotes for the full string and single quotes within the string, ie, ['name']). This should be giving you a syntax error. Use double quotes for the full string like this:
The messageTop: 'test', looks correct and looks to be in the correct spot. If this doesn't help then please post a test case showing the issue so we can help debug.
@kthorngren Ok, I know the issue now. I want if I click on print button, I don't want it to automatically open a new tab. So, I took this 5th comment (jvcunha's comment) that is available here https://datatables.net/forums/discussion/45126/buttons-print-chrome-problem
and pasted instead of the official code and it worked. But it ruined the messageTop. So, I tried to edit the modified code in line 160, I removed message: '', and I added messageTop: '*', and under it messageBottom:'*',. Now the messageTop appear but it says undefined... Definitely, there must be other modifications in the code, but I don't know how to do it... Is there a better solution to implement messageTop + not to automatically open in a new tab? Or could you please help me edit the code? Allan said he will implement in the source code, but he didn't until now...
Answers
I'm not familiar with PHP so won't be much help there. You can probably use the SUM() function to sum the results of the query to return as the data for the table.
Stack Overflow is a good resource to search for and ask about general PHP and SQL questions.
Kevin
@kthorngren Thanks Kevin! LOL, I think we need @allan or @colin reinforcement for PHP script. Anyways, could you please explain the idea of what I should do in server script, so I can ask clearly in stack overflow? Remember, I need when I load the page I just need to show the 2 date inputs. The table must show only when I choose between 2 dates, with only the salary. So, I don't need to show all records from the database in the page when on load.
Take a look at the SUM() link I gave. Expand upon its example:
The WHERE condition will be the date range and SUM(column_name) is the column you want to sum. You might be able to specify a string for the first column. Maybe something like this:
SQL date range WHERE condition example:
https://www.garron.me/en/bits/mysql-select-from-range-dates.html
You may or may not be able to get all the rows and columns you want in one sql statement. Depends on your specific data structure and what you are trying to get from the tables. You might need multiple queries which need combined for a single response back to Datatables.
I would start with an admin tool for your DB server to experiment with SQL queries before trying to get them to work with Datatables. Once you get the queries working then do the PHP and Datatables coding. Otherwise you will be fitting a bunch of different issues if you try making them all work at once.
To be honest Allan and Colin's priorities are with answering Datatables specific questions. Their time is limited - they may help with your PHP questions but instead of waiting on them, I would suggest using Stack Overflow as there are more people there to help with both.
Once you get the queries working we'll guide you to package it in a Datatables support format. Make sure to read through the Ajax docs for details. Probably the easiest for the simple table you have is array structure like this example but you can use objects if preferred like this example.
Kevin
@kthorngren Thanks Kevin! After a lot of work, I finally made server script work successfully and I am getting the total appear in the table. But, I have an important thing to do. I need to remove this HTML code:
and add it to jQuery code using
prepend()
. So, I need to say if there is a valid date in the database show the table with the total, without usingshow()
hide()
. I don't want the table to appear empty when the page is loaded. Here is the full code:There are lots of Stack Overflow threads, like this one, that provide techniques to dynamically add tables using jQuery. You should be able to find a solution that meets your needs. You will need the
table
inserted before initializing Datatables. And you will needthead
inserted or you can usecolumns.title
. See the HTML docs for the requirements.Kevin
@kthorngren Thanks Kevin! I successfully get the data and showed it in the table 100% finally. But I have a little issue. I need to have a messageTop in the print page, I did it but it does not show... Also, same thing for PDF, Here is the code:
Your code snippet seems to work here:
http://live.datatables.net/levahacu/1/edit
Please post a link to your page or a test case, update the above, showing the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
@kthorngren The print button in the test case you post is not working at all. I am using ajax to get data from the datatabse so I can't post a test case... I'll post the whole html and javascript code:
When I click the print button I get this so it is working:
What exactly happens when you click the print button in my example?
It looks like you have messageTop commented out in lines 51 and 63.
The problem you are reporting is not dependent on your data nor that you are using ajax. We just need you to show the problem you are having. You can use fake data. You can use one of the ajax sources here if you want ajax data for the fake data.
Kevin
@kthorngren Yeah sorry I forgot to remove the comment from both lines when pasted here, but in my real project they are both not commented. When I click on print button nothing shows at all... For PDF, I am getting this error "Uncaught TypeError: Cannot set properties of undefined (setting 'widths')" and I can't download PDF file. But if I commented the messageTop, I can download the PDF but without the messageTop...
@kthorngren Ok I found the issue for PDF! I fixed it by changing this
to this:
This fixed it for PDF, but for print button still...
To help troubleshoot please provide a test case showing the issue. My example with your print button code snippet works.
Kevin
@kthorngren Ok so this test case that you sent http://live.datatables.net/levahacu/1/edit
worked when I click on "Run with JS" button, if I don't click on this button, the print button won't work. And yes the messageTop appeared in your test case but it didn't appear on mine...
@kthorngren Here is the full code:
Are you saying that the Print button doesn't appear until you click Run with JS? That is expected with JS Bin. You need to click the Run with JS to execute the page.
One problem might be this line:
You are using single quotes for the full string and single quotes within the string, ie,
['name'])
. This should be giving you a syntax error. Use double quotes for the full string like this:The
messageTop: 'test',
looks correct and looks to be in the correct spot. If this doesn't help then please post a test case showing the issue so we can help debug.Kevin
@kthorngren Ok, I know the issue now. I want if I click on print button, I don't want it to automatically open a new tab. So, I took this 5th comment (jvcunha's comment) that is available here
https://datatables.net/forums/discussion/45126/buttons-print-chrome-problem
and pasted instead of the official code and it worked. But it ruined the messageTop. So, I tried to edit the modified code in line 160, I removed
message: '',
and I addedmessageTop: '*',
and under itmessageBottom:'*',
. Now the messageTop appear but it says undefined... Definitely, there must be other modifications in the code, but I don't know how to do it... Is there a better solution to implement messageTop + not to automatically open in a new tab? Or could you please help me edit the code? Allan said he will implement in the source code, but he didn't until now...