从php中以xls格式下载多个sql查询显示的数据

时间:2015-01-06 15:19:33

标签: php

我有一个包含三列的脚本:

  • id
  • 姓名
  • 金额

它从两个查询中获取数据。我想使用php将html表中显示的数据下载到xls格式。我正在尝试实现下载脚本,但我遗漏了一些东西,这是我的下载代码脚本。它由一个完整的页面组成,其中数据显示在html tabe中,之后它要求下载。

<table border="2">

<thead>

    <tr>
        <th>Member ID</th>
        <th>Member Name</th>
        <th>Amount</th>
        

    </tr>
<?php
include('db.php');

//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

$d_f   = "2014-11-01";
$d_to  = "2015-01-05";
$fid   = "PNQ001";

//select table to export the data
$select_table=mysql_query("select member.id,login.name from member inner join login on member.id=login.id where member.f_id='$fid' order by member.id");
$rows = mysql_fetch_assoc($select_table);

//if ($rows)
//{
//getcsv(array_keys($rows));
//}
while($rows)
{
//getcsv($rows);
$rows = mysql_fetch_assoc($select_table);

?>


    <tr>
        <td class="center"><?php echo $rows['id'];?></td>
        <td><?php echo ucfirst($rows['name']);?></td>
        <td>
        <?php 
        
        $mid   = $rows['id'];
        $CountMoney=mysql_query("select * from details where f_id='$fid' and m_id='$mid' and date between '$d_f' and '$d_to'");
        $rows1 = mysql_fetch_assoc($CountMoney);
        $point = mysql_query("select plan.amount,plan.name from plan inner join member on member.plan_code=plan.id where member.id='$mid'");
        $get_points  = mysql_fetch_array($point);
        $totals=0;
        while($row2=  mysql_fetch_assoc($CountMoney))
        {
            $totals= $row2['a_mail']+$totals;
        }
        echo $totals*$get_points['amount'];

        
        ?>
        </td>
       


    </tr>



<?php
}

// get total number of fields present in the database
//function getcsv($no_of_field_names)
//{
//$separate = '';
//
//
//// do the action for all field names as field name
//foreach ($no_of_field_names as $field_name)
//{
//if (preg_match('/\\r|\\n|,|"/', $field_name))
//{
//$field_name = '' . str_replace('', $field_name) . '';
//}
//echo $separate . $field_name;
//
////sepearte with the comma
//$separate = ',';
//}
//
////make new row and line
//echo "\r\n";
//}
?>
</tbody>
</table>

0 个答案:

没有答案