# Calculated field

**URL:** <https://www.dolibarr.org/forum/t/calculated-field/24683>\
**Category:** Developing for Dolibarr\
**Created:** [December 12, 2023, 4:40pm UTC](https://www.dolibarr.org/forum/t/calculated-field/24683 "2023-12-12T16:40:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanomior](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/stefanomior/32/404_2.png) [@stefanomior](https://www.dolibarr.org/forum/u/stefanomior)\
**Post date:** [December 12, 2023, 4:40pm UTC](https://www.dolibarr.org/forum/t/calculated-field/24683/1 "2023-12-12T16:40:13Z")

</div>

Good evening, I know very well that the formula is wrong, due to my ignorance; I wrote it this way, just to know if it is possible to get a conditional calculation and possibly an example on how to write it.  
Thank you

```auto
if (($object->array_options ['options_misureps1']) = 1 and ($object->array_options ['options_stratigrafia1']) = 1 ) then ($object->array_options ['options_str1a'])-($object->array_options ['options_str1b']);
if (($object->array_options ['options_misureps1']) = 1 and ($object->array_options ['options_stratigrafia1']) = 2 ) then ($object->array_options ['options_str2a'])-($object->array_options ['options_str2b']); 
.......etc...continue n times

```

---

<div class="post-metadata">

**Author:** ![ksar](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/ksar/32/514_2.png) [@ksar](https://www.dolibarr.org/forum/u/ksar)\
**Post date:** [December 12, 2023, 4:59pm UTC](https://www.dolibarr.org/forum/t/calculated-field/24683/2 "2023-12-12T16:59:33Z")

</div>

Hello,

You should use Ternary : [PHP Ternary Operator](https://www.phptutorial.net/php-tutorial/php-ternary-operator/)

```auto
if (($object->array_options ['options_misureps1']) = 1 and ($object->array_options ['options_stratigrafia1']) = 1 ) then ($object->array_options ['options_str1a'])-($object->array_options ['options_str1b']);

```

should become

```auto
(($object->array_options ['options_misureps1']) = 1 and ($object->array_options ['options_stratigrafia1']) = 1 )?($object->array_options ['options_str1a'])-($object->array_options ['options_str1b']):'';

```

---

<div class="post-metadata">

**Author:** ![stefanomior](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/stefanomior/32/404_2.png) [@stefanomior](https://www.dolibarr.org/forum/u/stefanomior)\
**Post date:** [December 12, 2023, 11:40pm UTC](https://www.dolibarr.org/forum/t/calculated-field/24683/3 "2023-12-12T23:40:29Z")

</div>

Hi Ksar, I tried what you suggested but it doesn’t work, I think it’s a problem in the formula, as the system can no longer open the project page, could it be because some of the fields in the formula may not contain data?

Stephen

---

<div class="post-metadata">

**Author:** ![ksar](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/ksar/32/514_2.png) [@ksar](https://www.dolibarr.org/forum/u/ksar)\
**Post date:** [December 13, 2023, 9:32pm UTC](https://www.dolibarr.org/forum/t/calculated-field/24683/4 "2023-12-13T21:32:00Z")

</div>

Hello,

You have to tell a bit more, how the extrafields are declared etc…  
What is you Dolibarr version ?  
It’s a line extrafiled or a “normal” one ?

I also correct a bit the equation that was still wrong above :

```php
(($object->array_options['options_misureps1'] == 1) && ($object->array_options['options_stratigrafia1'] == 1))?($object->array_options['options_str1a']-$object->array_options['options_str1b']):'';

```

---

<div class="post-metadata">

**Author:** ![stefanomior](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/stefanomior/32/404_2.png) [@stefanomior](https://www.dolibarr.org/forum/u/stefanomior)\
**Post date:** [December 15, 2023, 8:08am UTC](https://www.dolibarr.org/forum/t/calculated-field/24683/5 "2023-12-15T08:08:01Z")

</div>

Good morning Ksar, you are right; the version used is 16.0.3 the fields are project-extrafields, in practice a field must be calculated based on n combinations between 2 other fields all present in the indicated table.

Stefano
