⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Howto export full table



jadin
  • profile picture
  • Member

Posted 12 March 2013 - 09:42 AM

Hi,

 

Currently the Flexigrid theme is exporting only the fields that are specified as columns in the table. How can I edit the functionality to export the all the table's fields?

 

Many thanks


jadin
  • profile picture
  • Member

Posted 12 March 2013 - 10:02 AM

edited


jadin
  • profile picture
  • Member

Posted 12 March 2013 - 11:31 AM

edited


jadin
  • profile picture
  • Member

Posted 12 March 2013 - 13:12 PM

This is obviously not optimal since it's messing with Grocery_CRUD.php, and pretty sure it's not tidy either, but in that file I basically changed the function calls from get_xxx to get_field_xxx. I duplicated the function with calls to get_columns to functions with calls to get_fields and renamed the duplicates get_field_xxx(). Finally I added get_fields() function.

 

There is probably a far more straightforward way to do this, and it'll be appreciated if someone can point me to how to a howto without editing grocery_crud.php

 

 

protected function exportToExcel($state_info = null)
	{
		$data = $this->get_common_data();
	
		$data->order_by 	= $this->order_by;
		$data->types 		= $this->get_field_types();
	
		$data->list = $this->get_field_list();
		$data->list = $this->change_field_list($data->list , $data->types);
		$data->list = $this->change_list_add_actions($data->list);
	
		$data->total_results = $this->get_total_field_results();
	
		$data->columns 				= $this->get_fields();
		$data->primary_key 			= $this->get_primary_key();
	
		@ob_end_clean();		
		$this->_export_to_excel($data);
	}

 

 

protected function get_fields()
	{
		if($this->columns_checked === false)
		{
			$field_types = $this->get_field_types();
			if(empty($this->fields))
			{
				$this->fields = array();
				foreach($field_types as $field)
				{
					if( !isset($field->db_extra) || $field->db_extra != 'auto_increment' )
						$this->fields[] = $field->name;
				}
			}
			
			foreach($this->fields as $col_num => $column)
			{				
			
				if(isset($this->relation[$column]))
				{
					
					$new_column = $this->_unique_field_name($this->relation[$column][0]);
					$this->fields[$col_num] = $new_column;
					
					if(isset($this->display_as[$column]))
					{
						$display_as = $this->display_as[$column];
						unset($this->display_as[$column]);
						$this->display_as[$new_column] = $display_as;
					}
					else
					{
						$this->display_as[$new_column] = ucfirst(str_replace('_',' ',$column));
					}
					
					$column = $new_column;
					$this->fields[$col_num] = $new_column;
				}
				else
				{	
					if(!empty($this->relation))
					{
						$table_name  = $this->get_table();
						foreach($this->relation as $relation)
						{
							if( $relation[2] == $column )
							{
								$new_column = $table_name.'.'.$column;
								if(isset($this->display_as[$column]))
								{
									$display_as = $this->display_as[$column];
									unset($this->display_as[$column]);
									$this->display_as[$new_column] = $display_as;
								}
								else
								{
									$this->display_as[$new_column] = ucfirst(str_replace('_',' ',$column));
								}
								
								$column = $new_column;
								$this->fields[$col_num] = $new_column;
							}
						}
					}
						
				}
				
				if(isset($this->display_as[$column]))
					$this->fields[$col_num] = (object)array('field_name' => $column, 'display_as' => $this->display_as[$column]);
				elseif(isset($field_types[$column]))
					$this->fields[$col_num] = (object)array('field_name' => $column, 'display_as' => $field_types[$column]->display_as);
				else
					$this->columns[$col_num] = (object)array('field_name' => $column, 'display_as' => 
						ucfirst(str_replace('_',' ',$column)));
					
				if(!empty($this->unset_columns) && in_array($column,$this->unset_columns))
				{
					unset($this->fields[$col_num]);
				}
			}			
			
			$this->columns_checked = true;
			
		}
		
		return $this->fields;
	}

davidoster
  • profile picture
  • Member

Posted 19 March 2013 - 14:16 PM

Why didn't you make a view (gc view) that shows all the fields and make a print out of it?


ebin123456
  • profile picture
  • Member

Posted 10 June 2013 - 09:35 AM

i am using this simple technique to achieve this

 
if ($this->uri->segment(3) == "export") {
$crud->columns('code','name','address','email','cell','phone','specialization','department','fees');
}
else{
$crud->columns('name','cell','email','phone');
      }

 

 


Fabrizio
  • profile picture
  • Member

Posted 27 June 2013 - 10:00 AM

Hello ebin123456,

 

many many thanks for this tip, i have solved a problem i had too!!

 

fabrizio


activebuzz.ma
  • profile picture
  • Member

Posted 25 January 2017 - 12:41 PM

This is an old thread, but if someone still need a solution to get all the fields on export :

//segment 3 or 4 depending on your route config
		
if ($this->uri->segment(4) == "export") {

			$query = $this->db->query("select * from users");
			$string = [];
			foreach ($query->result_array() as $row)
			{
				foreach($row as $key=>$val){$string[]=$key;}
			}


			$crud->columns( $string);
		}
		else{
			$crud->columns('groups', 'first_name', 'last_name', 'birth','email','active');
		}

Charly
  • profile picture
  • Member

Posted 14 January 2018 - 21:49 PM

 

This is an old thread, but if someone still need a solution to get all the fields on export :

//segment 3 or 4 depending on your route config
		
if ($this->uri->segment(4) == "export") {

			$query = $this->db->query("select * from users");
			$string = [];
			foreach ($query->result_array() as $row)
			{
				foreach($row as $key=>$val){$string[]=$key;}
			}


			$crud->columns( $string);
		}
		else{
			$crud->columns('groups', 'first_name', 'last_name', 'birth','email','active');
		}

Thank you very much :)