PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 4, 2022

[FIXED] How to export data from MySQL to Excel(.xlsx) but handling the data before exporting

 October 04, 2022     excel, php, phpexcel, phpspreadsheet     No comments   

Issue

I am trying to export data from MySQL to xlsx but in my database may have some 0 and 1 instead of real data. Let me explain better: There's a column used to set if it's day or night but using 0 to day and 1 to night. When i export using the code bellow, i get a xlsx with these 0 and 1.

Already tried stuff like https://phppot.com/php/database-data-export-to-excel-file-using-php/ and https://artisansweb.net/how-to-export-mysql-database-data-to-excel-using-php/ but i just can't edit what's going to xlsx before sending

 $filename = "Export_excel.xls";
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    $isPrintHeader = false;
    if (! empty($productResult)) {
        foreach ($productResult as $row) {
            if (! $isPrintHeader) {
                echo implode("\t", array_keys($row)) . "\n";
                $isPrintHeader = true;
            }
            echo implode("\t", array_values($row)) . "\n";
        }
    }

When the code see a zero, i want to set it to "day"


Solution

If you want to show specific fields only.

select id,name, ( CASE WHEN table_column = 0 THEN 'day' WHEN table_column = 1 THEN 'night' END ) as table_column from your_table

Output Look Like:

id name table_column

1 Lucas day



Answered By - Siddhart hundare
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing