⚠ 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

problem with callback_before_update



LoganCMV
  • profile picture
  • Member

Posted 24 July 2015 - 18:06 PM

First, sorry for my English

----------------------------------

 

hi, helpme with a problem.

 

public function ver_prog_riego(){
	$crud = new grocery_CRUD();
        $crud->set_table('riego_calend')
      	 ->set_subject('Calendario de Riego')
         ->columns('bloq_cod','calend_linea','calend_ha','calend_precip','calend_horario','calend_d1','calend_d2','calend_d3','calend_d4','calend_d5','calend_d6','calend_d7','calend_total','calend_mt3')
    	 ->display_as('bloq_cod','Sector de Riego')
    	 ->display_as('calend_linea','Línea')
         ->display_as('calend_ha','Has.')
         ->display_as('calend_precip','Precipitación (Mt3/Hr/Ha)')
    	 ->display_as('calend_horario','Inicio/Termino')
    	 ->display_as('calend_d1', 'Lu')
	 ->display_as('calend_d2', 'Ma')
    	 ->display_as('calend_d3', 'Mi')
    	 ->display_as('calend_d4', 'Ju')
    	 ->display_as('calend_d5', 'Vi')
    	 ->display_as('calend_d6', 'Sa')
    	 ->display_as('calend_d7', 'Do');
	 ->display_as('calend_total', 'Total Horas')
	 ->display_as('calend_mt3', 'Mt3/Ha.');
	 $crud->where('calend_key','key150');
	 $crud->unset_read();
	 $crud->unset_add();
	 $crud->unset_delete();
	 $crud->set_relation('bloq_cod','bloques','bloq_nom');
	 $crud->fields('bloq_cod','calend_linea','calend_ha','calend_precip','calend_horario','calend_d1','calend_d2','calend_d3','calend_d4','calend_d5','calend_d6','calend_d7','calend_total','calend_mt3');
	 $state = $crud->getState();
	if($state == 'edit'){
  	       $crud->field_type('calend_precip','readonly');
  	       $crud->field_type('bloq_cod','readonly');
  	       $crud->field_type('calend_linea','readonly');
  	       $crud->field_type('calend_ha','readonly');
  	       $crud->field_type('calend_total','readonly');
  	       $crud->field_type('calend_mt3','readonly');
	   }
	$crud->callback_before_update(array($this,'totales_calendar_callback'));
        $output = $crud->render();
	$this->load->view('view_prog2.php',$output);
	 }

function totales_calendar_callback($post_array, $primary_key) {
	  $horas = $post_array['calend_d1']+$post_array['calend_d2']+$post_array['calend_d3']+$post_array['calend_d4']+$post_array['calend_d5']+$post_array['calend_d6']+$post_array['calend_d7'];
	  $precip = $post_array['calend_precip'];
	  $post_array['calend_total'] = $horas;
	  $post_array['calend_mt3'] = $$horas * $precip;
  return $post_array;
 }

all ok... edit, save and show grid, the problem is when calculating  '$post_array['calend_mt3'] = $horas * $precip;' ... not done..!!!!

 

 

I tested :

function with and without '$primary_key'   ----> FAIL

$post_array['calend_mt3'] = $post_array['calend_total'] * $post_array['calend_precip']     ---->  FAIL

$post_array['calend_mt3'] = $horas * $post_array['calend_precip']     ---->  FAIL

$post_array['calend_mt3'] = $post_array['calend_total'] * $precip     ---->  FAIL

$post_array['calend_mt3'] = $horas * $precip     ---->  FAIL

$precip = = $horas * $precip     ---->  FAIL

and all variant.   FAIL

 

reviewing and testing many options for that calculation I could see that value of  "$post_array['calend_precip']" never enter to function callback and take 'null' on this. But in the grid the value field 'calend_precip' appears correctly...!!!!

 

Finally found that erasing   '$crud->field_type('calend_precip','readonly')'   WORK...!!!

but I need that field in 'readonly'.

 

I do not understand why '$post_array['calend_total'] = $horas' is calculated correctly if used in the same way that 'calend_precip'

 

 

all the field have the same type  'decimal(10,2)'

 

 

Best regards..

P.L.


Paul Savostin
  • profile picture
  • Member

Posted 24 July 2015 - 18:27 PM

Hi! are you sure that you have $horas variable and not $$horas?


P.S. and dont need to send extension of file to call the view like this $this->load->view('here_name_of_view_file_without_extension_php",$data);


LoganCMV
  • profile picture
  • Member

Posted 24 July 2015 - 18:49 PM

Hi! are you sure that you have $horas variable and not $$horas?


P.S. and dont need to send extension of file to call the view like this $this->load->view('here_name_of_view_file_without_extension_php",$data);

 

Hi ,

 

$$horas is wrong write, just rewrite for example, in the php is $horas.  The problem is 'calend_precip', everything else is good.

 

about call to view, this year started to use codeigniter and take this line from a example, only continue using as was.   I try without '.php' and  work fine, good information..!!!

 

P.L.


Paul Savostin
  • profile picture
  • Member

Posted 24 July 2015 - 20:00 PM

so you had resolve your problem or not?


LoganCMV
  • profile picture
  • Member

Posted 24 July 2015 - 20:44 PM

so you had resolve your problem or not?

 

not... the problem is when anything field in the first edition have a value and set 'readonly', in that case enter to function callback with 'null'. Try with 'calendar_ha' and it is the same. but, in case the field have value '0' or ' null' work fine, as do it the field 'calend_total'.

 

erase line 'readonly' to 'calend_precip' for the callback work, in the first edition set 'calendar_d1' with a value and 'calendar_total' take on > '0'. if I continue editing and setting d2,d3,d4 work and calculations are correct.

 

 

P.L.


Paul Savostin
  • profile picture
  • Member

Posted 24 July 2015 - 21:12 PM

So tell me pls what u want to implement? what logic? and in readonly u cant get value of field cause its only for read


Best way to find out what the problem is use Firebug and Firephp tools. When you add such tools you can easily see what happens in your callback function, what data has $post_array ans so on.


LoganCMV
  • profile picture
  • Member

Posted 27 July 2015 - 12:41 PM

the logic -->:  fill the grid to find the required amount of water for irrigation in a week.

 

'calend_ha' -> Area of irrigation in hectare 
'calend_precip' -> quantity cubic meters x hour x hectare

'calend_d1' ->  Hour of irrigation day monday

'calend_d2' ->  Hour of irrigation day tuesday

'calend_d3' ->  Hour of irrigation day wednesday

'calend_d4' ->  Hour of irrigation day thursday

'calend_d5' ->  Hour of irrigation day friday

'calend_d6' ->  Hour of irrigation day saturday

'calend_d7' ->  Hour of irrigation day sunday

'calend_total'-> sum hour of irrigation of week (d1+d2+d3+d4+d5+d6+d7)    work fine.
'calend_mt3' -> water for irrigation a hectarea a week. (total * precip)       fail...

 

then finally  'calendar_mt3' * 'calendar_ha' give me the amount of water necesary for irrigation the farm in this area (bloq_cod)

 

 

P.L.


Paul Savostin
  • profile picture
  • Member

Posted 27 July 2015 - 23:23 PM

So what I understood from that you explain - you need calculate some field

data and save it in to db field but make it in callback_before and do not show field in table?

So do not make this field like 'hidden', calculate it in callback and add it to the post array?


Cindy Lorena
  • profile picture
  • Member

Posted 30 July 2015 - 16:40 PM

hola tengo un problema en cuanto a las listas dependientes con esta libreria. estoy utilizando 

$crud->callback_edit_field('datos_generales_id_datos_generales', array($this, 'empty_state_dropdown_select'));

 $crud->callback_edit_field('formato_legalizacion_id_formato_legalizacion', array($this, 'empty_city_dropdown_select'));

 

 $dd_data = array(
            //GET THE STATE OF THE CURRENT PAGE - E.G LIST | ADD
            'dd_state' => $crud->getState(),
            //SETUP YOUR DROPDOWNS
            //Parent field item always listed first in array, in this case countryID
            //Child field items need to follow in order, e.g stateID then cityID
            'dd_dropdowns' => array('formatoLista_id_formato', 'datos_generales_id_datos_generales'),
            'dd_dropdown' => array('formatoLista_id_formato', 'formato_legalizacion_id_formato_legalizacion'),
            //SETUP URL POST FOR EACH CHILD
            //List in order as per above
            'dd_url' => array('', site_url() . '/revision/get_states/', site_url() . '/revision/get_cities/'),
            //LOADER THAT GETS DISPLAYED NEXT TO THE PARENT DROPDOWN WHILE THE CHILD LOADS
            'dd_ajax_loader' => base_url() . 'ajax-loader.gif'
        );
        $output->dropdown_setup = $dd_data;

en el momento de agregar funciona genial pero cuando voy a editar un registro me marca errores