0

how to get value for search in joomla module.

Does anyone know how to get the value for the search field? my problem is that after i search for specific data it still shows everything

In my helper.php I have this code. but doesn't work


    public function getFilterForm()
    {
        JHtml::_('bootstrap.tooltip');
        JHtml::_('behavior.multiselect');
        JHtml::_('formbehavior.chosen', 'select');

        Form::addFormPath(dirname(__FILE__) . '/forms');

        $form = Form::getInstance('module.filter', 'filter');


        return $form;
    }


        public function getFieldsvalue($params)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('*')
                ->from('#__users');

        $db->setQuery($query);
        $rows = $db->loadObjectList();

        return $rows;
    }

    public function loadFormData()
    {
        $data = $rows->getFieldsvalue();

        return $data;
    }

I have a folder forms and inside it has filter.xml

in the tmpl folder. the in the default.php this is my code

<form name="adminForm" id="adminForm">
    <?php echo JLayoutHelper::render('joomla.searchtools.default', array ('view' => (object) array ('filterForm' => $form))); ?>

    <table class="table table-striped">
  <thead class="thead-light">
    <tr>
      <th scope="col">#</th>
      <th scope="col">Name</th>
    </tr>
  </thead>
  <tbody>
      <?php foreach  ($rows as $item) {?>
    <tr>
      <th scope="row"><?php echo $item->id; ?></th>
      <td><?php echo $item->name; ?></td>
    </tr>
      <?php } ?>
  </tbody>
</table>
</form>

bumped to the homepage by Community 14 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • 1
    Are you trying to filter the data from #__users table or are you tryng to get the filter value from #__user? It is not really clear what your code is trying to do. Maybe if you provide more explanation of what you are trying to achieve would help. Update your post with the contents of the filter .xml you mention in your /forms folder and the name of the table with the data you are trying to filter. – Irata Oct 19 at 11:34
  • in the view.. if i enter data on search field, how to get the data in the helpers.php? because i want to use it for my Where clause sql – Rhea Lorraine Oct 19 at 11:54
0

The basics of what you need are in your filter .xml file there is a name= field and that is where the text you want to filter your list will be stored. In the below example it is 'search.

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="filter">
        <field name="search" type="text" hint="COM_INVENTORY_SEARCH_FILTER_SUBMIT"
               label="COM_USERS_FILTER_SEARCH_DESC"
               class="js-stools-search-string" 
               />
    </fields>
</form>

Because haven't answered my earlier question about if you are trying to filter the #__user table I can't be more specific other than to say normally your data base call would or should be be in a model and not a helper. This would extend JModelList that has a few auto-magical things happening in the background to help to wen dealing with data and tables.

Either way in your function to query the table you need to have something like this line to retrieve the value from the search field from the User State object where it should have been stored from your form. // Filter by search $search = $this->getState('filter.search');

And then you need to build add the Where clause to the query with something like this

    if (!empty($search))
    {
        if (stripos($search, 'id:') === 0)
        {
            $query->where('a.id = ' . (int) substr($search, 3));
        }
        else
        {
            $search = $db->Quote('%' . $db->escape($search, true) . '%');
        $query->where('( a.ItemCode LIKE ' . $search . '  OR  a.GenericCode LIKE ' . $search . '  OR a.Description LIKE ' . $search . '  OR  a.SupplierName LIKE ' . $search . ' )');
        }
    }

There is not enough of your code or an explanation to tell if your Form is correctly passing your value from the Filter/search box to the userState object but this might be enough information above to help you progress.

  • $search = $this->getState('filter.search');... this works in component. but in module it doesnt work. – Rhea Lorraine Oct 19 at 12:27
  • Without more details, and example or a link to what you have got then I have to keep guessing. You could create an input field on your form to receive a value for the filter and then once the form is submitted then access $_POST['input'] to get the field and put that field into your Where clause. – Irata Oct 19 at 12:47
  • As another example, mod_login uses this code in the com_user/controllers/user.php to retrieve the username and password fields when you login, $app = JFactory::getApplication(); $input = $app->input->getInputForRequestMethod(); – Irata Oct 19 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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