将搜索结果导出到.CSV,PHP-MySQL
我还是PHP MySQL的初学者。我正在寻找将将搜索结果导出到.CSV,PHP-MySQL,php,mysql,csv,Php,Mysql,Csv,我还是PHP MySQL的初学者。我正在寻找将搜索数据导出到(单击搜索按钮) 如果未找到结果>提示错误>模具 如果找到任何记录>显示>(单击导出按钮)>导出到.CSV 这是我的搜索表单代码: <?php session_start(); include_once "dbconnect.php"; date_default_timezone_set('Hongkong'); //LOG IN VALIDATION if(!isset($_SESSION['use
搜索数据导出到
的最有效/最简单的方法
场景:
我想有一个导出按钮,搜索结果可以导出到.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";
}}}} ?>
//HTML搜索表单在这里
//搜索按钮
搜索客户
//用于将搜索数据导入CSV的按钮
//没有功能,但不知道如何导出数据
导出到CSV
PS:我仍然在使用MySQL,因为我想先熟悉一下。下面的步骤将帮助您
创建用于检索搜索结果记录的函数
验证输入并调用此函数,然后以数组/对象的形式返回值
如果结果为空,则返回false
显示获取的记录
如果“单击导出”,则使用一个导出标志提交相同的搜索值
检查您设置的标志并调用相同的函数进行搜索
设置下载CSV文件的标题
使用“|”或“,”等分隔符打印结果
@德鲁:是的,我报告删除了,先生。还宽吗,先生?我的问题是如何将搜索表单的结果导出到csv?
我要求您至少显示一些您想要的示例输出,并首先转向mysql。不是用这样的输出来改进问题(说您看到的只是PHP代码),而是堆栈上没有其他引用起作用,您知道rest@Drew:我明白了,先生,我很快就会编辑这个。谢谢您能提供一个这个实现的工作示例吗?谢谢