• 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.

Solved: Storing a Dropdown Element values and labels into an array?

ahmedo

Member
I have an element of Dropdown type. It has for values and labels. Example

Element name is currency

value 1
label MYR

value 2.98
label 2.9

value 3.96
label USD

I need to access any part of the element to be used in few calc elements when I add a new record.

If this is not possible then, what would be the best way to go about it.

My calc elements add and reduce few % to the rate depending one which currency is used.

Thanks.
 
I strongly recommend using a join element for your currency, not a dropdown. Getting access to the labels for dropdowns is not entirely trivial, as they are stored as internal metadata in Fabrik's own tables.

The other compelling reason to use a table is that you can then treat your currency data like any other Fabrik list, and add/edit/delete through a Fabrik list (on the front end if youw ant, with ACL settings), rather than having to edit the Fabrik dropdown element settings on the backend.

If you create a 'currency' table ...

id, value, label

... and make your currency element a join to it (displayed as a dropdown), with the 'id' as the 'value' and 'label' as 'label', then you can then easily get the values and labels yourself, with a simple query.

Code:
// replace with your currency element's fullname, with _raw appended
$currencyId = '{yourtable___currency_raw}';
if (!empty($currencyId)) {
   $db = JFactory::getDbo();
   $query = $db->getQuery(true);
   // replace with your currency table name
   $query->select('*')->from('currency')->where('id = ' . (int)$currencyId);
   $db->setQuery($query);
   $currency = $db->loadObject();
   // you can now use $currency->value and $currency->label
}

-- hugh
 
Thanks Hugh.

Actually, accessing the selected the currency and the rate when using the Dropdown type was ok. But I want to extract the rate or the currency label based on an index in an array. This means that if I have 4 currencies, with 4 different values, I want them all to be stored in an array so that I can select any of them for some calculations.
 
Trust me on this. It may seem OK right now, but I can absolutely guarantee that you will wish it was a table sooner or later. Only use dropdown or radio elements for choices that (pretty much) never change - "Favorite Color" or "Gender", and that you don't need to do any kind of "stuff" in your own code with. OK, so gender is getting a little fluid these days, but you know what I mean.

For anything that you may want to "do stuff" with (including getting an array of values and labels), or that may change, create a table, and use a join set to dropdown or radio.

So to get that array of all of the values and their labels from my example table ...

Code:
   $db = JFactory::getDbo();
   $query = $db->getQuery(true);
   // replace with your currency table name
   $query->select('*')->from('currency');
   $db->setQuery($query);
   $currency = $db->loadObjectList();
   // you now have an array of objects with the values and labels

If you wanted to index your array by the "label" ...

Code:
   $currency = $db->loadObjectList('label');

... so you can then access the $currency array keyed by the label, like $currency['MYR']->value.

This is probably one of the most frequent issues I wind up having to help people correct in their projects over time, where they used a dropdown element, and find that as they add functionality, they need that data to be in a table.

The other pitfall to avoid, when using tables instead of dropdowns, is using anything other than the primary key ('id') as the "value" of the join. The temptation in your case would be to use the 'label' ('MYR') as the value you store for the join, because it seems intuitive to store the actual currency name in your currency element. But that's not how relational databases work. Always use the 'id', so it's an actual FK (foreign key).

-- hugh
 
I shall heed your advice and spend my weekends fixing as suggested. Thank you once again.

There goes my weekend.
 
Lol. I know that feeling.

But honestly, it's not that hard. Kind of depends how much logic you already have that depends on it being a dropdown, with "amount" values rather than FK's. Actually creating the new list, populating it and changing the element is trivial.

Note that if you have existing data that you don't want to lose, you can fix that in phpMyAdmin, by running commands by hand. So if 'yourtable' has a field called 'currency' which currently has values like '3.96' ... and you've added a row to your new table for that which has 'id' of (say) 2 ...

UPDATE mytable SET currency = 2 WHERE currency = '3.96';

Do this BEFORE you change the element type in Fabrik, as when you change it to a join, we'll change the field type to INT, which will lose any non integer values.

-- hugh
 
Oh, and of course ... Akeeba Backup Is Your Friend. Back your database up before doing anything potentially destructive.

-- hugh
 
Oh, and of course ... Akeeba Backup Is Your Friend. Back your database up before doing anything potentially destructive.

-- hugh
I can't thank you enough. Everything went well. Not only that, but managed to understand the power of Fabrik more.




Sent from my iPhone using Tapatalk
 
As a personal aside, I almost always use a databasejoin to a reference table rather than a dropdown element, as experience has taught me that it is far easier to tweak values in a table than to go in and update Fabrik elements every time I need to change the dropdown list.

You also get to do front-end additions (whereby users can add to the list) and complicated ajaxed WHERE clauses etc. that you cannot do with dropdown elements.
 
I guess one reason I initially opted for the dropdown type is that I did not know how to read the values from the database, programming-wise. Thanks to Hugh, I learned how to do it, and I converted the elements to databasejoin where I thought it would right.

A few minutes ago, I finished the Products module where 12 different price calc elements were used based on a databasejoin element. I must say, I am pleased with the progress, but it has been a very slow one. I still have few challenges, and hopefully, before this year ends, version 1.0 of my custom-made ERP is done.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top