在php中将数据导出到csv文件中

在php中将数据导出到csv文件中,php,mysql,Php,Mysql,我用php制作了一个页面,在不同的列中显示一些用户的数据,如姓名、电子邮件、地址等。这些数据来自数据库。现在我想导出.csv文件中的所有信息。是否有任何php解决方案。 下面是在页面上显示数据的代码 <table width='98%' align="center" id='Categories_Main' cellspacing='0' cellpadding='0' class="table table-striped"> &l

我用php制作了一个页面,在不同的列中显示一些用户的数据,如姓名、电子邮件、地址等。这些数据来自数据库。现在我想导出.csv文件中的所有信息。是否有任何php解决方案。
下面是在页面上显示数据的代码

<table  width='98%' align="center" id='Categories_Main' cellspacing='0' cellpadding='0'  class="table table-striped">            
            <tr height="25">
              <th class="cat_list" colspan="4"><br /><?php echo $this->db->get_modules($cid);?><br /><br /></th>
              <th></th>
            </tr>
            <tr>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Gender </a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Name</a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Adress</a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Email</a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Number</a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Course</a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Price</a></th>
              <th width="150" style="min-width:55px" class="nowrap center"><a   class="hasTip">Action</a></th>
           </tr>
              <?php

                $sql = "SELECT * FROM 
                            prospective_request  

                            ORDER BY name ASC limit $st,$limit";        

                $this->db->query($sql);
                while($this->db->fetch_array())
                {
              ?>


                  <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['user_title'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>
                        <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['name'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>
                        <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['address'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>
                        <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['email'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>
                        <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['number'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>
                        <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['course'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>
                        <th class="nowrap has-context" width="200" style="min-width:55px">
                        <div class="pull-left"> <p style='margin-left: 10px; text-align:center'><?php echo showLink($this->db->record['total_price'],"?module=prospective_request&func=view&page=$page&cid=".$this->db->record['id'], $this->db->record['id']);?>
                        </p></div></th>


                    <th class="tabletxt" width="200" style="min-width:55px"><p align='left'>
                        <?php 
                        if($cid){ 
                    ?>
                    <a href="javascript:changeStatus(<?php echo $this->db->record['id'];?>,<?php echo ($this->db->record['status']==1)?"0":"1";?>, <?php echo $this->db->record['parent_id'];?>,<?php echo $page; ?>);"><img src="images/<?php echo ($this->db->record['status']==1)?"active":"inactive";?>.gif" border="0" align="absbottom" alt="<?php echo ($this->db->record['status']==1)?"Active - Click to deactivate":"Inactive - Click to activate";?>" /></a>&nbsp;

                    <?php } //IF ?>

                   <a href="index.php?module=prospective_request&func=edit&page=<?php echo $page;?>&cid=<?php echo $this->db->record['id'];?>&pid=<?php echo $cid;?>"><img src="images/editrec.png" border="0" align="absbottom" alt="Edit Record" /></a>&nbsp;&nbsp;&nbsp;<a href="javascript:delete_module('<?php echo $this->db->record['id']; ?>', '<?php echo $page;?>')" title="Delect Record"><img src="images/delete.gif" border="0" align="absbottom" alt="Delete Record" /></a>
                   </p> </th>
                  </tr>
                  <?php
                    } //While
                  ?>
                    <form name="frmChangeStatus" action="index.php?module=prospective_request&func=view&page=<?=$page?>&cid=<?=$_GET['cid'];?>" method="post" >
                        <input type="hidden" name="action" value="" />
                        <input name="cat_id" type="hidden" value="" />
                        <input name="prospective_request" type="hidden" value="" />
                    </form> 

        <tr>
           <td class="sub" align="center" colspan="6">
             <?php
                      pagination($numpages, $page, "?module=".$_GET['module']."&func=".$_GET['func']."&cid=".$cid );
                    ?>        
                  </td>
          </tr>
</table>





如果需要快速解决方案,请为要导出的数据行使用临时变量。
例如,在您的while循环之前:

[...]
$this->db->query($sql);
$separator=";";    // Separator to be used in your file
$crlf="\r\n";      // End of line
// Array with the fields to be exported, indexeded on your db fields:
$a_exp=array("gender"="Gender"
            ,"name"=>"Name"
            ,"address"=>"Address"
            ,"email"=>"Email"
            ,"number"=>"Number"
            ,"course"=>"Course"
            ,"price"=>"Price"
            ,"action"=>"Action");
$csv=""; // Add additional headers here
//Header
while(list($field,$descr)=each($a_exp)) {
    $csv.=$descr.$separator;
}
$csv.=$crlf;
while($this->db->fetch_array()) {
    // And here you can add the rows
    reset($a_exp);
    while(list($field,$descr)=each($a_exp)) {
        $csv.=$this->db->record[$field].$separator;
    }
    $csv.=$crlf;
    [...]
}
// At the end of the while cycle you can save the file
$f=fopen("/path/to/your/file.csv","a");
fwrite($f,$csv);
fclose($csv);

或者,您可以使用内置的fputcsv函数()或现有的CSV导出库。

以与在表中显示相同的方式在变量中存储数据

$content=“col1、col2、col3\n\r”
$content.=“数据1、数据2、数据3\n\r”
$content.=“数据4、数据5、数据6\n\r”

上述代码的第一行是excel中各列的标题 csv中的
\n\r
行与html表中的
(新行)相同

运行此代码以生成csv文件

$filename=“yourfile.csv”
$f=fopen($filename,'w')
fwrite($f$content)
fclose($f)

然后运行下面的代码下载文件

头文件(“缓存控制:公共”)
标题(“内容描述:文件传输”)
标题(“内容长度:“.filesize($filename”)。”;”
标题(“内容处置:附件;文件名=$filename”)
标题(“内容类型:应用程序/八位字节流;”)
标题(“内容传输编码:二进制”)

readfile($filename)

我希望这将对您有所帮助。我认为,SO上的登录页应该成为一个搜索框。。。