dongshu4755 2011-12-01 14:57
浏览 38

更改CSV导出的数据库值

I have a CSV export script which grabs certain fields from a certain table and their values and creates an Excel file with the data. See script below.

It first grabs the column names (except for the ID). Secondly it will grab the values of firstname, lastname, employee_id, unit and present and puts all these values in an Excel file.

Now note that it grabs the data from the table: table_info In this table, the UNIT and PRESENT exists out of numbers only. These numbers represent the ID's of the unit/present from two other tables:

table_units (id, title) table_presents (id, title)

What I would like to have is that in the Excel sheet it doesn't show the numbers (id's) of the unit/present but the title of the unit/present. How can I accomplish this? My PHP knowledge just stops here.

        $table = 'table_info';
    $file = 'export';

    $result = mysql_query("SHOW COLUMNS FROM ".$table."");
    $i = 0;
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            if($row['Field'] != 'id'){
                $csv_output .= $row['Field']."; ";
                $i++;
            }
        }
    }
    $csv_output .= "
";

    $values = mysql_query("SELECT firstname, familyname, employee_id, unit, present FROM ".$table."") or die(mysql_error());
    while ($rowr = mysql_fetch_row($values)) {

        for ($j=0;$j<$i;$j++) { 
            $csv_output .= $rowr[$j]."; ";
        }
        $csv_output .= "
";
    }

    $filename = $file."_".date("Y-m-d_H-i",time());
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: filename=".$filename.".csv");
    print $csv_output;
    exit;
  • 写回答

2条回答 默认 最新

  • dosin84644 2011-12-01 17:35
    关注

    Change your sql query to do a join that includes table_units and table_presents. Or if you are concerned that a join will cause a performance bottleneck, you could store table_units and table_presents in an array keyed by ID, that you can reference when you have your foriegn keys (unit_id, present_id).

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?