1

Here I show some partial code for database table export my excel file. The file is written but not right formatted:

function get_report(){
    $this->load->dbutil();
    $this->load->helper('download');
    $query = $this->pharmacy_model->get_report();
    $data = $this->dbutil->csv_from_result($query, ';');
    force_download('CSV_Report.csv', $data);
}

It is downloaded and extracted with data, but format is not right. I need it like in this image

1

2 Answers 2

2
$data = $this->dbutil->csv_from_result($query, ';');

just use ',' instead of semi-colon () in this

Sign up to request clarification or add additional context in comments.

Comments

0

Step 1: Get MySql data in key value pair.

    Array
(
    [14] => Array
        (
            [Name] => Parvez Alam
            [Age] => 21
            [Gender] => Male
        )

    [15] => Array
        (
            [Name] => Shavez Alam
            [Age] => 21
            [Gender] => Male
        )

)

Step 2: PHP code to get options type and force to browser download file instead of display.

if(isset($_POST["ExportType"]))

{

switch($_POST["ExportType"])
{
    case "export-to-excel" :
        // Submission from
        $filename = $_POST["ExportType"] . ".xls";       
        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        ExportFile($data);
        //$_POST["ExportType"] = '';
        exit();
    default :
        die("Unknown action : ".$_POST["action"]);
        break;
}

}

function ExportFile($records) {
    $heading = false;
        if(!empty($records))
          foreach($records as $row) {
            if(!$heading) {
              // display field/column names as a first row
              echo implode("\t", array_keys($row)) . "\n";
              $heading = true;
            }
            echo implode("\t", array_values($row)) . "\n";
          }
        exit;
}

Step 3: Define html layout for display data in table and button to fire export-to-csv action.

<div><a href="javascript:void(0)" id="export-to-excel">Export to excel</a></div>


<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
                        <input type="hidden" value='' id='hidden-type' name='ExportType'/>
                      </form>
                  <table id="" class="table table-striped table-bordered">
                    <tr>
                        <th>Name</th>
                        <th>Status</th>
                        <th>Priority</th>
                        <th>Salary</th>
                  </tr>
                <tbody>
                  <?php foreach($data as $row):?>
                  <tr>
                  <td><?php echo $row ['Name']?></td>
                  <td><?php echo $row ['Status']?></td>
                  <td><?php echo $row ['Priority']?></td>
                  <td><?php echo $row ['Salary']?></td>
                  </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>

Step 4: Now we will use jQuery code to get click event.

    <script  type="text/javascript">
$(document).ready(function() {
jQuery('#Export to excel').bind("click", function() {
var target = $(this).attr('id');
switch(target) {
    case 'export-to-excel' :
    $('#hidden-type').val(target);
    //alert($('#hidden-type').val());
    $('#export-form').submit();
    $('#hidden-type').val('');
    break
}
});
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.