gpt4 book ai didi

php - 将整数解释为字符串 csv

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

我发现这段代码将 SQL 表输出到 CSV 中,效果很好,但在我从中提取的表中,我们使用整数来表示两个不同的值在两个不同的列中,例如,'0'A 列 中应读作 'All',而 '0' 在code> 在B 列 中应读作'New'。我无法弄清楚如何将 0 解释为两个不同的事物,因为我不能只对所有值说 '0 = X' 。任何见解将不胜感激:

    <?php
session_start();
if (isset($_POST['export']) && !empty($_POST['export'])){
if($_POST['export'] == 'safefilter') {

$colnames = [
'team' => "Team",
'name' => "INI",
'inidesc' => "INI Description",
'item' => "Item",
'itemdesc' => "Item Description",
'newonly' => "New Only?",
];

$filter = $_POST['filter'];
$name = $filter . 'StandardFiles';

if (in_array($filter, $_SESSION['teamarray'], true)) {

$sql = "SELECT DISTINCT teams.team, safecheck.name, safecheck.inidesc, safecheck.item, safecheck.itemdesc, safecheck.newonly FROM safecheck INNER JOIN teams ON safecheck.teamid=teams.id WHERE team = '$filter' ORDER BY name ASC, team ASC, item ASC;";

} elseif (in_array(strtoupper($filter), $_SESSION['iniarray'], true)) {

$sql = "SELECT DISTINCT teams.team, safecheck.name, safecheck.inidesc, safecheck.item, safecheck.itemdesc, safecheck.newonly FROM safecheck INNER JOIN teams ON safecheck.teamid=teams.id WHERE name = '$filter' ORDER BY name ASC, team ASC, item ASC;";

}

} elseif($_POST['export'] == 'authfilter') {

$colnames = [
'team' => "Team",
'name' => "INI",
'fullini' => "INI Description",
];

$filter = $_POST['filter'];
$name = $filter . 'AuthorizingTeams';

if (in_array($filter, $_SESSION['teamarray'], true)) {

$sql = "SELECT DISTINCT teams.team, authcheck.name, authcheck.fullini FROM authcheck INNER JOIN teams ON authcheck.teamid=teams.id WHERE team = '$filter' ORDER BY name ASC, team ASC;";

} elseif (in_array(strtoupper($filter), $_SESSION['iniarray'], true)) {

$sql = "SELECT DISTINCT teams.team, authcheck.name, authcheck.fullini FROM authcheck INNER JOIN teams ON authcheck.teamid=teams.id WHERE name = '$filter' ORDER BY name ASC, team ASC;";

}

} elseif($_POST['export'] == 'safeview') {

$colnames = [
'team' => "Team",
'name' => "INI",
'inidesc' => "INI Description",
'item' => "Item",
'itemdesc' => "Item Description",
'newonly' => "New Only?",
];

$name = 'AllStandardFiles';
$sql = "SELECT DISTINCT teams.team, safecheck.name, safecheck.inidesc, safecheck.item, safecheck.itemdesc, safecheck.newonly FROM safecheck INNER JOIN teams ON safecheck.teamid=teams.id ORDER BY name ASC, team ASC, item ASC";

} elseif($_POST['export'] == 'auth') {

$colnames = [
'team' => "Team",
'name' => "INI",
'fullini' => "INI Description",
];

$name = 'AllAuthorizingTeams';
$sql = "SELECT DISTINCT teams.team, authcheck.name, authcheck.fullini FROM authcheck INNER JOIN teams ON authcheck.teamid=teams.id ORDER BY name ASC, team ASC;";

}
}

function cleanData(&$str) {
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if(strstr($str, "'")) $str = "'" . str_replace("'", '', $str) . "'";
}

function map_colnames($input) {
global $colnames;
return isset($colnames[$input]) ? $colnames[$input] : $input;
}

// filename for download
$filename = $name . date('mdY') . ".csv";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv");

$out = fopen("php://output", 'w');

$flag = false;
include('dbconnect.php');
$result = mysqli_query($conn, $sql) or die('Query failed!');

while($row = mysqli_fetch_assoc($result)) {
if(!$flag) {
// display field/column names as first row
$firstline = array_map(__NAMESPACE__ . '\map_colnames', array_keys($row));
fputcsv($out, $firstline, ',', '"');
$flag = true;
}
array_walk($row, __NAMESPACE__ . '\cleanData');
fputcsv($out, array_values($row), ',', '"');
}

fclose($out);
exit;
?>

最佳答案

在您的 sql 语句中对这些列执行以下操作:

SELECT IF(column_name_A = 0, 'All', column_name_A) FROM your_table

SELECT IF(column_name_B = 0, 'New', column_name_B) FROM your_table

关于php - 将整数解释为字符串 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45243087/

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