【发布时间】:2020-10-18 05:52:53
【问题描述】:
您好,我创建了一个导出器页面,当用户过滤数据并按下过滤器时...数据出现在表格下方,然后用户可以导出过滤的数据,但导出时出现问题,这是打印的错误图像在导出的 CSV 文件中:
当我按下导出 BTN 时它导出了。
但这是问题所在:
<?php
include('database_connection.php');
$query='';
if(isset($_POST["export_rule"]))
{
$connect = mysqli_connect("localhost", "root", "", "uni");
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=EXPORT_FILTER.csv');
$output = fopen("php://output", "w");
/////////////////
fputcsv($output, array('email', 'password', 'firstname', 'lastname', 'field', 'country'));
$tag= $_POST['tags'];
$field=$_POST['filter_field'];
$country=$_POST['filter_country'];
// $search = str_replace(",", "|",$all );
$query .= '
SELECT * FROM csv WHERE email LIKE "%'.$tag.'%" AND field = "'.$field.'" AND Country = "'.$country.'"
';
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
}
?>
在这部分
$tag= $_POST['tags'];
$field=$_POST['filter_field'];
$country=$_POST['filter_country'];
我使用用户在索引页面中过滤的变量
这是我的索引页:
<?php
include('database_connection.php');
//index.php
$connect = new PDO("mysql:host=localhost;dbname=uni", "root", "");
$qy = "SELECT DISTINCT email FROM csv ORDER BY email ASC";
$sta = $connect->prepare($qy);
$sta->execute();
$resa = $sta->fetchAll();
$country='';
$field='';
$qu = "SELECT DISTINCT field FROM csv ORDER BY field ASC";
$query = "SELECT DISTINCT Country FROM csv ORDER BY Country ASC";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$country .= '<option value="'.$row['Country'].'">'.$row['Country'].'</option>';
}
$field='';
$state = $connect->prepare($qu);
$state->execute();
$res = $state->fetchAll();
foreach($res as $row)
{
$field .= '<option value="'.$row['field'].'">'.$row['field'].'</option>';
}
?>
<html>
<head>
<title> Search By at least 3 Rule</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js" crossorigin="anonymous"></script>
<link href="css/bootstrap-select.min.css" rel="stylesheet" />
<script src="js/bootstrap-select.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">upload by samad</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li ><a href="index.php">search by company</a></li>
<li class="active" ><a href="index_rule.php">Search by 2 Rule</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">tool box <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://extendsclass.com/csv-generator.html">CSV Generator</a></li>
<li><a href="https://www.w3schools.com/default.asp">W3 </a></li>
<li><a href="https://gmail.com">check you're mail</a></li>
</ul>
</li>
<li><a href="about.html">Who We Are</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="register.html"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="login.html"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="container box">
<div class="container">
<h3>search among of data that you stored them by check up 3 rule</h3>
<p>HELP:you can search anything BUT you need to check 3 availabe rule for this search for example you have to select field - country - and company to start searching so if you DONT fill All Data you will be alerted that you're leting a field empty and you must fill it too! </p>
</div>
<br><br><br><br><br>
<h1 align="center">Rule search </h1>
<br />
<br>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="form-group">
<div align="center">
</select>
</div>
</div>
<form action="export_rule.php" method="post" class="form-group">
<div class="form-group">
<div class="row">
<div align="center" class="col-md-29">
<input type="text" name="tags" id="tags" placeholder="Company Filter" class="form-control"/>
</div>
</div>
</div>
<div class="form-group">
<select name="filter_field" id="filter_field" class="form-control" required>
<option value="">Select Field</option>
<?php echo $field; ?>
</select>
</div>
<div class="form-group">
<select name="filter_country" id="filter_country" class="form-control" required>
<option value="">Select Country</option>
<?php echo $country; ?>
</select>
</div>
</form>
<div class="form-group" align="center">
<button type="button" name="filter" id="filter" class="btn btn-info">Filter</button>
</div>
</div>
<div class="col-md-4"></div>
</div>
<div class="table-responsive">
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="20%">Email</th>
<th width="10%">Password</th>
<th width="25%">FirstName</th>
<th width="15%">LastName</th>
<th width="15%">Field</th>
<th width="15%">Country</th>
</tr>
</thead>
</table>
<br />
<br />
<br />
</div>
</div>
<div align="center">
<form action="export_rule.php" method="post"><input type="submit" name="export_rule" id="export_rule" value="CSV Export" class="btn btn-info" />
</div>
</body>
</html>
<?
///eport_rule
if(isset($_POST["export_rule"]))
{
$connect = mysqli_connect("localhost", "root", "", "uni");
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=EXPORT_FILTER.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('email', 'password', 'firstname', 'lastname', 'field', 'country'));
$sample= $_POST["tags"];
$field=$_POST["filter_field"];
$country=$_POST["filter_country"];
$all=[$sample,$field,$country];
$search = str_replace(",", "|",$all );
$query .= '
WHERE email LIKE "%'.$_POST['tags'].'%" AND field = "'.$_POST['filter_field'].'" AND Country = "'.$_POST['filter_country'].'"
';
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
}
?>
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
fill_datatable();
function fill_datatable(filter_field = '', filter_country = '', tags = '')
{
var dataTable = $('#user_data').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"searching" : false,
"ajax" : {
url:"fetch_rule.php",
type:"POST",
data:{
filter_field:filter_field, filter_country:filter_country, tags:tags
}
}
});
}
$('#filter').click(function(){
var filter_field = $('#filter_field').val();
var filter_country = $('#filter_country').val();
var tags = $('#tags').val();
if(filter_field != '' && filter_country != '' && tags != '')
{
$('#user_data').DataTable().destroy();
fill_datatable(filter_field, filter_country, tags);
}
else
{
alert('you have to fill 3 option to search');
$('#user_data').DataTable().destroy();
fill_datatable();
}
});
});
</script>
关于更多信息,这里是 fetch.php,它过滤和接收来自 DB 的数据:
<?php
////fetch_rule
include('database_connection.php');
$column = array('email', 'password', 'firstname', 'lastname', 'field', 'country');
$query = "
SELECT * FROM csv
";
if(isset($_POST['filter_field'], $_POST['filter_country']) && $_POST['filter_field'] != '' && $_POST['filter_country'] != '' && $_POST['tags'] != '')
{
$query .= '
WHERE email LIKE "%'.$_POST['tags'].'%" AND field = "'.$_POST['filter_field'].'" AND Country = "'.$_POST['filter_country'].'"
';
}
if(isset($_POST['order']))
{
$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY email ASC ';
}
$query1 = '';
if($_POST["length"] != -1)
{
$query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$number_filter_row = $statement->rowCount();
$statement = $connect->prepare($query . $query1);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row['email'];
$sub_array[] = $row['password'];
$sub_array[] = $row['firstname'];
$sub_array[] = $row['lastname'];
$sub_array[] = $row['field'];
$sub_array[] = $row['country'];
$data[] = $sub_array;
}
function count_all_data($connect)
{
$query = "SELECT * FROM csv";
$statement = $connect->prepare($query);
$statement->execute();
return $statement->rowCount();
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => count_all_data($connect),
"recordsFiltered" => $number_filter_row,
"data" => $data
);
echo json_encode($output);
//////
?>
【问题讨论】:
标签: javascript php html jquery mysql