gpt4 book ai didi

PHP - 导出 CSV 函数为每个条目重复标题

转载 作者:行者123 更新时间:2023-11-29 16:42:35 29 4
gpt4 key购买 nike

我们正在创建一个将数据从表导出到 CSV 的函数。然而,代码似乎重复了我们为插入 CSV 的每一行设置的标题,因此它最终看起来像这样:

csv data (the red lines are blocked out data but you can see how the headers repeat for each row)

这是我们创建导出的代码:

//inserts data into import table
$sql = "INSERT into import (suid, studentName, studentEmail, studentAffiliation, studentProgram, studentEduLevel) values ('$suid', '$studentName', '$studentEmail', '$studentAffiliation', '$studentProgram', '$studentEduLevel')";
if (!$fail) {

if (mysqli_multi_query($csvDatabase, $sql)) {
//once imported properly, export csv

$query = "SELECT suid, studentName, studentEmail, studentAffiliation, studentProgram, studentEduLevel from import ORDER BY id DESC LIMIT {$successCount}";
$result = mysqli_query($csvDatabase, $query);
if ($result->num_rows > 0) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data-export.csv');
$output = fopen("php://output", "w");
$headers = array('SUID', 'Student Name', 'Student Email', 'Student Affiliation', 'studentProgram', 'Student Edu Level');
fputcsv($output, $headers);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
//then delete records in database
$deletesql = "DELETE FROM import ORDER BY id DESC LIMIT {$successCount}";
if (mysqli_query($csvDatabase, $deletesql)) {
//echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($csvDatabase);
}

}


} else {
echo "Error: " . $sql . "<br>" . mysqli_error($csvDatabase);
}
}

关于重复标题的代码中可能发生的情况有什么想法吗?

编辑:省略服务器信息的完整代码:

<?php
require_once('connection.php');

$successCount = 0;

if(isset($_POST['submit'])){

$filename = $_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0) {
for($i=0; $i<count($filename); $i++) {
$file = $filename[$i];
//open file in read only
$files = fopen($file, "r");
//skips first line
fgets($files);
//get data from csv & uses comma to find separate values
while (($getData = fgetcsv($files, 0, ",")) !== FALSE)
{
$fail = FALSE;
//store SUID from 2nd line in csv
$suid = $getData[0];
if (strlen($suid) === 9 && ctype_digit($suid) ) {
// start ldap look up
$server="***";
$basedn="***";

//Connect to server
$ds=ldap_connect($server);

if ($ds) {
//bind with our special account that retrieves more attributes
$ldaprdn = '***'; // ldap rdn or dn
$ldappass = '***'; // associated password
$r=ldap_bind($ds,$ldaprdn,$ldappass); // this is an authenticated bind
if (substr($suid, 0, 1) === ";" || is_numeric($suid)) {
if ($r) {
//filter to all objectclasses that the SUID we are looking for
$filter = "(&(objectClass=*)(syrEduSUID={$suid}))";

//We are only interested in retrieving these attributes
$justthese = array("displayName", "syrEduLevel", "syrEduProgramDesc", "syrEduProgram", "mail", "eduPersonPrimaryAffiliation", "eduPersonAffiliation" );

// Search SUID
$sr=ldap_search($ds, $basedn, $filter, $justthese );
//Need to test if the search succeeded. FALSE value means it failed
//if ($sr!==FALSE) {
//Search found something. Now return Attributes and their values - note, there can be multiple values per attribute. We need to make sure the search only returned one result

$entry = ldap_get_entries($ds, $sr);
// if we have only one result, return the values, if not, we have a problem

if ($entry["count"] == 1) {
// get student name and email from suid

$studentName = mysqli_real_escape_string($csvDatabase, $entry[0]['displayname'][0]);
$studentEmail = mysqli_real_escape_string($csvDatabase, $entry[0]['mail'][0]);
$studentAffiliation = mysqli_real_escape_string($csvDatabase, $entry[0]['edupersonprimaryaffiliation'][0]);
$studentProgram = mysqli_real_escape_string($csvDatabase, $entry[0]['syreduprogramdesc'][0]);
$studentEduLevel = mysqli_real_escape_string($csvDatabase, $entry[0]['syredulevel'][0]);

$successCount++;

// close ldap
ldap_close($ds);

} else {
$msg = "Ldap search returned 0 or more than one result";
$fail = TRUE;
}

//} else {
// $msg = "Search failed";
// $fail = TRUE;
//}
}
} else {
$msg = "Bind failed";
$fail = TRUE;
}
} else {
$msg = "LDAP connection failed";
$fail = TRUE;
}

//inserts data into import table
$sql = "INSERT into import (suid, studentName, studentEmail, studentAffiliation, studentProgram, studentEduLevel) values ('$suid', '$studentName', '$studentEmail', '$studentAffiliation', '$studentProgram', '$studentEduLevel')";
if (!$fail) {

if (mysqli_multi_query($csvDatabase, $sql)) {
//once imported properly, export csv

$query = "SELECT suid, studentName, studentEmail, studentAffiliation, studentProgram, studentEduLevel from import ORDER BY id DESC LIMIT {$successCount}";
$result = mysqli_query($csvDatabase, $query);
if ($result->num_rows > 0) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data-export.csv');
$output = fopen("php://output", "w");
$headers = array('SUID', 'Student Name', 'Student Email', 'Student Affiliation', 'studentProgram', 'Student Edu Level');
fputcsv($output, $headers);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
//then delete records in database
$deletesql = "DELETE FROM import ORDER BY id DESC LIMIT {$successCount}";
if (mysqli_query($csvDatabase, $deletesql)) {
//echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($csvDatabase);
}

}


} else {
echo "Error: " . $sql . "<br>" . mysqli_error($csvDatabase);
}
}
}
}

//closes file
fclose($files);
}
} else {
echo "You did not upload a CSV file or the CSV file is blank.";
}
} else {
?>

最佳答案

正如 IdontDownVote 所指出的,代码被放置在每个文件循环内。一旦取出并放置在 fclose($files) 之后,它就不会重复标题。

关于PHP - 导出 CSV 函数为每个条目重复标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53270482/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com