jQuery Datatables Export to Excel, CSV, PDF, Copy and Print using PHP

jQuery plugin that helps to convert your HTML table into a grid layout. Using jQuery plugin you can create Datatable into full functional data grid with many features like live search, pagination, sorting data and exporting table in various format. In this article, we show you how to export jQuery Datatable data into Excel, CSV, PDF, Copy and Print using PHP & MySQL
Now using the Ajax & PHP, you going to display the data from MySQL to Datagrid. If you like to export your data in various format like Excel, CSV, PDF, Copy and Print. For this, you need to work for separately, but with the help of jQuery Datatables plugin, we can export tables data into different file format whatever which you like. If you like to use this plugin, first you want to add some jQuery Datatables library to enable this feature.
By using this jQuery library, you going to enable export button features. Then you need to define HTML table for initialization jQuery Datatable plugin on the web page based on using the section ID “#user_info”. After you need to use the Datatable() method for initialized this plugin. Once you finished this process, you going to define Ajax which helps to send a request to fetch.php page and from there the data will fetch from MySQL table and that will display on the webpage. For the export button, you need to add “dom: lBfrtip” option which only leads to export in a various format which you like. The following code will help to integrate into your project.
Database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
-- -- Table structure for table `export_excel_more` -- CREATE TABLE `export_excel_more` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `gender` varchar(10) NOT NULL, `age` varchar(15) NOT NULL, `email` varchar(255) NOT NULL, `phone` varchar(15) NOT NULL, `organization` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `export_excel_more` -- INSERT INTO `export_excel_more` (`id`, `name`, `gender`, `age`, `email`, `phone`, `organization`) VALUES (21, 'Breanna', 'Female', '26', '[email protected]', '1-620-872-5823', 'A Felis Ullamcorper Foundation'), (23, 'Florence', 'Female', '25', '[email protected]', '791-1534', 'Libero Donec Consectetuer Inc.'), -- -- Indexes for dumped tables -- -- -- Indexes for table `export_excel_more` -- ALTER TABLE `export_excel_more` ADD PRIMARY KEY (`id`); COMMIT; |
conn.php
1 2 3 4 5 6 7 8 |
<?php $host = "localhost"; $db_user = "root"; $db_pass = ""; $dbname = "pdf_export"; $connect = new PDO("mysql:host=$host; dbname=$dbname", $db_user, $db_pass); ?> |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<html> <head> <title>jQuery Datatables Export to Excel, CSV, PDF, Copy and Print using PHP</title> <link rel="stylesheet" type="text/css" href="css/datatables.min.css" /> <link rel="stylesheet" href="css/bootstrap.min.css" /> <style> .dataTables_length { margin-bottom: 30px; } .dataTables_length select { border: 1px solid #e4e4e4; } .dt-buttons a { margin-left: 12px; font-size: 12px; padding: 6px; border: 1px solid #e4e4e4; background: #FFF; box-shadow: 0px 0px 14px 0px #ececec; } .dataTables_filter input { border: 1px solid #e4e4e4; } .table-striped tbody tr { line-height: 30px; } </style> </head> <body> <div class="container box"> <br/> <br/> <h3 align="center">jQuery Datatables Export to Excel, CSV, PDF, Copy and Print using PHP</h3> <br /> <div class="table-responsive"> <table id="user_info" class="table table-bordered table-striped"> <thead> <tr> <th>Name</th> <th>Gender</th> <th>Age</th> <th>Email</th> <th>Phone</th> <th>Organization</th> </tr> </thead> </table> </div> </div> <br /> <br /> </body> </html> <script src="js/jquery.min.js"></script> <script type="text/javascript" src="js/datatables.min.js"></script> <script src="js/bootstrap.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $('#user_info').DataTable({ "processing": true, "serverSide": true, "ajax": { url: "fetch.php", type: "POST" }, dom: 'lBfrtip', buttons: [ 'excel', 'csv', 'pdf', 'copy', 'print' ], "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ] }); }); </script> |
fetch.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php include('conn.php'); $column = array( 'name', 'gender', 'age', 'email', 'phone', 'organization' ); $query = "SELECT * FROM export_excel_more"; if (isset($_POST['search']['value'])) { $query .= ' WHERE name LIKE "%' . $_POST['search']['value'] . '%" OR gender LIKE "%' . $_POST['search']['value'] . '%" OR age LIKE "%' . $_POST['search']['value'] . '%" OR email LIKE "%' . $_POST['search']['value'] . '%" OR phone LIKE "%' . $_POST['search']['value'] . '%" OR organization LIKE "%' . $_POST['search']['value'] . '%" '; } if (isset($_POST['order'])) { $query .= 'ORDER BY ' . $column[$_POST['order']['0']['column']] . ' ' . $_POST['order']['0']['dir'] . ' '; } else { $query .= 'ORDER BY CustomerID DESC '; } $query1 = ''; if ($_POST['length'] != -1) { $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length']; } $statement = $connect->prepare($query); $statement->execute(); $number_filter_row = $statement->rowCount(); $statement = $connect->prepare($query . $query1); $statement->execute(); $result = $statement->fetchAll(); $data = array(); foreach ($result as $row) { $sub_array = array(); $sub_array[] = $row['name']; $sub_array[] = $row['gender']; $sub_array[] = $row['age']; $sub_array[] = $row['email']; $sub_array[] = $row['phone']; $sub_array[] = $row['organization']; $data[] = $sub_array; } function count_all_data($connect) { $query = "SELECT * FROM export_excel_more"; $statement = $connect->prepare($query); $statement->execute(); return $statement->rowCount(); } $output = array( 'draw' => intval($_POST['draw']), 'recordsTotal' => count_all_data($connect), 'recordsFiltered' => $number_filter_row, 'data' => $data ); echo json_encode($output); ?> |
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]