2017-07-21 15 views
0

このコードではCSVにテーブルSQLを出力しましたが、これはうまくいきますが、私は2つの異なる値、例えば'0'の2つの異なる値を表すために整数を使用しています。 column A'All'と、はに'New'と表示されます。私は0を2つの異なるものとして解釈する方法を考え出すのに問題があります。すべての値に対して'0 = X'と言うことはできません。任意の洞察力をいただければ幸いです:あなたのSQL文で整数を文字列として解釈する

<?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

は、それらの列のために次の操作を行います

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 
+0

本当にありがとうございました!素晴らしい仕事をした。 –

関連する問題