• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

doubt repeater fields and subtraction

Hi!

I have a form where I have a script to do a mathematical reduction operation.

[...]

$var1 = '{tn___espelho_estoque_entrada}';
$var2 = '{tn___quantidade_saida}';
$var3 = $var1 - $var2;
$query->update('farmacia_entrada')->set('quantidade_entrada = ' . $var3)->where('id = ' . $db->quote('{farmacia_saida___nome_produto}'));
[...]


In that same form, I have a databasejoin field that takes a list of items from another table, and a cascading dropdown field that shows the relative quantity of that item and another field of type input where a value is inserted that will be reduced from the total of that item.

My question is, can I put these databasejoin, cascading and input fields to keep repeating?
And what would the mathematical formula look like for this reduction of this repeater group?


the question was a little confusing, I'm sorry :/
 
Hey @lousyfool !
I saw this part of the documentation but honestly I didn't even know how to start :/

I think the biggest problem will be the following:

each repeater group will be for an item and its quantity, so how to do the mathematical operation similar to the scenario when it was only 1 x 1 field, without being a repeater?

Sorry for my lack of advanced knowledge. :(
 
Well, as already mentioned, the Wiki shows you examples of how to get form element data -- also if those elements are in repeatable groups. You'll need to try/learn understanding, then adjusting the examples to your needs, your form, your elements.
More can hardly be said or done here because only you have all the details of your system. In other words, writing the code for you is a custom job, also requiring access and insight to your system.
 
Hey @lousyfool !
I can't evolve in my problem.
My knowledge of PHP is little to solve this :/
Could you help me more?

I was able to assemble this code:

$db = JFactory::getDBO();
$query = $db->getQuery(true);


foreach ($this->data['farmacia_saida_47_repeat___id'] as $key => $value)
{

$query->clear()
->update('farmacia_entrada')
->set('quantidade_entrada = ' . $db->quote($this->data[farmacia_saida_47_repeat___espelho_estoque_repetidor][$key]))
->where('id = ' . $db->quote($this->data[farmacia_saida_47_repeat___nome_produto][$key]));

$db->setQuery($query);
$db->execute();
// do something with $cost and $qty
}


But I can't update the table I need :(


The field "farmacia_saida_47_repeat___espelho_estoque_repetidor" of type calc that brings the result of a subtraction.

I've been trying to solve this for days but without any success.
I'm already desperate trying to solve this.:(


Thanks again and sorry for the inconvenience!
 
First, it's really hard to give advice on how to solve problem without detailing the db and input form configuration.
Second. depanding on workflow and when do calculation and update another table (after/befor the form is saved, when user input some data in a field/elemner...) you should find out which plugin to use. Maybe you need Form PHP plugin, maybe you can use JS and ajax.

Some app workflow, db structure and form picture will help a lot.
 
Hi @deant !

See if it helps to understand the problem


upload_2022-12-1_15-0-31.png


The field "Estoque Atual" (cascading dropdown) shows the quantity of the product selected in the field "Nome Produto".

Each product only shows a quantity
 
OK. I would do it OnAfterProces...i allways do updates to another table on after proces

Check how to get form data and how data looks
PHP:
echo "<pre>";print_r($data);exit;

Get the data which you'll be need it in your code
PHP:
defined('_JEXEC') or die();

$db = JFactory::getDbo();

$fs_id = $data['farmacia_saida___id'];
$nome_prodoto = $data['farmacia_saida_47_repeat___id_raw'];

Print out your vars
PHP:
echo "<pre>";print_r($nome_prodoto);exit;
...you get array inside array
How to solve? double foreach

PHP:
foreach ($nome_prodoto as $key) {
    foreach ($key as $val => $value){
    //    print_r($value);

Now lets find your 'espelho_estoque_repetido' in repeat table
PHP:
$query_2 = $db->getQuery(true);

        $query_2
        ->select($db->quoteName('espelho_estoque_repetido'))
        ->from($db->quoteName('farmacia_saida_47_repeat'))
        ->where($db->quoteName('id') . ' = ' . $db->quote($nome_prodoto))
        ->where($db->quoteName('parent_id') . ' = ' . $db->quote($fs_id)) ;
        $db->setQuery($query_2);
        $espelho = $db->loadResult();
and finally update quantidade_entrada table

PHP:
        $query_3->update('farmacia_entrada')
        ->set('quantidade_entrada =' . $db->quote($espelho))
        ->where('id =' . $db->quote($value));
   
        $db->setQuery($query_3);
        $db->execute();
    }
}
Code was not tested it is just for demonstration...done in 15-20min so check it and modifay it for your purpose.

have fun
 
Last edited:
sorry for the delay in answering you friend @deant !

it's just that I had a health problem (COVID-19) and I left the project.

Coming back now!

Thank you for your attention and help!

I tested the code model that you created, I made some other changes and nothing works :/
 
Of course the code I wrote does not working...

Here is a hint: In foreach statement I wrote var $value....in qurey statement is $nome_prodoto. So what is wrong ;)?

And did you print out vars? What do you get?

And BTW check your erroelog file on your server root directory.
 
Thanks for the tip again @deant !

I adjusted what you said and it didn't work :/

In the project log it is returning:

PHP Warning: Invalid argument supplied for foreach() in /home/......./plugins/fabrik_form/php/php.php(595) : eval()'d code on line 9

This line 9 is the line of the second foreach
 
PHP Warning: Invalid argument supplied for foreach() in /home/......./plugins/fabrik_form/php/php.php(595) : eval()'d code on line 9
Great now you know what is wrong. Did you print out $value e.g. echo "<pre>";print_r($value);exit; ?
Maybe you can try: foreach ($key as $value)...if the code not working print out your vars line by line and you'll find error.
And BTW did you set your plugin to run OnAfterProcces?
 
Hi @deant !

I am using the plugin with OnAfterProcess.

if i use two foreach and try to print anything inside the second foreach it returns nothing.

it only returns if I use a foreach

strangely, even using a foreach to get the data from "$product_name = $data['pharmacy_saida_47_repeat___id_raw'];"
It only returns me the first item of the array.

Sorry for the inconvenience :(
 
there's the question @deant ...
it's an array with several items (repeater groups) and the code is only getting the first one :/

And even using that first item, it's not updating in the other table :(
 
Please read here how php foreach works.

If you print out $nome_prodoto = $data['farmacia_saida_47_repeat___id_raw'];
echo "<pre>";print_r($nome_prodoto);exit;

you should get something like:

Array
(
[0] => Array
(
[0] => 467
)

[1] => Array
(
[0] => 499
)

[2] => Array
(
[0] => 500
)

.
.
.

[12] => Array
(
[0] => 502
)
}

Using foreach inside foreach loop you shoud get repeat row id...something like:
467

Then you can get item id from this row and other data if you need. Look your data structure using phpMyAdmin.
And final step is update another table. All code is inside your second foreach loop...

I thing I can't explain more simple...
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top