I have made some success but still need improvement...
Look at the following code:
Code:
<?php
if(isset($_GET['product']))
{
$key = $_GET['product'];
echo "<br /><br /><br />";
echo "<h2 style = \" text-align:center; \" > Total installations of " . ${$key} . "</h2>";
echo "<br /><br />";
$query_string = "SELECT * FROM inventory WHERE $key = 1";
$query = mysql_query($query_string);
if(!$query){ die(mysql_error()); }
$body = "";
$display = "<table border = \"1\" bgcolor = \"FFFFCC\" cellspacing= \"15\" cellpadding = \"7\">";
echo $display;
$body .= $display;
$display = "<thead style =\" font-weight:bold; color:green; \"><tr> <td>Name </td> <td>Cabin no</td> <td>Team</td><td> Service Tag</td> <td>System name</td> </tr></thead>";
echo $display;
$body .= $display;
while($result = mysql_fetch_array($query))
{
$display = "<tr>";
$display .= "<td><a href = \"search.php?searchfor=stagno&searchstring=" . $result['stagno'] . "\">";
$display .= $result['first_name'] . " " . $result['last_name'] . "</a></td>";
$display .= "<td>" .$result['cabin_no'] . "</td>";
$display .= "<td>" .$result['team'] . "</td>";
$display .= "<td>" .$result['stagno'] . "</td>";
$display .= "<td>" .$result['sname'] . "</td>";
$display .= "</tr>";
echo $display;
$body .= $display;
}
$display = "</table>";
echo $display;
$body .= $display;
$body = htmlspecialchars($body);
?>
<form action = "exp_to_excel.php" method = "post">
<input type = "hidden" name = "body" value = "<?php echo $body ; ?>">
<input type = "submit" name = "submit" Value = "Export to excel">
</form> <?php
}
?>
see that I have two variables $body and $display... $display is just for displaying the table to the current page but every piece of $display appends to $body.. Atlast $body is submitted to another page as a hidden value to another page called exp_to_excel.php..
Here is the code in exp_to_excel.php:
Code:
function setHeader($excel_file_name)//this function used to set the header variable
{
header("Content-type: application/octet-stream");//A MIME attachment with the content type "application/octet-stream" is a binary file.
//Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor.
header("Content-Disposition: attachment; filename=$excel_file_name");//with this extension of file name you tell what kind of file it is.
header("Pragma: no-cache");//Prevent Caching
header("Expires: 0");//Expires and 0 mean that the browser will not cache the page on your hard drive
}
if(isset($_POST['submit']))
{
$body = $_POST['body'];
setHeader("inventory.xls");
echo $body;
}
This actually works great but there are two problems:
1) The button to 'export to excel' is at the bottom of the page because $body is getting built in each and every line and becomes complete only at the bottom... But I want the button to display above the table.
2) The table has some links, you notice that I have added some 'href' tags but I don't want them to be in the table that is exported..
Of course I can write the complete code twice and I can make the first one for building the $body and second one for just displaying the results in the same page... But is there any other efficient way? Will it be a smart programming if I repeat same kind of code twice in a page?
Bookmarks