Count databasejoin checked elements in calc element

carlos_k

New Member
Hi,

Ihave a databasejoin element rendered as a group of checkboxes, I want to store the number of checkboxes that are checked, the element's name is:
ordenes_publicidad_15_repeat___Orden_Ediciones

I used some code from the Wiki to no avail:

$cnt = 0;
$vals = $data['ordenes_publicidad_15_repeat___Orden_Ediciones'];

foreach ($vals as $val)
{
if ($val != 0)
{
cnt++;
}
}

return $cnt;

Could you help me with this, I'm stuck..

Thanks
Carlos
 
If I set the calc element's code to:

return $data['ordenes_publicidad_15_repeat___Orden_Ediciones'];

I get:

["2016-Bimestral 2","2016-Bimestral 3","2016-Bimestral 4"]

How do I return 3?
 
return count( $data['ordenes_publicidad_15_repeat___Orden_Ediciones'] );
That returns the number of elements in the array - but it doesn't account for any with zero values.

In the code you posted that does check for zero values - you forgot the $ ahead of cnt++;
That line should be $cnt++;
 
Thanks Bauer, it works. I think the multiselect checkbox stores only the elements that are checked, so counting them is a valid solution.

From another post, also works, but it is more complicated:

$ediciones = json_decode($data['ordenes_publicidad_15_repeat___Orden_Ediciones']);
return sizeof($ediciones);
 
I'm trying to make the same calculation but instead of using a calculation plugin I would like to use javascript so it runs faster.

I changed the structure of the checkbox from my last question, it didn't need to be a repeatable element, I have to select several possible choices and fabrik stores
them in a repeat table.


I made a test function which is triggered by onchange of the multiselect checkbox, it always shows 0:

function calcularCantAvisos()
{
var myForm = Fabrik.getBlock('form_7');
var cantAvisos = 0;
var subElements = myForm.elements.get('ordenes_publicidad___revistas').subElements;

for(i = 0;i < subElements.length; i++)
{
if (subElements.checked === 'checked')
{
cantAvisos++
}
}

alert(cantAvisos);
}

The HTML elements that are included in the source are like:

<input value="9" data-role="fabrikinput" name="ordenes_publicidad___revistas[0]" class="fabrikinput" type="checkbox">
<input value="1" data-role="fabrikinput" name="ordenes_publicidad___revistas[1]" class="fabrikinput" checked="checked" type="checkbox">

Maybe someone can help, thanks!
 
try using...
JavaScript:
if(subElements[i].checked)
instead.

This jQuery code should do the same thing - but in 3 lines...
JavaScript:
jQuery("input:checkbox[name^=ordenes_publicidad___revistas]:checked").each(function () {
      alert("Checked Name: " + jQuery(this).prop("name") + " Value: " + jQuery(this).val());
});
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top