• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Fill dropdown with data depending on dropdown

Fill dropdown with data depending on dropdown 10 months 3 weeks ago #42750

  • info5963
  • info5963's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 1
I have 2 dropdowns that need to be populated with data from a database depending on each other.

I have the following code in Dropdown-A:

//<code>
$items = array();
$db = JFactory::getDbo();
$db->setQuery("SELECT distinct medium FROM #table.`medien` ");
$results = $db->loadObjectList();
foreach ($results as $result) {
$value = $result->medium;
$label = $result->medium;
$items[] = $value.'|'.$label;
}
$items = implode("\n", $items);
return $items;
//</code>


In dropdown-B:

//<code>
$items = array();
$db = JFactory::getDbo();
$db->setQuery("SELECT artikel FROM #table.`medien` WHERE medium = {Dropdown-A} ");
$results = $db->loadObjectList();
foreach ($results as $result) {
$value = $result->artikel;
$label = $result->artikel;
$items[] = $value.'|'.$label;
}
$items = implode("\n", $items);
return $items;
//</code>

When a media is selected from dropdown-A, dropdown-B should be filled accordingly.
Unfortunately it does not do that?
The administrator has disabled public write access.

Fill dropdown with data depending on dropdown 10 months 3 weeks ago #42751

Hello,I'm trying to achieve the exact same thing currently. (also in German ;) ).

But I'm using this code from the documentation to pull the data from the table:
//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Master-LP wählen[c]";
 
// Run the SQL query and store it in $results
$db->setQuery("SELECT id, title FROM #__content WHERE catid = 30");
$results = $db->loadObjectList();
 
// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. m|M-sized T-shirt
foreach ($results as $result) {
  $value = $result->id;
  $label = $result->title;
  $items[] = $value.'|'.$label;
}
 
// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);
 
// Now we need to return the value to the field
return $items;
//</code>

I'd appreciate an answer, too. Furthermore I'd like to know how to control the selection of the second dropdown if it's a static dropdown and not a populated dropdown from the db.
Last Edit: 10 months 3 weeks ago by T.Venugopal. Reason: typo
The administrator has disabled public write access.

Fill dropdown with data depending on dropdown 10 months 3 weeks ago #42767

Solution for me. But it's extended to query a custom field and binding it to another dropdown.
//<code>
// Ersetze die Array-Einträge mit den gewünschten Artikel-IDs
$article_ids = array(64, 968, 428,196, 197);
 
$results = array();
 
foreach ($article_ids as $id) {
    $db = JFactory::getDBO();
    $db->setQuery("SELECT value FROM #__fields_values WHERE item_id='$id' AND field_id='8'");
    $result = $db->loadObjectList();
 
    // Lade den Titel des Artikels
    $db->setQuery("SELECT title FROM #__content WHERE id='$id'");
    $title = $db->loadResult();
 
    // Füge das Ergebnis der Ergebnisliste hinzu
    $results[] = array('value' => $result[0]->value, 'label' => $title.' ('.$result[0]->value.')');
}
 
// Konvertiere das Ergebnis in ein RSform Pro-lesbares Format
$items = array();
$items[] = 'Master-LP mit Standard-WBKZ wählen'; // Platzhalter hinzufügen
foreach ($results as $result) {
    $value = $result['value'];
    $label = $result['label'];
    $items[] = $value.'|'.$label;
}
 
return implode("\n", $items);
 
//</code>

and optionally binding them:
<script>
// Die Funktion setWbkzDefault lädt das Standard-WBKZ abhängig von der ausgewählten Produkt-Option in das WBKZ-Standard-Dropdown-Feld.
function setWbkzDefault(productDropdown, wbkzDefaultDropdownId) {
var wbkzDefaultDropdown = document.getElementById(wbkzDefaultDropdownId);
var selectedOptionIndex = productDropdown.selectedIndex;
 
// Erstellen eines Arrays, das die Indizes der WBKZ-Optionen enthält.
var wbkzOptions = [0, 1, 2, 3, 4, 5];
 
// Verwenden des ausgewählten Produktindex, um das Standard-WBKZ-Dropdown auszuwählen.
wbkzDefaultDropdown.selectedIndex = wbkzOptions[selectedOptionIndex];
}
</script>
Last Edit: 10 months 3 weeks ago by T.Venugopal. Reason: script
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!