0

I have HTML table that i want to export as excel , that works!

I needed to show total value of one column on top of the page (and it works on browser) i did it with jQuery.

Here is were i add total value:

echo '<p style="display:inline-block;" class="total-title"></p>';

Here is how i add value:

<script>
$(document).ready(
    function() {
        $('p.total-title').html('&nbsp;total: <?=$total;?>');
    }
);
</script>

And here are my headers:

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=print_report(".date('Y-m-d').").xls");
header("Pragma: no-cache");
header("Expires: 0");
header('Content-Description: File Transfer');  
header("Content-type: application/vnd.ms-excel");

All other data is exported but just this jQuery added content is not showing!

Why it works when i remove headers? And why could it not work in export?

What possible fixes are here?

Thanks

16
  • Of course not. That function gets executed by the browser. Excel knows nothing about jQuery. Commented Mar 8, 2018 at 8:13
  • Also, I hope that you're not really doing this but it was just for the sake of a minimal example here. Commented Mar 8, 2018 at 8:14
  • 1
    Exactly. In this particular case the solution would be to just do echo sprintf('<p style="display:inline-block;" class="total-title">%s</p>', $total); directly with php. Commented Mar 8, 2018 at 8:22
  • 1
    @IngusGraholskis first you do your calculations, when you're done you start printing your totals and your table. It's not a php thing, it's a basic pattern in most languages. First calculate, then when you have everything you need, print it. Commented Mar 8, 2018 at 8:37
  • 1
    @IngusGraholskis first you write ur php code & after that html part of table & in that where u want ur variables or $taotal use them in HTML Table. Commented Mar 8, 2018 at 8:43

1 Answer 1

2

After talking with @MohitKumar and @FedericoklezCulloca i tied to do it like this:

$table = '<table>';
$table .= '<thead>';
$table .= '<tr>';
$table .= '<th>product</th>';
$table .= '<th>value</th>';
$table .= '</tr>';
$table .= '</thead>';
$table .= '<tbody>';

$result = mysqli_query($conn, "SELECT product, value FROM my_table");

while($row = mysqli_fetch_array($result)){

    $table .= '<tr>';
        $table .= '<td>'.$row['product'].'</td>';
        $table .= '<td>'.$row['value'].'</td>';
    $table .= '</tr>';

    $abAmount += $row['value'];

}

$table .= '</tbody>';
$table .= '</table>';       

echo '<br>THAT VALUE HERE: '.$abAmount.'<br>';                  

echo $table;

And it did work!

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, deleted my comment by mistake. Yes, this is a good way to do this :)
@IngusGraholskis Yes , this is the approach i am telling. & finally u got ur excel by exporting as u want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.