Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export data from php page to excel spreadsheet 1

Status
Not open for further replies.

mamasita60440

Programmer
Aug 4, 2006
16
US
I am trying to export contents from a php page to an excel spreadsheet. I am not an expert so my code may not be as sophisticated. For some reason the variables from the previous page are not posting even though I have created a session and listed the variables. Here is the code for the page where the variables that will be exported to the excel sheet should come from this page called 'displayReportData.php':

<?php session_start();

$_SESSION["ReportData"]=$_POST["ReportData"];
$_SESSION["PerformanceID"]=$_POST["PerformanceID"];
$_SESSION["HandleName"]=$_POST["HandleName"];
$_SESSION["ServerName"]=$_POST["ServerName"];
$_SESSION["LoadName"]=$_POST["LoadName"];
$_SESSION["InstallDate"]=$_POST["InstallDate"];

?>

<form action="tryexcel.php" method= "post" name="FormName">

<html>
<body>
<table>

<table border='1'>
<tr><th>Handle</th><th>Load</th><th>Server</th><th>Installation Date</th><th>Performance ID</th>

</tr>
<?php

foreach ($_POST['ReportData'] as $cbox=>$cboxvalue) {

$HandleName= $_POST["HandleName"][$cbox];
$LoadName= $_POST["LoadName"][$cbox];
$ServerName= $_POST["ServerName"][$cbox];
$InstallDate= $_POST["InstallDate"][$cbox];
$PerformanceID= $_POST["ReportData"][$cbox];

echo "<tr><td>".$HandleName."</td>
<td>".$LoadName."</td>
<td>".$ServerName."</td>
<td>".$InstallDate."</td>
<td>".$PerformanceID."</td>

</tr>";
}
?>
</table>
<br><br><input type="Submit" value="Display Graphs">
</form>
</body>
</html>

The information displayed on this page named 'tryexcel.php' should be exported to the excel spreadsheet. Here is the code:

<?php session_start();

$PerformanceID=$_SESSION["ReportData"]=$_POST["ReportData"];

?>

<?php
$db_host = "gpb";
$db_user = "ott";
$db_pwd = "77";
$db_name = "_run";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<?php
$select = "SELECT * FROM Peformance where PerformanceID='$PerformanceID' ";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
?>


<?php
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
?>

<?php
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
?>

<?php
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
?>

<?php
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>

I am not looking for a free lunch. I have been working on this for quite some time. Any assistance you can offer is greatly appreciated.

In sum, I am trying to get the information stored in the 'PerformanceID' variable on the 'displayReportData.php' page to post on the 'tryexcel.php' so that the data with those performance id's will be exported to excel. Thank you for help in advance.
 
What submits information to the first script?

Your script "displayRecordData.php" begins with the lines:

Code:
session_start();

$_SESSION["ReportData"]=$_POST["ReportData"];
$_SESSION["PerformanceID"]=$_POST["PerformanceID"];
$_SESSION["HandleName"]=$_POST["HandleName"];
$_SESSION["ServerName"]=$_POST["ServerName"];
$_SESSION["LoadName"]=$_POST["LoadName"];
$_SESSION["InstallDate"]=$_POST["InstallDate"];

$_POST will be empty unless some HTML form is submitting data to it. If $_POST is empty, so will be all those session variables. In fact, PHP should generate a bunch of warnings about unset variables -- the only reason why it would not is if your settings error_reporting and display_errors are not set right for a beginner in a developement environment (the settings should be "on" and "E_ALL", respectively).



Want the best answers? Ask the best questions! TANSTAAFL!
 
Here is the code for the form 'PfrReports.php' which submits to the 'displayReportData.php'.

<?php session_start();

$PfrDate =$_SESSION["PfrDate"]=$_POST["PfrDate"];
?>

<html>
<head>
<title>Displaying MySQL Data</title>
</head>
<body>
<?php
$db_host = "????";
$db_user = "???";
$db_pwd = "???";
$db_name = "_run";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>

<form id="FormName" action= "displayReportData.php" method="post"
name="FormName">

<table>
<?php
$sql = "SELECT * FROM Performance where PfrDate='$PfrDate ' ";

echo "<table border='1'>";
echo "<tr><th>Select</th><th>Handle</th><th>Load</th><th>Server</th><th>Installation Date</th<th>Performance ID</th></tr>";

$query= mysql_query($sql);

while($row = mysql_fetch_assoc($query)){

$i=$row['PerformanceID'];


echo <<<STR
<tr>
<td><input type='checkbox' name="ReportData[$i]" value="{$i}"></td>
<td><input type="text" name="HandleName[$i]" value="{$row['HandleName']}" /></td>
<td><input type="text" name="LoadName[$i]" value="{$row['LoadName']}" /></td>
<td><input type="text" name="ServerName[$i]" value="{$row['ServerName']}" /></td>
<td><input type="text" name="InstallDate[$i]" value="{$row['InstallDate']}" /></td>
<td><input type="text" name="PeformanceID[$i]" value="{$i}" /></td>

</tr>

STR;

}

?>
</table>
<br><br><input type="submit" value="Review Your Selected Results">
</form>
</body>
</html>
 
PfrReports.php creates a form which submits to displayReportData.php, which outputs an HTML form that submits to tryexcel.php.

But at some point data isn't getting where it needs to.

What data is missing at what point? How have you tested this?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Yes, I have tested all the code. The information submitted on the 'PfrReports.php' form is displayed on the 'displayReportData.php' page.

On the tryexcel.php I started another session and created a variable 'PerformanceID' which is equal to the session variable 'ReportData'.

When I run the code with the following:

<?php session_start();

$ID=$_SESSION["ReportData"];
?>

<?php
$db_host = "n";
$db_user = "es";
$db_pwd = "43";
$db_name = "_run";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>

<?php
$select = "SELECT * FROM Peformance where PerformanceID='$ID' ";

The information from the 'displayReportData.php' is not carrying over to the 'tryexcel.php' page.

I have also tried the following with a post variable with no luck:

<?php session_start();

$ID=$_SESSION["ReportData"]=$_POST["ReportData"];
?>

<?php
$db_host = "n";
$db_user = "e";
$db_pwd = "43";
$db_name = "_run";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>

<?php
$select = "SELECT * FROM Peformance where PerformanceID='$ID' ";
 
Ok...so i changed the code again. This time I am no longer querying the db b/c according to print_r all of the session variables are being exported to the spreadsheet.

Here is what I have so far

<?php session_start();

$ID=$_SESSION["ReportData"];

?>

<?php

$line = '';
foreach($ID as $line) {
foreach($line as $key => $value) {
$data .= $value . "\t";
}

$data .= "\n";

}



$data = str_replace("\r","",$data);
?>

<?php
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
?>

<?php
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

?>

The session variable "ReportData" comes from the displayReportData.php

Is there anyone who can help me to get this stuff to print on the spreadsheet? The variables are being stored in a session.
 
The code is printing to the excel sheet, but now only one field is printing. I thought with the foreach statement the entire row would print. Here is the code, any suggestions? Thanks.

<?php session_start();

$_SESSION["ReportData"];

?>

<?php

foreach($_SESSION as $line) {
foreach($line as $key => $value) {
$data .= $value . "\t";
}

$data .= "\n";

}



$data = str_replace("\r","",$data);
?>

<?php
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
?>

<?php
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

?>
 
it's difficult to help without seeing how you generate the session data. if you do a print_r of the $_SESSION data do you get what you expect? should you not be doing a foreach of the $_SESSION['ReportData'] variable rather than the whole superglobal?

also, another member pointed out recently that the best way to get data into excel from php was to build a string in table format (using <table><tr><td></td></tr></table>) and then echo it out with an excel content-type. the advantage of this method is that you get some formatting ready accepted by excel from the html table. there should also be no need to do any conversions when you open the table in excel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top