2015-10-13 58 views
0

對於一個項目,我必須導出一個報告給我的文件location.It必須是.csv文件格式,需要包含標題從php表導出到.csv,而不創建新的sql表

<html> 
    <?php 
    $profiles = new CGenRs("SELECT * from adapt_profile ", $database); 
    $profiles->first(); 
    $users = new CGenRs("SELECT * from authuser where userno in (select user_number from adapt_profile_time where exported = 'f')", $database); 
    $users->first(); 
    $logtime = new CGenRs("SELECT time_id, 
    profile_id , 
    user_number , 
    start_time , 
    end_time, 
    description, 
    exported, EXTRACT(hour FROM(end_time - start_time)) as diff from adapt_profile_time where exported = 'f' order by user_number", $database); 
    $logtime->first(); 
    $timestamp = new CGenRS("SELECT EXTRACT(minute FROM(end_time - start_time))as difference FROM adapt_profile_time WHERE exported='f'", $database); 
    $timestamp->first(); 

    ?> 

    <table width="100%" > 
     <tr> 
      <th align="left">USER NAME</th> 
      <th align="left">WORKED FROM</th> 
      <th align="left">WORKED TO</th> 
      <th align="left">TOTAL</th> 
      <th align="left">FOR PROFILE</th> 
      <th align="left">DESCRIPTION</th> 

    </tr> 

    <?php 
    $curr_userno = $logtime->valueof('user_number'); 
    $tot_mins = 0; 
    while (!$logtime->eof()) { 
     while (!$timestamp->eof()) { 


      if ($curr_userno != $logtime->valueof('user_number')) { 
       $total_time = floor($tot_mins/60) . " hours " . ($tot_mins % 60)." minutes" 
       ?> 
       <tr> 

        <td></td> 
        <td></td> 
        <td align="right"><b>Total =</b></td> 
        <td colspan="8"><b><?php echo $total_time; ?></b></td> 
        <td></td> 
        <td></td> 

       </tr> 

       <?php 
       $curr_userno = $logtime->valueof('user_number'); 
       $tot_mins = 0; 
      } 
      $tot_mins = ($tot_mins + $logtime->valueof('diff') * 60) + $timestamp->valueof('difference'); 
      ?> 
      <tr> 
       <td> 
        <?php 
        while (!$users->eof()) { 
         if ($users->valueof('userno') == $logtime->valueof('user_number')) { 
          echo $users->valueof('auth_name') . ' ' . $users->valueof('auth_surname'); 

          $users->first(); 
          break; 
         } 
         $users->next(); 
        } 
        ?> 
       </td> 

       <td><?php echo $logtime->valueof('start_time') ?></td> 
       <td><?php echo $logtime->valueof('end_time') ?></td> 
       <td><?php echo $logtime->valueof('diff') . " " . "hours" . " " . $timestamp->valueof('difference') . " " . "minutes"; ?></td> 
       <td> 
        <?php 
        while (!$profiles->eof()) { 
         if ($profiles->valueof('profile_id') == $logtime->valueof('profile_id')) { 
          echo $profiles->valueof('profile_name'); 
          $profiles->first(); 
          break; 
         } 
         $profiles->next(); 
        } 
        ?> 
       </td> 
       <td><?php echo $logtime->valueof('description') ?></td> 
      </tr> 

      <?php 
      $timestamp->next(); 
      $logtime->next(); 
     } 
     echo "<hr></hr>"; 
    } 
    $total_time = floor($tot_mins/60) . " hours " . ($tot_mins % 60)." minutes"; 
    ?> 
    <tr> 
     <td> </td> 
     <td></td> 
     <td align="right"><b>Total =</b></td> 
     <td colspan="8"><b><?php echo $total_time; ?></b></td> 
     <td></td> 
     <td></td> 

    </tr> 


    <?php 
    ?> 
    <tr> 
     <td colspan="8"><hr></hr></td> 
    </tr> 
</table> 
    <form id="submit_form" action="" method="post" name="sbt_frm" align="center"> 
     <input align="center" id="sbt_btn" type="submit" value="Export" name="sbt_btn"></input> 
     </form> 
</html> 

以上是我從上一頁輸入工作時間和日期生成的報表。當我按下導出按鈕時,我希望將底部表單導出到.csv中的頁面/報告爲「location/x」。我該如何解決這個問題。使用PostgreSQL 我只需要知道使用什麼查詢或命令或可考慮,將做進一步的研究,然後上傳反饋

回答

0

這可能是一個良好的開端(使用使用Javascript/JQuery的),改編自援引消息人士在演示代碼內:

JSFiddle demo here注意:提琴版本可能無法在IE瀏覽器中運行 - 請嘗試使用FF或Chrome。

這裏是我如何提取表數據爲使用JavaScript字符串:

var $rows = $table.find('tr:has(td)'), 

// Temporary delimiter characters to avoid accidentally splitting the actual contents 
tmpColDelim = String.fromCharCode(11), // vertical tab character 
tmpRowDelim = String.fromCharCode(0), // null character 

// actual delimiter characters for CSV format 
colDelim = ',', 
rowDelim = '\r\n', 

// Grab text from table into CSV formatted string 
csv = $rows.map(function(i, row) { 
    var $row = $(row), 
    $cols = $row.find('td'); 

    return $cols.map(function(j, col) { 
    var $col = $(col), 
     text = '="' + $col.text() + '"'; // to make dates interpret literally in excel 
    return text.replace(/"/g, '"'); // escape double quotes 

    }).get().join(tmpColDelim); 

}).get().join(tmpRowDelim) 
.split(tmpRowDelim).join(rowDelim) 
.split(tmpColDelim).join(colDelim); 

IE的行爲與其他瀏覽器不同:

// IE9+ support - using iframe to set up file 
// https://github.com/angular-ui/ui-grid/issues/2312#issuecomment-70348120 
// note: if using IE10+, consider ieblob: https://github.com/mholt/PapaParse/issues/175#issuecomment-75597039 

if (msieversion()) { 
    var frame = document.createElement('iframe'); 
    document.body.appendChild(frame); 

    frame.contentWindow.document.open("text/html", "replace"); 
    frame.contentWindow.document.write('sep=,\r\n' + csv); 
    frame.contentWindow.document.close(); 
    frame.contentWindow.focus(); 
    frame.contentWindow.document.execCommand('SaveAs', true, fileName); 

    document.body.removeChild(frame); 
    return true; 

} else { 
    var uri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); 
    $(this).attr({ 
    'download': fileName, 
    'href': uri, 
    'target': '_blank' 
    }); 
} 

這些都只是片段,詳見完整的jsfiddle。

我修改了註釋掉的代碼引用以使用我的表格 - 我認爲如果您想要執行此客戶端任何操作,表格到字符串位將幫助您。