尝试导出为CSV

时间:2019-01-17 13:18:11

标签: php csv export

我通过php导出文件。当我导出文件时,它不会影响export.php中$_POST["program"]的值 它只是获取第一个sql并将其导出。

  

funtcions2.php

<?php
 include 'config2.php';
  if(isset($_POST["Export"])){

  if($_POST["program"]="dengue"){
      // get records from database
  $query = $db->query("SELECT * FROM lcho_dengue_activities ");

  if($query->num_rows > 0){
  $delimiter = ",";
  $filename = "dengueactivities" . date('Y-m-d') . ".csv";

 //create a file pointer
  $f = fopen('php://memory', 'w');


 //output each row of the data, format =line as csv and write to file 
 pointer
 while($row = $query->fetch_assoc()){

    $lineData = array( $row['district_id'], $row['barangay_id'], 
     $row['month'], $row['year'],$row['dengue_ind1'],$row['dengue_ind2'],
     $row['dengue_ind3'],$row['dengue_ind4'],$row['dengue_ind5'],
    $row['dengue_ind6'],$row['dengue_ind7'],$row['dengue_ind8'],
   $row['dengue_ind9'],$row['dengue_ind10'],$row['dengue_ind11']);

    fputcsv($f, $lineData, $delimiter);
}

//move back to beginning of file
fseek($f, 0);

//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');

//output all remaining data on a file pointer
fpassthru($f);
    }
  exit;
    }
 elseif($_POST["program"]=="mproblem"){
  //get records from database
 $query = $db->query("SELECT * FROM maternalproblem ");

 if($query->num_rows > 0){
$delimiter = ",";
$filename = "maternalproblem" . date('Y-m-d') . ".csv";

//create a file pointer
$f = fopen('php://memory', 'w');


//output each row of the data, format =line as csv and write to file 
 pointer
while($row = $query->fetch_assoc()){

    $lineData = array($row['district_id'], $row['barangay_id'], 
   $row['PID'], $row['tuberculosis'],$row['sakit'],$row['diyabetes'],
   $row['hika'],$row['bisyo']);

    fputcsv($f, $lineData, $delimiter);
}

//move back to beginning of file
fseek($f, 0);

//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');

//output all remaining data on a file pointer
fpassthru($f);
    }
  exit;
   }  

 }
   ?>
  

export.php

<form class="form-horizontal" action="functions2.php" method="post" 
 name="upload_excel"   
                  enctype="multipart/form-data">
           <div class="form-group">
               <div class="col-md-4">
        <label for="prog"><b>Program</b></label>
        <select name="program">
        <option value="dengue">Dengue Activites</option>      
        <option value="mproblem">Maternal Problem</option>   


        </select></br>
        </div>
           </div>
              <div class="form-group">
                        <div class="col-md-4 col-md-offset-4">
                            <input type="submit" id="submit" name="Export" 
       class="btn btn-success" value="export to excel"/>
                        </div>
               </div>                    
        </form>           

0 个答案:

没有答案