1

I have a form which displays mysql data in the form of an HTML table, using pagination of 5 records per page. I'm trying to export my table to excel, but it's only exporting data of the rows that are currently show, meaning if there are 20 rows, it only shows the first 5 as those get displayed. How can I make it export the one's that are not being displayed but still part of the search? I saw a few other people with a similar question but none have answers( How to export all HTML table rows to Excel file from a paginated table?). Hopefully I can get one!

<?php
if(isset($_GET['submit']) || isset($_GET['page'])){
    // Setting up the Pagination below

    echo '<center>';
$page_query = "SELECT * FROM tbl_call_log_detail ";
$page_query .= "WHERE
     (dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
  OR ( Time <= '$endDate' AND Time >= '$startDate'  
                    AND (dealer_id = '$call_id' OR'$call_id'='' ))    
  OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01'  AND '$call_id'='') ";
$page_result = mysqli_query($conn, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
$start_loop = $page;
$difference = $total_pages - $page;

if($difference <= $total_pages){
    $start_loop = $total_pages - $difference;
}
$end_loop = $start_loop + 2;

if($end_loop > $total_pages){
    $end_loop = $total_pages;
}
if($difference > $total_pages){
    $end_loop = $total_pages;
}
echo '<div class = "center">';
echo '<div class = "pagination">';
if($page > 1){
    echo "<a href= 'dealer_call_log.php?page=1".$urlparameter."'>First</a>";
    echo "<a href= 'dealer_call_log.php?page=".($page - 1).$urlparameter."'> << </a>";
}
for ($i = $start_loop; $i <= $end_loop; $i++){
    echo "<a href= 'dealer_call_log.php?page=".$i.$urlparameter."'>".$i."</a>";
}
if($page < $end_loop){
    echo "<a href= 'dealer_call_log.php?page=".($page + 1).$urlparameter."'> >> </a>";
    echo "<a href= 'dealer_call_log.php?page=".$total_pages.$urlparameter."'>Last</a>";
}
 if($page < 1){
$page = 1;
 }
    echo '</div>'; 
    echo '</div>';

    echo '<br>';

    $sql = "SELECT Name, SFID, Comment, Time FROM tbl_call_log_detail 
    WHERE
     (dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
  OR ( Time <= '$endDate' AND Time >= '$startDate'  
                    AND (dealer_id = '$call_id' OR'$call_id'='' ))    
  OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01'  AND '$call_id'='')
     ORDER BY Time DESC LIMIT $start_from, $record_per_page ";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_num_rows($result);
    $all_property = array();

    echo "<table class = 'data-table' border = '1' cellpadding = '9' bgcolor = '#CCCCCC' id = 'data-table'>
            <tr class = 'data-heading'>";
    while($property = mysqli_fetch_field($result)){
        echo '<td><b> '. $property ->name. ' </b></td>';
        array_push($all_property, $property ->name);

    }
    echo '</tr>';

    while ($row = mysqli_fetch_array($result)){
        echo '<tr>';
        foreach($all_property as $item){
            echo '<td> '. $row[$item] . ' </td>';
        }
        echo '</tr>';
        echo '</center>';

            }
            echo '</table>';
            echo '<br>';
            ?>

// This is what is getting the current rows, but not all

            <input type = "submit" onclick = "window.open('data:application/vnd.ms-excel, '+encodeURIComponent(document.getElementById('data-table').outerHTML));" value = "Export into excel" />  
            <?php 
}
?>

UPDATE: Found the answer I was looking for I simply ran a new sql query without the LIMIT clause and stored it in a hidden table. I then use the hidden table to export data

// SQL and hidden table for exporting to excel 
            $page_query2 = "SELECT * FROM tbl_call_log_detail ";
            $page_query2 .= "WHERE
     (dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
  OR ( Time <= '$endDate' AND Time >= '$startDate'  
                    AND (dealer_id = '$call_id' OR'$call_id'='' ))    
  OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01'  AND '$call_id'='') ORDER BY TIME DESC ";

  $page_result2 = mysqli_query($conn, $page_query2);
  $row2 = mysqli_num_rows($page_result2);
  $all_property2 = array();

  echo "<table class = 'data-table2' border = '1' cellpadding = '9' bgcolor = '#CCCCCC' id = 'data-table2' hidden>
            <tr class = 'data-heading2'>";
    while($property = mysqli_fetch_field($page_result2)){
        echo '<td><b> '. $property ->name. ' </b></td>';
        array_push($all_property2, $property ->name);

    }
    echo '</tr>';

    while ($row2 = mysqli_fetch_array($page_result2)){
        echo '<tr>';
        foreach($all_property2 as $item){
            echo '<td> '. $row2[$item] . ' </td>';
        }
        echo '</tr>';
        echo '</center>';

            }
            echo '</table>';
            ?>
            <input type = "submit" onclick = "window.open('data:application/vnd.ms-excel, '+encodeURIComponent(document.getElementById('data-table2').outerHTML));" value = "Export into excel" />
            <?php 
}
?>
abc
  • 45
  • 1
  • 6

1 Answers1

1

You can use JQuery table2excel plugin following is the link

http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html

Try following code pen to only JavaScript

https://codepen.io/kostas-krevatas/pen/mJyBwp
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34