Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export to Excel and PDF in PHP, Causing code redundancy

I need to create export to excel and export to pdf option for some of my mysql queries and provide it to the user.

But also, the user needs to view the query results on my website too.

So, in this case, code redundancy occurs , as I have to create three php code files:

One to let the user view the query results directly on my website , and,

Other one to let the user export the same query results to Excel ,and ,

last one to let the user export the same query results to PDF

All the three files are having the same code, only the difference being the way of sending the data to excel or to pdf.

How do I eradicate this code duplicacy , Is there some way using which I could have only one file and use it for all purposes , to view , to export to pdf and to export to excel too.

like image 255
sqlchild Avatar asked Dec 10 '22 03:12

sqlchild


1 Answers

"Is there some way using which I could have only one file and use it for all purposes , to view , to export to pdf and to export to excel too." I think answer is YES.

You need to put a select box to give user option to view that particular code in a particular way. See below example code for that.

<select name="viewoption" id="viewoption">
<option value="view">Page view</option>
<option value="xls">Export in Excel</option>
<option value="pdf">Export in PDF</option>
</select>

and then on action page you just need to check out the value of $_POST['viewoption'] and put if condition as per the requirements.

This may be helpful to you.let me know in case of any query.

Thanks.

like image 83
Chandresh M Avatar answered Jan 04 '23 23:01

Chandresh M