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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度