简体   繁体   中英

Having problem trying to export data pulled from database(PhpMyAdmin) from a collapsible using checkboxes shown on WORDPRESS

I am new to PHP and trying to work on a wordpress page.

I'm trying to build a collapsible which pulls data from a table from an online database, I'm currently writing the PHP code, along with some javascript on a wordpress plugin called snippets .

No matter how I approach it I can't seem to find a solution, can someone please help me image the top checkbox with no collapsible is check all button.

My goal is to be able to export the information onto an excel based on the checkbox row by row. Thank you. Below is the code regarding this problem.

Also, since it's on wordpress I do not know if it's possible to link the $.ajax to another PHP page like I've seen online so I try to put everything in one snippet(one PHP file I suppose). When I click export now nothing appears the alert box comes up just fine but it won't export an excel file

 <button type="button" name="export" id="export" class="btn btn-success">Export </button> </form> </body> <p> <br> </p> <input type="checkbox" class="checkAll"> <p> <br> </p> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ $('.checkAll').on('click', function() { $(this).closest('div').find(':checkbox').prop('checked',this.checked); }); }); </script> <script> $(document).ready(function(){ $('#export').click(function(){ if(confirm("Are you sure you want to export this?")) { var id = []; $(':checkbox:checked').each(function(i){ id[i] = $(this).val(); }); if(id.length === 0) { alert("Please Select at least one checkbox"); } else { $.ajax({ method:'POST', data:{id:id}, success:function() { for(var i=0; i<id.length; i++) { $('tr#'+id[i]+'').css('background-color', '#ccc'); $('tr#'+id[i]+'').fadeOut('slow'); } } }); } } else { return false; } }); }); </script> <?php $output = ''; if(isset($_POST["id"])) { foreach($_POST as $id) { $que = "SELECT * FROM Product limit 20"; $res = mysqli_query($con, $que); if(mysqli_num_rows($res) > 0) { $output.= ' <table class="table" bordered="1"> <tr> <th>id</th> <th>Name</th> <th>Short Description</th> <th>Full Description</th> <th>SKU</th> <th>Order Minimun Quantity</th> <th>Order Maximum Quantity</th> <th>Categories</th> <th>Manufacturers</th> <th>PREMIX</th> <th>Medicine Name</th> </tr> '; while($rows = mysqli_fetch_array($res)) { $output.= ' <tr> <td>'.$rows["id"].'</td> <td>'.$rows["Name"].'</td> <td>'.$rows["Short Description"].'</td> <td>'.$rows["Full Description"].'</td> <td>'.$rows["SKU"].'</td> <td>'.$rows["Order Minimum Quantity"].'</td> <td>'.$rows["Order Maximum Quantity"].'</td> <td>'.$rows["Categories"].'</td> <td>'.$rows["Manufacturers"].'</td> <td>'.$rows["PREMIX"].'</td> <td>'.$rows["Medicine Name"].'</td> </tr> '; } $output.= '</table>'; header('Content-Type: application/xls'); header('Content-Disposition: attachment; filename=download.xls'); echo $output; } } }

 <?php function phpexcel() { if ( defined('CBXPHPSPREADSHEET_PLUGIN_NAME') && file_exists( CBXPHPSPREADSHEET_ROOT_PATH. 'lib/vendor/autoload.php' ) ) { require_once( CBXPHPSPREADSHEET_ROOT_PATH. 'lib/vendor/autoload.php' ); $file_name = 'Product Data_'.$date.'.xlsx'; $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $product = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Product'); $spreadsheet->addSheet($product, 0); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Id'); $sheet->setCellValue('B1', 'Name'); $sheet->setCellValue('C1', 'Short Description'); $sheet->setCellValue('D1', 'Full Description'); $sheet->setCellValue('E1', 'SKU'); $sheet->setCellValue('F1', 'Order Minimum Quantity'); $sheet->setCellValue('G1', 'Order Maximum Quantity'); $sheet->setCellValue('H1', 'Categories'); $sheet->setCellValue('I1', 'Manufacturers'); $sheet->setCellValue('J1', 'PREMIX'); $sheet->setCellValue('K1', 'Medicine Name'); $query2 = mysql_query($con,"SELECT * FROM Product limit 20"); $row = 12; while($data= mysql_fetch_object($query2)){ $sheet->setCellValue('A'.$row, $data->Id); $sheet->setCellValue('B'.$row, $data->Name); $sheet->setCellValue('C'.$row, $data->ShortDescription); $sheet->setCellValue('D'.$row, $data->FullDescription); $sheet->setCellValue('E'.$row, $data->SKU); $sheet->setCellValue('F'.$row, $data->OrderManimumQuantity); $sheet->setCellValue('G'.$row, $data->OrderMaximumQuantity); $sheet->setCellValue('H'.$row, $data->Categories); $sheet->setCellValue('I'.$row, $data->Manufacturers); $sheet->setCellValue('J'.$row, $data->PREMIX); $sheet->setCellValue('K'.$row, $data->MedicineName); $row++; } $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"$file_name\";"); header("Content-Transfer-Encoding: binary"); $writer->save('php://output'); exit; } }?> <button type="button" name="test" id="test" class="btn btn-success" onclick="phpexcel()">test </button>

I tried using @Nolicasara's suggestion and this is what I am working with. I tried using creating a button to test out the function but still nothing appears. The snippet shows the code for the phpspreadsheet and the code for the button.

You can use this plugin https://github.com/codeboxrcodehub/cbxphpspreadsheet as library then you do own writable data to excel

function phpexcel() {
  if ( defined('CBXPHPSPREADSHEET_PLUGIN_NAME') && file_exists( CBXPHPSPREADSHEET_ROOT_PATH . 'lib/vendor/autoload.php' ) ) {
    //Include PHPExcel
    require_once( CBXPHPSPREADSHEET_ROOT_PATH . 'lib/vendor/autoload.php' );
    $date = current_time('dmyyHis');
    $file_name = 'HangryAppSalesNPS_'.$date.'.xlsx';
    //now take instance
    //  $objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
    $pbisale = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Sales');
    $nps = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'NPS');

    // Attach the "My Data" worksheet as the first worksheet in the Spreadsheet object
    $spreadsheet->addSheet($pbisale, 0);
    
    $sheet = $spreadsheet->getSheet(0);;
    $sheet->setCellValue('A1', 'Date');
    $sheet->setCellValue('B1', 'Location ID');
    $sheet->setCellValue('C1', 'Sales (w/ VAT)');
    $sheet->setCellValue('D1', 'Dockets');
    $data =  get_report_data();
    $row = 2;
    foreach($data as $value){
      $sheet->setCellValue('A'.$row, date('d/m/yy'));
      $sheet->setCellValue('B'.$row, $value['location_id']);
      $sheet->setCellValue('C'.$row, $value['total_sale']);
      $sheet->setCellValue('D'.$row, $value['total_order']);
      $row++;
    }
    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    $spreadsheet->addSheet($nps, 1);
    $sheet = $spreadsheet->getSheet(1);;
    $sheet->setCellValue('A1', 'Date, time');
    $sheet->setCellValue('B1', 'LocationID');
    $sheet->setCellValue('C1', 'OrderID');
    $sheet->setCellValue('D1', ' NPS result(0-9)');

    $nps_data = generate_csv_nps();
    $row_nps = 2;
    foreach($nps_data as $value){
      $sheet->setCellValue('A'.$row_nps, date( "d/m/Y, H:i:s", strtotime($value['date'])));
      $sheet->setCellValue('B'.$row_nps, $value['location_id']);
      $sheet->setCellValue('C'.$row_nps, $value['order_id']);
      $sheet->setCellValue('D'.$row_nps, $value['rating']);
      $row_nps++;
    }
    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$file_name\";");
    header("Content-Transfer-Encoding: binary");
    $writer->save('php://output');
    exit;
    // $writer->save($file_name);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM