Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php表导出为.csv,而不创建新的sql表_Php_Html_Sql_Postgresql_Export To Csv - Fatal编程技术网

从php表导出为.csv,而不创建新的sql表

从php表导出为.csv,而不创建新的sql表,php,html,sql,postgresql,export-to-csv,Php,Html,Sql,Postgresql,Export To Csv,对于一个项目,我必须将我制作的报告导出到一个文件位置。它必须是.csv文件格式,并且需要包含标题 <html> <?php $profiles = new CGenRs("SELECT * from adapt_profile ", $database); $profiles->first(); $users = new CGenRs("SELECT * from authuser where userno in (select user_

对于一个项目,我必须将我制作的报告导出到一个文件位置。它必须是.csv文件格式,并且需要包含标题

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

这可能是一个好的开始(使用Javascript/JQuery),改编自演示代码中引用的源代码:

JSFiddle演示注意:提琴版可能无法在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'
  });
}
这些只是片段,请参阅完整的JSFIDLE以了解详细信息

我修改了注释掉的代码引用来处理我的表-我认为表到字符串位会对您有所帮助,如果您想在客户端执行任何操作的话