Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:44+00:00 2026-05-26T22:57:44+00:00

I’ve got a .php page showing a HTML table similar to this one :-

  • 0

I’ve got a .php page showing a HTML table similar to this one :-

<table border="1">
    <tr bgcolor="DCDCDC">
      <th><div align="center"><b>Data1</b></div></th>
      <th><div align="center"><b>Data2</b></div></th>
      <th><div align="center"><b>Data3</b></div></th>
    </tr>

the table then has some data shown below it. I allow a variable to be passed in the URL if the user wants an .xls file of the table downloading to their PC. The file download code is similar to this :-

function savexls(){
   $data = array( array("Data1" => "Mary", "Data2" => "Johnson", "Data3" => 25), 
         array("Data1" => "Amanda", "Data2" => "Miller", "Data3" => 18), 
         array("Data1" => "James", "Data2" => "Brown", "Data3" => 31), 
         array("Data1" => "Patricia", "Data2" => "Williams", "Data3" => 7), 
         array("Data1" => "Michael", "Data2" => "Davis", "Data3" => 43), 
         array("Data1" => "Sarah", "Data2" => "Miller", "Data3" => 24), 
         array("Data1" => "Patrick", "Data2" => "Miller", "Data3" => 27) ); 

   # filename for download 
   $filename = "Dividend Data " . date("F j, Y") . ".xls"; 

   header("Content-Disposition: attachment; filename=\"$filename\""); 
   header("Content-Type: application/vnd.ms-excel"); 

   $flag = false; 
   foreach($data as $row) {
      if(!$flag) { 
         # display field/column names as first row 
         echo implode("\t", array_keys($row)) . "\r\n"; 
         $flag = true; 
      } 
      array_walk($row, 'cleanData'); 
      echo implode("\t", array_values($row)) . "\r\n"; 
   }
}

function cleanData(&$str) 
{ 
   $str = preg_replace("/\t/", "\\t", $str);
   $str = preg_replace("/\r?\n/", "\\n", $str); 
   if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
} 

I call the savexls() function in the php code using the following line of code after retrieving the variable if it’s in the URL :-

if ($xls=="y") {
   savexls();
}

The problem I’ve got is that if I declare that ?xls=y in the URL, the html table data doesn’t get output to the screen, although the xls file DOES get downloaded to the PC.

If I don’t declare that ?xls=y then the .XLS file DOESN’T get downloaded, and the HTML table gets printed on the screen fine.

Why does the act of writing the .xls file stop the other HTML table being printed to the screen ? They look like totally separate functions to me, but doing the file-saving does seem to stop the first HTML printing code stop doing it’s job.

I am stuck as to what the problem is, other than having a vague feeling that I should perhaps be doing the table-printing and the xls-file saving in different PHP files, and this may solve my problem, but I’m not sure how to do that.

First off I’d like to know why there’s a problem doing it this way, and secondly, is there a good way to solve this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:57:44+00:00Added an answer on May 26, 2026 at 10:57 pm

    Notice that the savexls() function sends the excelsheet headers

    header("Content-Disposition: attachment; filename=\"$filename\""); 
    header("Content-Type: application/vnd.ms-excel"); 
    

    in the response. This makes the browser believe that the returned response is the content of an xls file.

    I believe what you are doing now is the best way to do this. You can keep the php code as it is, but add a download link to your html, which will refer to the same page (but with xls=y in the url). This will allow people who want to only view the file to view it, and people who want to download the xls will be able to download it after they see the xls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm making a simple page using Google Maps API 3. My first. One marker
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.