我还是PHP MySQL的初学者。我正在为 exporting
Searched Data
.CSV
寻找最有效/最简单的方法。
情景:
我想有导出按钮,搜索结果可以导出到.CSV
之类的。
也许这个过程是这样的:
搜索您想要的任何数据> (单击“搜索”按钮)
如果没有找到结果>提示错误>死亡 如果找到任何记录>显示> (单击“导出”按钮)>导出到.CSV
以下是我的搜索表单代码:
<?php
session_start();
include_once "dbconnect.php";
date_default_timezone_set('Hongkong');
//LOG IN VALIDATION
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM accounts WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
?>
//HTML SEARCH FORM IS HERE
//SEARCH BUTTON <br/><h5 style="float:right;"><button type="submit" class="btn btn-theme02" name="search" ><i class="fa fa-check">Search Customers</i></button></h5>
//BUTTON FOR IMPORTING THE SEARCHED DATA INTO CSV
//DONT HAVE FUNCTION YET DON'T KNOW HOW TO EXPORT THE DATA
<button type="submit" name="EXPORT">EXPORT TO CSV</button>
<?php
$number = 1;//NOT IMPORTANT, JUST USE TO NUMBERING THE ITEMS SEARCHED
//TABLE
echo "<table class=\"table table-hover\" id=\"table1\" >";
//TABLE ROW NAMES - SQL TABLE
echo " <hr><thead><tr>";
echo "<th></th>";
echo "<th>Globe Number</th>";
echo "<th>Full Name</th>";
echo "<th>State</th>";
echo "<th>Country</th>";
echo "<th>Virtual Number</th>";
echo "<th>Date of Subscription</th>";
echo "<th>Expiration Date</th>";
echo "<th></th>";
echo "</tr></thead>";
//FUNCTION OF SEARCH BUTTON
if (isset($_POST['search'])) {
$globe = htmlentities(mysql_real_escape_string($_POST['globe']));
$name = htmlentities(mysql_real_escape_string($_POST['name']));
$vr = htmlentities(mysql_real_escape_string($_POST['virtual']));
$ctry= htmlentities(mysql_real_escape_string($_POST['country']));
if (empty($globe) && empty($name) && empty($vr) && empty($ctry) && empty($dateStarted) && empty($dateExpired)
&& empty($time) && empty($transaction) && empty($subs) && empty($status) && empty($remarks) && empty($year) && empty($month)) {
echo '<script>alert(\'Select atleast 1 filter\')</script>';
exit;
}
//SEARCH CONDITIONS
if (!empty($globe)) {
$sql = "SELECT * FROM `subscribers` WHERE `globeNumber` = '".$globe."' AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
}if (!empty($name)) {
$sql = "SELECT * FROM `subscribers` WHERE `fName` LIKE '%".$name."%' OR `lName` LIKE '%".$name."%' AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
}if (!empty($vr)){
$sql = "SELECT * FROM `subscribers` WHERE `virtualNumber` = '".$vr."' AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
}if (!empty($ctry)){
$sql = "SELECT * FROM `subscribers` WHERE `country` = '".$ctry."' AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
//COMBINATION CONDITIONS
// I MINIMIZE THE CONDITION FOR SEARCH HERE
} if (!empty($globe) && !empty($name)) {
$sql = "SELECT * FROM `subscribers` WHERE `globeNumber` = '".$globe."' AND `fName` = '".$name."' AND `lName` = '".$name."'
AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
} if (!empty($globe) && !empty($vr)) {
$sql = "SELECT * FROM `subscribers` WHERE `globeNumber` = '".$globe."' AND `virtualNumber` = '".$vr."'
AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
} if (!empty($globe) && !empty($ctry)) {
$sql = "SELECT * FROM `subscribers` WHERE `globeNumber` = '".$globe."' AND `country` = '".$ctry."'
AND `agentCode`='".$agent."' ORDER BY `dateStarted` DESC ";
}
//THE QUERY RUN HERE
if ($result=mysql_query($sql)) {
$query_num_rows = mysql_num_rows($result);
//IF NO RECORD FOUND RUN THIS
if($query_num_rows == 0){
echo "<script> alert('No Records Found, Please try again!')</script>";
} else {
while($row=mysql_fetch_array($result)){
echo "<tbody><tr>";
echo "<td>".$number."</td>";
echo "<td>".$row['globeNumber']."</td>";
echo "<td>".$row['lName'].", ".$row['fName']." ".$row['mName']."</td>";
echo "<td>".$row['state']."</td>";
echo "<td>".$row['country']."</td>";
echo "<td>".$row['virtualNumber']."</td>";
echo "<td>".$row['dateStarted']."</td>";
echo "<td>".$row['dateExpired']."</td>";
++$number;//NOT IMPORTANT, JUST USE FOR NUMBERING THE ITEMS SEARCHED
echo "</form></tr>";
echo "string";
}}}} ?>
PS:我还在使用MySQL,因为我想先熟悉它。
答案 0 :(得分:1)
贝娄的步骤会对你有帮助。
- 创建用于检索搜索结果记录的函数
- 验证输入并调用此函数并以array / object
的形式返回值- 如果结果为空,则返回false
- 显示提取的记录
- 如果&#34;点击导出&#34;使用一个导出标志提交相同的搜索值
- 检查您设置的标记并为搜索调用相同的功能
- 设置下载CSV文件的标题
- 使用&#39; |&#39;等分隔符打印结果或&#39;,&#39;
醇>