1

I'm working on a project, that requires exporting data from MYSQL DB depending on the multiple conditions. I am referring this:

This is my source code:

public function exportExcelData($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", ($row)) . "\n";
            }
 }

public function fetchDataFromTable()
{
     $query =$this->db->get('one_piece_characters'); // fetch Data from table
     $allData = $query->result_array();  // this will return all data into array
     $dataToExports = [];
     foreach ($allData as $data) {
        $arrangeData['Charater Name'] = $data['name'];
        $arrangeData['Charater Profile'] = $data['profile'];
        $arrangeData['Charater Desc'] = $data['description'];
        $dataToExports[] = $arrangeData;
  }
  // set header
  $filename = "dataToExport.xls";
                header("Content-Type: application/vnd.ms-excel");
                header("Content-Disposition: attachment; filename=\"$filename\"");
  $this->exportExcelData($dataToExports);
 }

If I use it without any where clause, it is giving entire table.

If i use only single where clause, it works good.

Bur, if I use multiple where conditions. it gives me a blank excel sheet.

Does anyone have any idea regarding how to make it work with multiple where conditions?

5
  • Are you sure there're any data in your db that fits your multiple conditions? Commented Oct 1, 2016 at 12:41
  • show your where condition? Commented Oct 1, 2016 at 12:43
  • Try echo $this->db->last_query() with your multiple conditions and execute the same query in your database or phpmyadmin & check if you are getting any results Commented Oct 1, 2016 at 12:58
  • @Zeeshan the query looks like this select * from dept where deptid=5 and deptname="motor" and it doesn't return any data. It gives a blank Excel Sheet Commented Oct 3, 2016 at 11:11
  • Did you tried running the same query in the database directly and if you can any result of it Commented Oct 3, 2016 at 11:50

2 Answers 2

5

Try using this code on your model file:

function createcsv(){
        $this->load->dbutil();
        $this->load->helper('file');
        $this->load->helper('download');
        $delimiter = ",";
        $newline = "\r\n";
        $filename = "filename.csv";
        $query = "SELECT * FROM YourTable"; //USE HERE YOUR QUERY
        $result = $this->db->query($query);
        $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
        force_download($filename, $data);

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

1 Comment

Is dbutil third party library?
0

// ecport contect list in csv ,

public function cnt_explode(){

$csvData[] =array(  "Name", "Email Id","Mobile No.","Event", "City","location","No of guest","Event Date","Budget",
                    "Venue Type","Food","Drink","Description","Date");
$data = $this->contact_model->get_contact(NULL);

foreach($data as $cnt){
$csvData[]=array(
      $cnt->name ,$cnt->email, $cnt->mobile_no, $cnt->event, $cnt->city,  $cnt->location,  $cnt->no_guest,$cnt->event_date,$cnt->budget, $cnt->venue_type,
      $cnt->food, $cnt->drink, $cnt->description,$cnt->created_on,
      );
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=venuexpo_".time().".csv");
header("Content-Transfer-Encoding: binary");
$df = fopen("php://output", 'w');
array_walk($csvData, function($row) use ($df) {
  fputcsv($df, $row);
});
fclose($df);
}

///------------ downlode csv done --------------

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.