pagination export csv cakphp code example

Example: pagination export csv cakephp

// model
public function get_data_guest_export($conditions, $page, $limit, $lang){
        $all_settings = array(
            'conditions' => $conditions,
            'contain' => array(
				'Member' => array(
					'fields' => array(
						'Member.first_name',
						'Member.last_name',
					),
				),
				'BuildingFacility' => array(
					'fields' => array(
						'BuildingFacility.id',
					),
					'BuildingFacilityLanguage' => array(
						'conditions' => array(
							'BuildingFacilityLanguage.alias' => $lang,
						),
					),
					'BuildingPost' => array(
						'BuildingPostLanguage' => array(
							'conditions' => array(
								'BuildingPostLanguage.alias' => $lang,
							),
						),
					),
				),
				'Company' => array(
					'CompanyLanguage' => array(
						'conditions' => array(
							'CompanyLanguage.alias' => $lang,
						),
					),
				),
				'CreatedBy',
				'UpdatedBy'
            ), 
            'order' => array('Member.id DESC'),
            'limit' => $limit,
            'page' => $page
        );

		return $this->find('all', $all_settings);
	}
	
	// __('id'),
	// __d('building', 'building_post'),
	// __d('building', 'building_facility'),
	// __('name'),
	// __d('member','name'),
	// __d('building','date'),
	// __d('building','time'),
	// __('enabled'),
	// __('created'),
	// __('created_by'),
	// __('updated'),
	// __('updated_by'),

    public function format_data_guest_export($data, $row) {

        return array(
            @$row['MemberBookFacility']['id'],
			isset($row['BuildingFacility']['BuildingPost']['BuildingPostLanguage']) && !empty($row['BuildingFacility']['BuildingPost']['BuildingPostLanguage']) ? reset($row['BuildingFacility']['BuildingPost']['BuildingPostLanguage'])['title'] : '',
			isset($row['BuildingFacility']['BuildingFacilityLanguage']) && !empty($row['BuildingFacility']['BuildingFacilityLanguage']) ? reset($row['BuildingFacility']['BuildingFacilityLanguage'])['title'] : '',
			isset($row['Company']['CompanyLanguage']) && !empty($row['Company']['CompanyLanguage']) ? reset($row['Company']['CompanyLanguage'])['name'] : '',
			$row['Member']['first_name'] . " " . $row['Member']['last_name'],
			isset($row['MemberBookFacility']['date']) 	? $row['MemberBookFacility']['date'] : '',
			isset($row['MemberBookFacility']['time']) 	? $row['MemberBookFacility']['time'] : '',
		    $row['MemberBookFacility']['enabled'] == 1 ? 'Y' : 'N',
            isset($row['MemberBookFacility']["created"]) ? $row['MemberBookFacility']["created"] : '',
            isset($row['CreatedBy']["email"]) ? $row['CreatedBy']["email"] : '',
            isset($row['MemberBookFacility']["updated"]) ? $row['MemberBookFacility']["updated"] : '',
            isset($row['UpdatedBy']["email"]) ? $row['UpdatedBy']["email"] : '',
        );
	}



// controller

public function admin_guest_export(){
		$this->disableCache();

		if( $this->request->is('get') ) {
            $limit = 1000;

            $header = array(
                __('id'),
                __d('building', 'building_post'),
				__d('building', 'building_facility'),
				__('name'),
				__d('member','name'),
                __d('building','date'),
                __d('building','time'),
				__('enabled'),
                __('created'),
                __('created_by'),
                __('updated'),
                __('updated_by'),
            );

            try{
                $file_name = 'member_book_facility_' . date('Y-m-d h-i');
				$data_binding = array();

                // export xls
                if ($this->request->type == "xls") {
                    $this->ExcelSpout->setup_export_excel($header, 'Member.Member', $data_binding, $this->request->conditions, $limit, $file_name, $this->lang18);
                } else {
					
                    $this->Common->setup_export_csv($header, 'Member.MemberBookFacility', $data_binding, $this->request->conditions, $limit, $file_name, $this->lang18, 1);
                }
				exit;
				
            } catch ( Exception $e ) {
                $this->LogFile->writeLog($this->LogFile->get_system_error(), $e->getMessage());
                $this->Session->setFlash(__('export_csv_fail') . ": " . $e->getMessage(), 'flash/error');
            }
		}
	}

Tags:

Php Example