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);
?>
答案 0 :(得分:0)
您可以使用以下代码。首先需要根据条件选择数据库。
在You HTML中设置选择下拉按钮,并在Get request中使用Day参数。
<label>Day</label>
<select id='lead_ids' name='lead_ids' onchange="location = this.value;">
<option value=""> ... </option>
<option value="search"> All </option>
<option value="search?day=Monday">{{ Monday }}</option>
<option value="search?day=Tuesday">{{ Tuesday }}</option>
</select>
通过这种方式,您可以根据城市建立链接,您可以使用$ _GET参数或您的申请方式获取参数。
$q = "SELECT * FROM tblguest";
if(!empty($_GET('day'))) {
$q .= ' WHERE day= ' . $_GET('day');
}
$stmt = $db_con->prepare($q);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
$file_name = "downloads/".date('dmY_His').".csv";
// call the csv export
exportToCsv($result, $file_name);
这样您就可以使用不同的参数添加多个条件。
调用此函数将其导出为CSV。
function exportToCsv($array, $file_name = "export.csv", $delimiter=";") {
$f = fopen($file_name, "w");
$head = array_keys($array[0]);
fputcsv($f, $head);
foreach ($array as $line) {
fputcsv($f, $line);
}
if (file_exists($file_name))
{
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.basename($file_name));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));
ob_clean();
flush();
readfile($file_name);
exit;
}
}
如果得到答案,请将答案标记为已接受。