如何在PHP中导出动态表
我有一个网站,用户可以在我的下拉列表中选择任何一个来操作表中的数据。现在,用户可以选择导出表中显示的数据。当用户点击导出时,什么也没有发生,我是新的PHP和英语抱歉 下面是我的index.php和excel.php,你能告诉我遗漏了什么吗 INDEX.PHP如何在PHP中导出动态表,php,mysql,Php,Mysql,我有一个网站,用户可以在我的下拉列表中选择任何一个来操作表中的数据。现在,用户可以选择导出表中显示的数据。当用户点击导出时,什么也没有发生,我是新的PHP和英语抱歉 下面是我的index.php和excel.php,你能告诉我遗漏了什么吗 INDEX.PHP <?php if (isset($_POST['search'])) { $cmbDept = $_POST['cmbDept']; $query = "SELECT * FROM daily_data2 WHERE Camp
<?php
if (isset($_POST['search'])) {
$cmbDept = $_POST['cmbDept'];
$query = "SELECT * FROM daily_data2 WHERE Campaign LIKE '" . $cmbDept . "'";
$search_result = filterTable($query);
} else {
$query = "SELECT * FROM daily_data2";
$search_result = filterTable($query);
}
function filterTable($query) {
$connect = mysqli_connect("localhost", "root", "", "bio_db");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
INDEX.PHP中的HTML代码
<html>
<head>
<title>Employee Logs</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<h2 align="center">Time and Attendance Monitoring</h2>
<center>
<form method="GET" action="excel.php">
<input type="submit" name="export_excel" class="btn btn-success" value="Export to Excel">
</form>
<form action="index.php" method="post">
<select id="cmbDept" name="cmbDept">
<option value = '' selected="selected">Filter by Department</option>
<option value = 'TKV'>TKV</option>
<option value = 'NA'>NA</option>
<option value = 'PURE-INC'>PURE INC</option>
<option value = 'DUTY-FREE'>DUTY-FREE</option>
<option value = 'HQL'>HQL</option>
<option value = 'PRO-XPN'>PRO-XPN</option>
<option value = 'Mate1'>Mate1</option>
<option value = 'STUDENT-rUS'>STUDENT-rUS</option>
<option value = 'COLLECTIONS'>COLLECTIONS</option>
<option value = 'NTD'>NTD</option>
<option value = 'DATA RESEARCHER'>DATA RESEARCHER</option>
<option value = 'VA'>DATA RESEARCHER</option>
</select>
<input type="submit" name="search" value="Search"><br><br>
</center>
<table align="center" width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Userid</th>
<th>Name</th>
<th>Campaign</th>
<th>Date</th>
<th>Hoursworked</th>
<th>Overtime</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td style="text-align:center;"><?php echo $row['Userid'];?></td>
<td width="200"><?php echo $row['Name'];?></td>
<td style="text-align:center;"><?php echo $row['Campaign'];?></td>
<td width="100" style="text-align:center;"><?php echo $row['Date'];?></td>
<td style="text-align:center;"><?php echo $row['Hoursworked'];?></td>
<td style="text-align:center;"><?php echo $row['Overtime'];?></td>
</tr>
<?php endwhile;?>
</table>
//<?php
// $cmbDept = $_GET['cmbDept'];
// ?>
</form>
</body>
员工日志
表,tr,th,td
{
边框:1px纯黑;
}
时间和出勤监控
按部门筛选
TKV
NA
纯公司
免税
HQL
PRO-XPN
Mate1
学生罗斯
收藏
新台币
数据研究员
数据研究员
用户ID
名称
运动
日期
工作时间
加班
//
在HTML中:
<form method="POST" action="excel.php">
<input type="hidden" name="cmbDept" value="<?php echo isset($_POST['cmbDept']) ? $_POST['cmbDept'] : ''; ?>">
<input type="submit" name="export_excel" class="btn btn-success" value="Export to Excel">
</form>
在HTML中:
<form method="POST" action="excel.php">
<input type="hidden" name="cmbDept" value="<?php echo isset($_POST['cmbDept']) ? $_POST['cmbDept'] : ''; ?>">
<input type="submit" name="export_excel" class="btn btn-success" value="Export to Excel">
</form>
需要进行更改:
1)excel.php中的更改
<?php
$connect = mysqli_connect("localhost", "root", "", "bio_db");
$output = '';
if (isset($_POST["export_excel"])) {
$query = "SELECT * FROM daily_data2 WHERE Campaign LIKE '" . $cmbDept . "'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
$output .= '
<table class="table bordered="1">
<tr>
<th>Userid</th>
<th>Name</th>
<th>Campaign</th>
<th>Date</th>
<th>Hoursworked</th>
<th>Overtime</th>
</tr>
';
while ($row = mysqli_fetch_array($result)) {
$output .= '
<tr>
<td>' . $row["Userid"] . '</td>
<td>' . $row["Name"] . '</td>
<td>' . $row["Campaign"] . '</td>
<td>' . $row["Date"] . '</td>
<td>' . $row["Hoursworked"] . '</td>
<td>' . $row["Overtime"] . '</td>
</tr>';
}
$output .= '</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
echo $output;
}
}?>
if(isset($\u POST[“export\u excel”]){
到
if(isset($\u GET[“export\u excel”]){
例如,method=“GET”
出现在
2)否,$cmbDept
存在于excel.php
中。因此,您需要以隐藏形式传递表单内部
像
excel.php
<form method="GET" action="excel.php">
<input type="hidden" name="cmbDept" value="<?php echo isset($_POST['cmbDept']) ? $_POST['cmbDept'] : ''; ?>">
<input type="submit" name="export_excel" class="btn btn-success" value="Export to Excel">
</form>
<?php
$connect = mysqli_connect("localhost", "root", "", "bio_db");
if (isset($_GET["export_excel"])) {
$cmbDept = $_GET['cmbDept'];
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
header("Pragma: no-cache");
header("Expires: 0");
$sep = "\t";
$sql = "SELECT `Userid`,`Name`,`Campaign`,`Date`,`Hoursworked`,`Overtime` FROM daily_data2 WHERE Campaign LIKE '" . $cmbDept . "'";
$result = mysqli_query($connect,$sql);
//start of printing column names as names of MySQL fields
while ($property = mysqli_fetch_field($result)) {
echo $property->name.$sep;
}
print("\n");
//end of printing column names
//start while loop to get data
while($row = mysqli_fetch_array($result,MYSQLI_NUM))
{
$schema_insert = "";
for($j=0; $j<mysqli_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
}
?>
需要进行更改:
1)excel.php中的更改
<?php
$connect = mysqli_connect("localhost", "root", "", "bio_db");
$output = '';
if (isset($_POST["export_excel"])) {
$query = "SELECT * FROM daily_data2 WHERE Campaign LIKE '" . $cmbDept . "'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
$output .= '
<table class="table bordered="1">
<tr>
<th>Userid</th>
<th>Name</th>
<th>Campaign</th>
<th>Date</th>
<th>Hoursworked</th>
<th>Overtime</th>
</tr>
';
while ($row = mysqli_fetch_array($result)) {
$output .= '
<tr>
<td>' . $row["Userid"] . '</td>
<td>' . $row["Name"] . '</td>
<td>' . $row["Campaign"] . '</td>
<td>' . $row["Date"] . '</td>
<td>' . $row["Hoursworked"] . '</td>
<td>' . $row["Overtime"] . '</td>
</tr>';
}
$output .= '</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
echo $output;
}
}?>
if(isset($\u POST[“export\u excel”]){
到
if(isset($\u GET[“export\u excel”]){
例如,method=“GET”
出现在
2)否,$cmbDept
存在于excel.php
中。因此,您需要以隐藏形式传递表单内部
像
excel.php
<form method="GET" action="excel.php">
<input type="hidden" name="cmbDept" value="<?php echo isset($_POST['cmbDept']) ? $_POST['cmbDept'] : ''; ?>">
<input type="submit" name="export_excel" class="btn btn-success" value="Export to Excel">
</form>
<?php
$connect = mysqli_connect("localhost", "root", "", "bio_db");
if (isset($_GET["export_excel"])) {
$cmbDept = $_GET['cmbDept'];
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
header("Pragma: no-cache");
header("Expires: 0");
$sep = "\t";
$sql = "SELECT `Userid`,`Name`,`Campaign`,`Date`,`Hoursworked`,`Overtime` FROM daily_data2 WHERE Campaign LIKE '" . $cmbDept . "'";
$result = mysqli_query($connect,$sql);
//start of printing column names as names of MySQL fields
while ($property = mysqli_fetch_field($result)) {
echo $property->name.$sep;
}
print("\n");
//end of printing column names
//start while loop to get data
while($row = mysqli_fetch_array($result,MYSQLI_NUM))
{
$schema_insert = "";
for($j=0; $j<mysqli_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
}
?>
$cmbDept在excel中未定义。phphow我应该定义它吗?您是否使用标记进行导出?您是否可以从进入excel.php的$cmbDept
位置显示html代码?$cmbDept在excel中未定义。phphow我应该定义它吗?您是否使用标记进行导出?您是否可以从进入excel.php的$cmbDept
位置显示html代码?并且,从您得到这行代码的地方,他在导出的同一文件index.phpuse post方法中包含html和php代码_excel@Rijin我尝试了你的代码,它运行得很好!但是它在excel中没有数据?但是它在所有列中都有标题,在这里显示错误“while($row=mysqli\u fetch\u array($result))”从这行代码中可以看出,他在export中的index.phpuse post方法的同一个文件中有html和php代码_excel@Rijin我尝试了你的代码,它运行得很好!但是它在excel中没有数据?但是它在所有列中都有标题,在这里显示错误“while($row=mysqli\u fetch\u array($result))”太棒了,你太棒了!事实上你解释得很好,你错过了Rijin使用的method=“POST”,所以我会给他答案,谢谢你的回答。”…事实上没有…“它不可能不起作用。没问题@Seryu。享受编码。太棒了,你太棒了!事实上你没有解释,你错过了method=“POST”这是Rijin使用的,所以我会给他答案,谢谢你的回答。”…实际上它不…“它不可能不起作用。没问题@Seryu。享受编码吧。