Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:48:30+00:00 2026-06-05T02:48:30+00:00

I need to run a query that will return multiple rows and export it

  • 0

I need to run a query that will return multiple rows and export it to a CSV. I have to put the cells in a certain order though.

So lets say my table is laid out id, name, address, wife. I need to build a csv in the order of id, address, wife, name. I figured I could just make an array in the correct order and then make a csv with that but after an hour of googling i cant find out how to make a csv with an array.

There is fputcsv but that requires a pre-made csv. Also, i was hoping there was a codeigniter way of doing it.

 public function export() {
    $this->load->helper('download');

    $data[1] = 'i like pie';
    $data[2] = 'i like cake';
    force_download('result.csv', $data);  
}

I tried that but the error said the download helper file was expecting a string not an array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:48:31+00:00Added an answer on June 5, 2026 at 2:48 am

    Here’s some code I use… you could adjust the columns you need in the export…

    Note: This CSV is directly sent to php://output which writes directly to the output buffer. This means you’re not saving any .csv files on the server and it can handle a much larger file size that building a giant array and then trying to loop through it.

        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=\"Jobs_".date('M.j.y', $from)."-".date('M.j.y', $to).".csv\"");
        header("Pragma: no-cache");
        header("Expires: 0");
    
        $handle = fopen('php://output', 'w');
        fputcsv($handle, array(
            'JobId',
            'Template',
            'Customer',
            'Status',
            'Error',
            'PDF',
            'Run Time',
            'Wait Time',
            'Server'
        ));
    
        foreach ($jobs as $jobData) {
            fputcsv($handle, array(
                $job->getId(),
                $job->getTemplate(),
                $jobData['customers_firstname'].' '.$jobData['customers_lastname'],
                $status,
                $error,
                $jobData['products_pdfupload'],
                $job->getRunTime(),
                $job->getWaitTime(),
                $jobData['server']
            ));
        }
    
        fclose($handle);
        exit;
    

    This should give you a good mental picture of how a CSV export works. I don’t use CodeIgniter’s file download helper, so I can’t help you on that front.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a function that will run a query and return the
So, I have a huge query that I need to run on an Access
I want to run a linq query that will return values to my custom
I need to run a query that returns a list of users, along with
i need run code that will create a database and populate tables. i am
I am trying to run a SQL query (SQL Server) that will look up
I have a sql query that I run against a sql server database eg.
In SQL 2005 stored proc I need to run a query that contains a
I have a VB.Net program that will basically make a query to a remote
I need a Regex Statement (run in c#) that will take a string containing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.