dtdvbf37193 2018-08-26 22:00
浏览 88

将数据库导出到csv会更改一些字段名称

I want to export my database to an csv file. I have this alrealdy:

$query=mysqli_query($conexion, "SELECT * FROM table_name") or die("Problemas con la 
 conexión: ".mysqli_error($conexion));
 $variable_name = array();
 if (mysqli_num_rows($query) > 0) {
   while ($row = mysqli_fetch_assoc($query)) {
      $variable_name[] = $row;
  }
 }
 header('Content-Type: text/csv; charset=utf-8');
 header('Content-Disposition: attachment; filename="filename.csv"');
 header('Pragma: no-cache');
 header('Expires: 0');
 $output = fopen('php://output', 'w');
 fputcsv($output, array('col1', 'col2', 'col3', 'col4', 'col5', 'col6', 
 'col7', 'col8', 'col9', 'col10'));
  if (count($varible_name) > 0) {
    foreach($variable_name as $row) {
    fputcsv($output, $row);
   }
  }
 fclose($output);

This is working fine, but I just realice that one column has the id value an not the real value that is related to another table. The thing is that I want to replace the id numbers of that column with the real names of the values. Let's say that col3 has values of 1, 2, or 4 and I want to replace those values with 'Column value for 1', 'Column value for 2', etc. I try to join the tables but it joined the ids, so i still have the numbers an not the named values.

How can I do it? thank you

UPDATE.

Example of the table to export.

table1
id   name   job   payment   workshop
 1   susan  job1  yes       1
 2   mike   job2  yes       3
 3   bob    job3  no        2
 4   rob    job3  no        3

table2
id  workshop_name
1   cooking
2   talking
3   running

When I try to join both tables the cvs file was:

id name job payment workshop 1 susan job1 yes 1 cooking 2 mike job2 yes 3 running 3 bob job3 no 2 talking 4 rob job3 no 3 running

What I want is to replace the numbers in col workshop with the names form table2: cooking running talking. Maybe with a simple switch i can change the numbers with the names, but I don't know how to do it.

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 luckysheet
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱