Forums

Fill dropdown dynamically with result from query

Erik66 18 Aug, 2013
Hello,

I have an application where I need a dropdown to be filled with the result of a query. This otherwise excellent tutorial by Greyhead ( http://www.chronoengine.com/faqs/2647-how-do-i-build-a-select-drop-down-radio-button-or-checkbox-group.html ) explains how to do it with a database table. I tried applying the same procedure but either the dropdown remains blank, or the page becomes garbled and all surrounding template(s) disappears.

Is there any way this could work with a query result instead of a database table ?

Thanks,

Erik
GreyHead 19 Aug, 2013
Hi Erik,

It will work form either a MySQL query or from the result of the DB MultiRecord Loader (which is a wrapper for a MySQL query).

The problem you are seeing sound link there may be a PHP Error, perhaps the query is failing. Do you see anything useful like an error message if you view the page source and/or set the site Error Reporting to Maximum.

Bob
Erik66 19 Aug, 2013
Hello Bob,

I thought about that too and have tested the query in PHPmyAdmin seperately where it works without problems. I tested it in the form by adding a small 'report' table that outputs the correct data every time. So the query seems to be working OK. As the report ios generated corectly as well and as it is generated using PHP, I think we can assume PHP errors are not at hand. Unfortunately I can not turn on error reporting to maximum.

Perhaps the first thing to ask is if I use the fields in the Dynamic Data tab correctly.

My practical example: I run a query in a custom element first, that generates a list with 3 columns. The columns represent a person's ID number (internal company ID), person's first name and a person's department name. They're called 'person_id' , 'person_firstname' and 'person_department'. The query is generated based on the user_id of the logged-in user and basically filters a list of co-workers.

The PHP code that does this looks like this:

<?php

$user =& JFactory::getUser();
$form->data['user_id'] = $user->id;

$db =& JFactory::getDBO();
$query = "

QUERY IS INSERTED HERE

";
$db->setQuery($query);
$form->data['list_data'] = $db->loadObjectList();

?>

As said the QUERY INSERTED HERE is Ok and works in PHPmyAdmin without errors.

I would like to be able to dynamically select a person's name from a dropdown menu while getting the persons 'id' as value in the form. I also want to pass all 3 field values of the person selected to the form so I can store them in a database.

From the description of the Dynamic Data tab I get that the Text Key is the value shown in the frontend of the dynamic pulldown. But which values do I insert in the 'Data Path' and 'Value Key'. If I insert 'list_data' (without quotes)in the Data Pathj field, the form gets mangled and only the dynamic pulldown is shown (all other form elements are gone then). If i leave Data Pathj blank the pulldown only shows the 'Show Empty' text string as the only available option.

Perhaps if we get this correct first then it would be a good moment to start digging into error logs ?

Thank you!


Erik
Erik66 24 Aug, 2013
Hello Bob,

still trying to get this thing working but no luck.

I first added a little reporting to the custom code. After the

$db->setQuery($query);
$form->data['list_data'] = $db->loadObjectList();

lines I added:

echo "Results : <br />";
echo "<br />";
print_r(array_values($form->data['list_data'])) ;

If I disable the dynamic dropdown, the screen output then shows:

Array ( [0] => stdClass Object ( [employee_id] => 1 [employee_name] => Name 1 [department] => Department 1 ) [1] => stdClass Object ( [employee_id] => 2 [employee_name => Name 2 [department] => Department 1 ) [2] => stdClass Object ( [employee_id] => 3 [employee_name] => Name 3 [department] => Department 2 ) )

But if I enable the dynamic dropdwon, this output disappears and an incomplete form appears.

When I then look at the server PHP error log I get the following mesaage:

[Sat Aug 24 16:13:56 2013] [warn] [client ---.---.---.---] mod_fcgid: stderr: PHP Fatal error: Cannot use object of type stdClass as array in /var/www/vhosts/xxx.xx/subdomains/xxxxxxx/httpdocs/administrator/components/com_chronoforms/form_actions/show_html/cfaction_show_html.php(142) : eval()'d code on line 66, referer: http://xxxxxxx.xxx.xx/administrator/index.php?option=com_chronoforms&task=form_wizard&form_id=103

Does this give us any clue as to hwat is going on ?

Thanks, best regards from

Erik
GreyHead 25 Aug, 2013
Hi Erik,

It looks as though you need loadAssocList() to get an array of arrays; instead of loadObjectList() which gives an array of objects. Both are useful in different places.

Bob
Erik66 25 Aug, 2013
Hello Bob,

thank you. That did the trick ! Worked immediately as intended after changing that code.

as I am not a programmer I would know the one from the other and when to use which one. Maybe we can put together an example for futher reference and tutorial ? Happy to supply code an screenshots if you like.

Super, thanks a lot.

Erik
GreyHead 27 Aug, 2013
Hi Erik,

I had the same problem a while ago and wrote (or mostly wrote) this page in the Joomla! docs. This was written for Joomla! 1.5 but I think all of it(?) is still valid in Joomla! 3.

Unfortunately the doc is now buried - and don't get me started about the useless updates to the Joomla! docs in recent times :-(

Bob
AntonioGraca 05 Sep, 2013
Hi, erik66

It is possible to put the all steps and the all code to fill, dynamically, a drop down (or 3 drop down) from selection of other drop down source?

Thanks
António Graça
GreyHead 08 Sep, 2013
Hi António,

I posted an example recently here. Does that help?

Bob
This topic is locked and no more replies can be posted.