1

I have a module which has dropdown select boxes. I have a table in the database, which is associated to a component, which the module is associated with.

If the component database table has data in it, how can I get the module to display the data in a dropdown and if selected it will show the results on a separate page?

For example, I have the following dropdowns in the module:

    <label for="airport">Airport:</label>
        <select id="airport" name="airport">
            <option selected="selected" value="A">Any Airport</option>
            <option value="LGW">Gatwick</option>
            <option value="MAN">Manchester</option>
        </select>

In the database, there is a table called "dep", it has either MAN or LGW in it. If the user selects LGW from the dropdown and then clicks search, I want the component to load all of the data from the database which has got LGW in the table.

I have set the following Database query in the module, but how can I use this to show the data in the dropdowns and then show the correct results?

$db = JFactory::getDbo();

$query = $db->getQuery(true);

$query->select($db->quoteName(array('mykey', 'hotelName', 'resortName', 'dep',     'startDate', 'sellingPrice')));
$query->from($db->quoteName('#__availability'));
$query->order('ordering ASC');


$db->setQuery($query);

$results = $db->loadObjectList();
3
  • Currently what data the module's drop-down is showing? Meaning, you can definitely query the database from within the module. But what exactly you are after? Do you want to customize the module and change what it's doing, you want to create it from scratch, or you just need general info about the approach? Please clarify, otherwise the question is to broad and not clear. In case you are asking for code, you should also present some code here.
    – FFrewin
    Commented Sep 9, 2014 at 11:32
  • The update of your question gave a whole new dimension of what you are asking. So currently the module is already able to display data from your table. All you need is to make the component showing its content which its column has the chosen value from the dropdown?
    – FFrewin
    Commented Sep 9, 2014 at 11:45
  • The module isn't showing any data from the table, it's hardcoded that way, would be good to get the module to display the data (in the dropdowns) and then if chosen take the user to the results. Commented Sep 9, 2014 at 11:47

1 Answer 1

3

You can fill the from like below:

<label for="airport">Airport:</label>
<select id="airport" name="airport">
    <option selected="selected" value="A">Any Airport</option>
<?php
foreach ($results as $result)
{
    echo "<option value='{$result->dep}'>{$result->dep}</option>";
}
?>
</select>

Then process the form regularly or by an AJAX method.

9
  • Thanks very much, if I have lots of items in there set to the same Airport, it's bringing up multiple versions of the Airports, so rather than just having LGW and MAN, it's bringing up all of the records, so there are lots of LGW and lots of MAN, any ideas how I can only show one of each without the repeats? Commented Sep 9, 2014 at 19:12
  • 1
    Use DISTINCT keyword. For example: $query = "SELECT DISTINCT dep FROM #__availability ORDER BY ordering ASC";
    – Farahmand
    Commented Sep 9, 2014 at 19:56
  • Or use group() method. For example: $query->group(dep);.
    – Farahmand
    Commented Sep 9, 2014 at 20:02
  • Thanks, that works great. However, how can I use the $query->group on multiples? So if I want to group 'dep' and 'resortName' ? Commented Sep 10, 2014 at 6:14
  • Try this: $query->group(array('dep', 'resortName'));.
    – Farahmand
    Commented Sep 10, 2014 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.