Conditional count on calc element

madpato

Member
Hello

I am trying to achieve the following:

On a calc element i am counting if elements are not empty in order to fill a % of completition of the form. I have everything almost done but now i run in a small problem, i need one field to condition the sum of the %. So its something like this:

PHP:
if( '{table___a}' != '0' ) $count++;
else if('{table___a}' == '2')
  if ( '{table___b}' != '')  $count++; 
  if( '{table___c}' != '') $count++;
  if( '{table___d}' != '0000-00-00 00:00:00' ) $count++;
 
if('{table___a}' == '2') $formula = round((($count / 24) * 100));
else $formula = round((($count / 21) * 100));
 
return ($formula . '%');

What it means is if the field "a" has a value of "2" the count should be done for the fields b,c,d. Then if table___a equals 2 the formula to obtain the % is the counts divided in 24 (since there are 3 more elements to consider). Else divide on 21.
So far with that aproach i get wrong % and even if i dont do the % (just the counts) it still wrong.

Any help is appreciated. Regards.
 
Not sure about your
if( '{table___a}' != '0' )
Do you really mean !='0' ?
In this case the else if part will never be run because with a == '2' it will run the a !='0' part.
If you want to count a always and b,c,d in case of a==2 additionally it should be something like
PHP:
if( '{table___a}' != '0' ) $count++;
 
if('{table___a}' == '2')
{
  if ( '{table___b}' != '')  $count++;
  if( '{table___c}' != '') $count++;
  if( '{table___d}' != '0000-00-00 00:00:00' ) $count++;
}
if('{table___a}' == '2') $formula = round((($count / 24) * 100));
else $formula = round((($count / 21) * 100));
 
return ($formula . '%');
 
I don't have acces to your details (only Rob and Hugh).

I just started with the code you gave, thinking it was only a code snippet.
If it is your complete code you must initialise $count somewhere, e.g.

$code=0;
if (...
 
Hello this is my exact code:

PHP:
$count = 0;
if( '{table1___nombre}' != '' ) $count++;
if( '{table1___rut}' != '' ) $count++;
if( '{table1___fechanacimiento}' != '0000-00-00 00:00:00' ) $count++;
if( '{table1___sexo}' != '' ) $count++;
if( '{table1___region}' != '106' ) $count++;
if( '{table1___comuna}' != '348' ) $count++;
if( '{table1___ciudad}' != '' ) $count++;
if( '{table1___direccion}' != '' ) $count++;
if( '{table1___niveleducativo}' != '0' ) $count++;
if( '{table1___detallenivel}' != '' ) $count++;
if( '{table1___telefono}' != '' ) $count++;
if( '{table1___celular}' != '' ) $count++;
if( '{table1___mail}' != '' ) $count++;
 
if( '{table2___nombre_empresa}' != '' ) $count++;
if( '{table2___region}' != '106' ) $count++;
if( '{table2___comuna}' != '348' ) $count++;
if( '{table2___ciudad}' != '' ) $count++;
if( '{table2___sectoreconomico}' != '' ) $count++;
if( '{table2___rubro}' != '' ) $count++;
if( '{table2___descripcion_actividad}' != '' ) $count++;
if( '{table2___estado_inicio_actividades}' != '0' ) $count++;
if( '{table2___estado_inicio_actividades}' == '2' )
{
if( '{table2___rut}' != '')  $count++; 
if( '{table2___razonsocial}' != '') $count++;
if( '{table2___fechainiciacion}' != '0000-00-00 00:00:00' ) $count++;
}
if('{table2___estado_inicio_actividades}' == '2') $formula = round((($count / 24) * 100));
else $formula = round((($count / 21) * 100));
 
$formula = round((($count / 21) * 100));
return ($formula . '%');

Regards.
 
With your last but one line you are always doing
$formula = round((($count / 21) * 100));
so ignoring your condition
if( '{table2___estado_inicio_actividades}' == '2' )
 
Your right i have corrected it but still the same problem. I will send you details via private message. Thank you.
 
You must take the raw value if you are testing against the value (2), not the label (Si)
if( '{table2___estado_inicio_actividades_raw}' == '2' )
 
I checked the example row you gave e (for id 450).

I think some of the problem is that sometimes the "empty" values you are assuming for placeholders, like empty dates being "0000-00-00 00:00:00" are not correct.

What I suggest you do is, as the first line of the calc:

PHP:
var_dump($data);exit;[php]
 
And submit a form with all empty values.  This will show you exactky what the submitted "empty" data is for that form, and you can work out what "empty" actually is for each element.  Then instead of using placeholders, use the $data array directly.  For instance ...
 
[code]if ($data['tns_emprendedor___sexo'] != '') { $count++; }[code]
 
   -- hugh
 
Tried this
Code:
($data['tns_emprendedor___sexo'] != '') { $count++; }

did not work, i have pretty much how to sort the counting process my issue is with the condition that if a field is zero or two problem where i am still struggling. If there is a zero if all other fields are with data, i should reach 100% which is not happening :(

This part in specific:

Code:
if( '{tns_empresas___estado_inicio_actividades_raw}' != '0' ) $count++;
if( '{tns_empresas___estado_inicio_actividades_raw}' == '2' )
{
if( '{tns_empresas___rut}' != '')  $count++;  
if( '{tns_empresas___razonsocial}' != '') $count++;
if( '{tns_empresas___fechainiciacion_raw}' != '' ) $count++;
}
if('{tns_empresas___estado_inicio_actividades_raw}' == '2') $formula = round((($count / 24) * 100));
else $formula = round((($count / 21) * 100));
 
return ($formula . '%');
 
Can you add a debug line just before you calculate the percentage, and also add initializeing $formula, and some block braces on the if/else (just to be sure):

PHP:
var_dump('{tns_empresas___estado_inicio_actividades_raw}', $count); exit;
 
$formula = 0;
if('{tns_empresas___estado_inicio_actividades_raw}' == '2') {
   $formula = round((($count / 24) * 100));
}
else {
   $formula = round((($count / 21) * 100));
}
return ($formula . '%');

So first make sure the var_dump throws out what you are expecting (like 0 and 21), and assuming it is, take that line out again, and see if adding those braces helped.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top