从PHP导出格式化文件TXT

时间:2015-03-31 08:42:09

标签: php oracle

所以我有像这样的工作代码

<html>
<head>
<title> COBA </title>
<script language="javascript" type="text/javascript">
function urlorder(a)
{
    var urlorder="order.php?on="+a;
    var prmpt=window.open(urlorder,"test","left=200,top=150,scrollbars=yes,resizable=no,width=640,height=480");
}
function printorder(a,b)
{
    var urlorder="printorder.php?on="+a+"&nu=2&tgl="+b;
    //var prmpt=window.open(urlorder,"test","left=200,top=150,scrollbars=yes,resizable=no,width=640,height=480");
    window.location = urlorder;
    //alert (urlorder);
}
</script>
</head>
<body> 

<?
//include "ceksession.php";
include 'connect.php';

//ambil tgl val
$q = "select tgl_val from para_info_kntr";
$s = OCIParse($c,$q);
OCIBindByName($s,":bind1",$ltid);
OCIExecute($s,OCI_DEFAULT);
while (OCIFetch($s))
{   
    $tgl=ociresult($s,"TGL_VAL");
    echo $tgl."<br>";
}
$tgl= date('d-m-Y', strtotime($tgl));
$tgl= date('d-m-Y'); 

$tgl = $_GET["tgl"];

echo "<div align=\"right\">";
echo "<input type=\"button\" onclick=\"window.location='month.php'\") value=\"HOME\">";
echo "</div>";
echo "<div align=\"center\">";
echo "<table border=\"1\">";
echo "<tr><td colspan=\"44\">";
echo "<div align=\"center\">PAYMENT LIST</div>";
echo "</td></tr>";

$q = "Select AMOUNT, SERVICES, BILL_NO, ORDER_NUMBER, TOTAL_AMOUNT, to_char(PAY_DATE,'YYYY-MM-DD HH:MI:SS') as PAY_DATE from payment_final where bt_number=".$bt_number." and to_char(tgl_val,'DD-MM-YYYY')='".$tgl."' order by bill_no";

$s2=OCIParse($c_slave,$q);
OCIBindByName($s2,":bind1",$ordernumber);
OCIExecute($s2,OCI_DEFAULT);

    echo "<tr><td>";
    echo "PAY_DATE";
    echo "</td><td>";
    echo "ORDER_NUMBER";
    echo "</td><td>";
    echo "AMOUNT";
    echo "</td><td>";
    echo "SERVICES";
    echo "</td><td>";
    echo "BILL_NO";
    echo "</td></tr>"; 

while (OCIFetch($s2))
{
    $V_GUEST_FOLIO        = oci_result($s2,'GUEST_FOLIO');
    $V_PAY_DATE           = oci_result($s2,'PAY_DATE');
    $V_ORDER_NUMBER       = oci_result($s2,'ORDER_NUMBER'); 
    $V_AMOUNT             = oci_result($s2,'AMOUNT'); 
    $V_SERVICES           = oci_result($s2,'SERVICES');
    $V_BILL_NO            = oci_result($s2,'BILL_NO');  

    echo "</td><td>"; 

    echo "<a href=\"order.php?on=".$V_ORDER_NUMBER."\">";
    echo $V_PAY_DATE;
    echo "</a>";
    echo "</td><td>"; 
    echo $V_ORDER_NUMBER;
    echo "</td><td>";
    echo $V_AMOUNT;
    echo "</td><td>";
    echo $V_SERVICES;  
    echo "</td><td>";
    echo $V_BILL_NO;  
    echo "</td><td>";
    echo "<input type=\"button\" onclick=\"printorder(".$V_ORDER_NUMBER.",'".$tgl."')\") value=\"PRINT\">";
    echo "</td></tr>";

}
?>
</body>
</html>

结果如下: enter image description here

现在,我想添加“保存文件”按钮。按下按钮时,它将导出文本(txt)文件。文本文件的内容应该来自查询(网页表格中显示的内容),格式如下:

PAY_DATE            |ORDER_NUMBER   |   AMOUNT  |   SERVICES|   BILL_NO     
02/01/2015 08:35:58 |298479         |   130200  |   6510    |   RS01022015001
02/01/2015 08:40:43 |298485         |   25800   |   1290    |   RS01022015002
02/01/2015 08:41:04 |298480         |   239600  |   11980   |   RS01022015003
02/01/2015 08:44:37 |298484         |   269400  |   13470   |   RS01022015004
02/01/2015 08:48:18 |298482         |   286400  |   14320   |   RS01022015005
02/01/2015 09:09:11 |298490         |   417000  |   20850   |   RS01022015006
02/01/2015 09:09:16 |298492         |   49600   |   2480    |   RS01022015007
02/01/2015 09:18:45 |298499         |   119200  |   5960    |   RS01022015011
... until end of file.

你们能帮助我吗?

1 个答案:

答案 0 :(得分:0)

有几种选择。 1)不熟悉OCI(oracle?),但也许有一个类似于mysql&#34; INTO OUTFILE&#34;的导出功能。

2)你可以通过javascript获取html一些fgrep magic

3)与现在相同,但不是使用...... 我认为你的样本中的填充是非常尴尬的 - 我实际上会忽略它。

//first headers
$headers = array();
$headers[] = str_pad("PAY_DATE", 20);
$headers[] = str_pad("ORDER_NUMBER", 16);
//etc.
$headers = implode('|', $headers);

//data
$rows = array();
while (OCIFetch($s2))
{
    //this is really similar to what you do
    $V_PAY_DATE = oci_result($s2,'PAY_DATE');
    $V_ORDER_NUMBER = oci_result($s2,'ORDER_NUMBER');
    //but this instead (follow pattern from header-section)
    $datarow = array();
    $datarow[] = str_pad($V_PAY_DATE, 20);
    $datarow[] = str_pad($V_ORDER_NUMBER, 16);
    $rows[] = implode('|', $datarow);
}

$data = $headers . "\n" . implode("\n", $rows);
file_put_contents('/path/to/file.txt', $data);

小心 - 未经测试 - 我打字就像一只猴子,所以它可能充满了拼写错误和语法错误......