【发布时间】:2017-02-18 13:43:27
【问题描述】:
index.php
我想根据所选月份导出数据
<div id="page-wrapper">
<div class="container-fluid">
<h2 class="form-signin-heading">Schedule Management</h2><hr />
<button class="btn btn-info" type="button" id="btn-add"> <span class="glyphicon glyphicon-pencil"></span> Add Schedule</button>
<button class="btn btn-info" type="button" id="btn-view"> <span class="glyphicon glyphicon-eye-open"></span> View Schedule</button>
<hr />
<div class="content-loader">
<form method="post" action="index.php">
Select a month <select name="months" onchange ='this.form.submit()'>
<option value="0"> </option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Sched #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Check In Date</th>
<th>Check Out Date</th>
<th>Room Rate</th>
<th>Reservation Fee</th>
<th>Date Paid</th>
<th>Mode of Paymnet</th>
<th>Status</th>
<th>edit</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<?php
require_once 'dbconfig.php';
if(isset($_POST['months'])){ $months = $_POST['months']; }else { $months='';}
$stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) = '".$months."' ");
//this is my solution to filter my table data base on the selected months in the index.php table
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['checkin']; ?></td>
<td><?php echo $row['checkout']; ?></td>
<td><?php echo $row['rrate']; ?></td>
<td><?php echo $row['reservefee']; ?></td>
<td><?php echo $row['datepaid']; ?></td>
<td><?php echo $row['modepayment']; ?></td>
<td><?php echo $row['stats']; ?></td>
<td align="center">
<a id="<?php echo $row['id']; ?>" class="edit-link" href="#" title="Edit">
<img src="edit.png" width="20px" />
</a></td>
<td align="center"><a id="<?php echo $row['id']; ?>" class="delete-link" href="#" title="Delete">
<img src="delete.png" width="20px" />
</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
<a class="btn" href="export.php">Export</a>
</div>
</div>
<br />
<div class="container">
</div>
export.php
导出按钮正在工作,但我想过滤我的数据,就像我在 index.php 中的表中基于选择的月份......但现在我只能在单击导出按钮时执行整个数据在我的数据库上获得输出到 .csv
<?php
require_once 'dbconfig.php';
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');
$stmt = $db_con->prepare("SELECT * FROM tblguest ");
$stmt->execute();
$filename = date('d.m.Y').'.csv';
$data = fopen($filename, 'w');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$csv = implode(',', $row) . "\n";
fwrite($data, $csv);
print_r($csv);
}
echo "\r\n";
fclose($data);
?>
【问题讨论】:
-
在选择任何选项时将参数添加到 URL。这样您就可以根据城市建立链接,您可以使用 $_GET 参数或您的应用程序方式获取参数。
-
请看一下,我写了答案,如果你觉得不错,那么你可以将其标记为接受并投票。