2016-10-10 68 views
2

的index.phpCSV文件輸出基地

我想導出基於選定本月

<div id="page-wrapper"> 

     <div class="container-fluid"> 


    <h2 class="form-signin-heading">Schedule Management</h2><hr /> 
    <button class="btn btn-info" type="button" id="btn-add"> <span class="glyphicon glyphicon-pencil"></span> &nbsp; Add Schedule</button> 
    <button class="btn btn-info" type="button" id="btn-view"> <span class="glyphicon glyphicon-eye-open"></span> &nbsp; View Schedule</button> 
    <hr /> 

    <div class="content-loader"> 
      <form method="post" action="index.php"> 
    Select a month <select name="months" onchange ='this.form.submit()'> 
    <option value="0"> </option> 
    <option value="1">January</option> 
    <option value="2">February</option> 
    <option value="3">March</option> 
    <option value="4">April</option> 
    <option value="5">May</option> 
    <option value="6">June</option> 
    <option value="7">July</option> 
    <option value="8">August</option> 
    <option value="9">September</option> 
    <option value="10">October</option> 
    <option value="11">November</option> 
    <option value="12">December</option> 
</select> 
<noscript><input type="submit" value="Submit"></noscript> 

</form> 
    <table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive"> 

    <thead> 
    <tr> 
    <th>Sched #</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>Check In Date</th> 
    <th>Check Out Date</th> 
    <th>Room Rate</th> 
    <th>Reservation Fee</th> 
    <th>Date Paid</th> 
    <th>Mode of Paymnet</th> 
    <th>Status</th> 
    <th>edit</th> 
    <th>delete</th> 

    </tr> 
    </thead> 
    <tbody> 
    <?php 
    require_once 'dbconfig.php'; 

    if(isset($_POST['months'])){ $months = $_POST['months']; }else { $months='';} 
    $stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) = '".$months."' "); 
    //this is my solution to filter my table data base on the selected months in the index.php table 

    $stmt->execute(); 
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
    { 
     ?> 
     <tr> 
     <td><?php echo $row['id']; ?></td> 
     <td><?php echo $row['fname']; ?></td> 
     <td><?php echo $row['lname']; ?></td> 
     <td><?php echo $row['checkin']; ?></td> 
     <td><?php echo $row['checkout']; ?></td> 
     <td><?php echo $row['rrate']; ?></td> 
     <td><?php echo $row['reservefee']; ?></td> 
     <td><?php echo $row['datepaid']; ?></td> 
     <td><?php echo $row['modepayment']; ?></td> 
     <td><?php echo $row['stats']; ?></td> 
     <td align="center"> 
     <a id="<?php echo $row['id']; ?>" class="edit-link" href="#" title="Edit"> 
     <img src="edit.png" width="20px" /> 
     </a></td> 
     <td align="center"><a id="<?php echo $row['id']; ?>" class="delete-link" href="#" title="Delete"> 
     <img src="delete.png" width="20px" /> 
     </a></td> 
     </tr> 
     <?php 
    } 
    ?> 
    </tbody> 
    </table> 
    <a class="btn" href="export.php">Export</a> 
    </div> 

</div> 

<br /> 

<div class="container"> 



</div> 

export.php

出口按鈕工作的數據,但我想過濾我的數據就像在我的表中index.php基於選擇的月份......但現在我只能執行時,我點擊導出按鈕我的數據庫上的整個數據獲取輸出到.csv

<?php 
    require_once 'dbconfig.php'; 


header('Content-Type: text/csv'); 
header('Content-Disposition: attachment;filename=exported-data.csv'); 

$stmt = $db_con->prepare("SELECT * FROM tblguest "); 

$stmt->execute(); 


$filename = date('d.m.Y').'.csv'; 

$data = fopen($filename, 'w'); 


    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
$csv = implode(',', $row) . "\n"; 
fwrite($data, $csv); 
print_r($csv); 
} 

echo "\r\n"; 

fclose($data); 

?> 
+0

在選擇任何選項時將參數添加到URL。通過這種方式,您可以根據城市進行鏈接,並且可以使用$ _GET參數或您的應用程序方式獲取參數。 –

+0

請大家看看,我寫了答案,如果你覺得很好,那麼你可以把它標記爲已接受並且投票贊成。 –

回答

0

您可以使用下面的代碼。您首先需要根據您的情況選擇數據庫。

在HTML中設置選擇下拉按鈕,並在Get請求中使用Day參數。

<label>Day</label> 
    <select id='lead_ids' name='lead_ids' onchange="location = this.value;"> 
     <option value=""> ... </option> 
     <option value="search"> All </option> 
     <option value="search?day=Monday">{{ Monday }}</option> 
     <option value="search?day=Tuesday">{{ Tuesday }}</option> 
    </select> 

這樣你就可以根據城市,使鏈接,你可以使用$ _GET參數,或者您的應用程序的方式獲取參數。

$q = "SELECT * FROM tblguest"; 
    if(!empty($_GET('day'))) { 
     $q .= ' WHERE day= ' . $_GET('day'); 
    } 
    $stmt = $db_con->prepare($q); 

    $stmt->execute(); 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
     $result[] = $row; 
    } 
    $file_name = "downloads/".date('dmY_His').".csv"; 
    // call the csv export 
    exportToCsv($result, $file_name); 

這樣你就可以用不同的參數添加多個條件。

調用此函數將其導出爲CSV。

function exportToCsv($array, $file_name = "export.csv", $delimiter=";") { 
     $f = fopen($file_name, "w"); 
     $head = array_keys($array[0]); 

     fputcsv($f, $head); 
     foreach ($array as $line) { 
      fputcsv($f, $line); 
     } 

     if (file_exists($file_name)) 
     { 
      header('Content-Description: File Transfer'); 
      header('Content-Type: text/csv'); 
      header('Content-Disposition: attachment; filename='.basename($file_name)); 
      header('Expires: 0'); 
      header('Cache-Control: must-revalidate'); 
      header('Pragma: public'); 
      header('Content-Length: ' . filesize($file_name)); 
      ob_clean(); 
      flush(); 
      readfile($file_name); 
      exit; 
     } 
    } 

如果您有答案,請將答案標記爲「已接受」。