⚠ 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

calculate with sum on field



ynbs
  • profile picture
  • Member

Posted 05 May 2015 - 06:58 AM

I possess difficulty in summing multiple fields in a table, what should I do at the grocery crud

 

I have dummy like this

 

can help me please, My english  not good sorry


skekeu
  • profile picture
  • Member

Posted 06 May 2015 - 18:55 PM

Hi,

 

You can get the total before inserting into the database, using callback_before_insert ...

 

1st - Change the total column of the database to decimal.

 

2nd - Put this in your controller :  

$crud->callback_before_insert(array($this,'my_sum_function'));

3rd - Add this function:

function my_sum_function($post_array) {

$post_array['total'] = $post_array['num_1'] + $post_array['num_2'] + $post_array['num_3'] + $post_array['num_4'] + $post_array['num_5'];
 
return $post_array;
} 

This should work!


ynbs
  • profile picture
  • Member

Posted 07 May 2015 - 06:32 AM

Hi,

 

You can get the total before inserting into the database, using callback_before_insert ...

 

1st - Change the total column of the database to decimal.

 

2nd - Put this in your controller :  

$crud->callback_before_insert(array($this,'my_sum_function'));

3rd - Add this function:

function my_sum_function($post_array) {

$post_array['total'] = $post_array['num_1'] + $post_array['num_2'] + $post_array['num_3'] + $post_array['num_4'] + $post_array['num_5'];
 
return $post_array;
} 

This should work!

 

Thanks alot, done

 

that script is only work, if enter new data.

 

but if want to edit data ??? or update data ?? 
total not updated

 

I tryed to use

callback_after_update

callbackt_after_edit

 

but total nothing updated

 

can u help me on edit data ??


ynbs
  • profile picture
  • Member

Posted 08 May 2015 - 05:27 AM

Thanks done all

 

edited on your code fuction :

 

function my_sum_function($post_array) {
$post_array['total'] = $post_array['num_1'] + $post_array['num_2'] + $post_array['num_3'] + $post_array['num_4'] + $post_array['num_5'];
return $post_array;

}

 

to

 

function my_sum_function($post_array, $primary_key) {

$post_array['total'] = $post_array['num_1'] + $post_array['num_2'] + $post_array['num_3'] + $post_array['num_4'] + $post_array['num_5'];
return $post_array;

}

 
then 
 
$crud->callback_before_insert(array($this,'my_sum_function'));
$crud->callback_before_update(array($this,'my_sum_function'));
 
callback_before_update can used fix after it

Foodzu
  • profile picture
  • Member

Posted 06 August 2015 - 12:02 PM

Thanks for sharing this code.


jacobaugustine
  • profile picture
  • Member

Posted 05 October 2015 - 10:48 AM

Thanks For this code


Martin Lobato
  • profile picture
  • Member

Posted 20 January 2016 - 20:59 PM

I cant get it done in my code:

			$output = $crud->render();
			
			/* La cargamos en la vista situada en 
			/applications/views/productos/administracion.php */
			$this->load->view('productos/administracion', $output);
			
			$crud->callback_after_insert(array($this,'my_sum_function'));
			$crud->callback_after_update(array($this,'my_sum_function'));
			//$crud->callback_edit_field(array($this,'my_sum_function'));
			
			
		}catch(Exception $e){
			/* Si algo sale mal cachamos el error y lo mostramos */
			show_error($e->getMessage().' --- '.$e->getTraceAsString());
		}
	}
}
		
		 function my_sum_function($post_array,$primary_key) {

			$post_array['FOB_USD'] = 99 ; //first try
			$this->db->insert('FOB_ARS',999); //desperate try

			return $post_array;