将整数解释为字符串csv

时间:2017-07-21 17:06:02

标签: php mysql csv export

我发现此代码将SQL表格输出到CSV,这样可以正常工作,但在表格中,我们使用整数来表示两个不同列中的两个不同值例如,'0'应在'All'中显示为column A,而'0'应在'New'中显示为Column B。我无法弄清楚如何将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;
?>

1 个答案:

答案 0 :(得分:1)

在您的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