3

I'm experimenting with creating my own Joomla component. The component searches records in database tables, but I have a problem with how to show them.

I have a search form that sends the search phrase to the controller.php.

<form action="<?php echo JRoute::_('index.php'); ?>" method="post" class="form-inline">
    <input name="company" id="form_hladat" maxlength="150" class="inputbox search-query input-medium" type="search" placeholder="hladat" />
    <input type="hidden" name="task" value="search" />
    <input type="hidden" name="option" value="com_regne" />
</form>

In the controller.php I have a search function. The function performs a search and redirects me to view basic.

public function search() {
    // Slashes cause errors, <> get stripped anyway later on. # causes problems.
    $badchars = array('#', '>', '<', '\\');
    $searchword = trim(str_replace($badchars, '', $this->input->getString('company', null, 'post')));

    $db = JFactory::getDbo();       
    $query = $db->getQuery(true);
    $query->select($db->quoteName(array('cin','tin','vatin','name','formatted_address','street','reg_number','building_number','postal_code','municipality','country','established_on','terminated_on','vatin_paragraph','registration_office','registration_number','formatted_street','street_number','rpo_organizations_id')))
        ->from($db->quoteName('corporate_bodies'))
        ->where($db->quoteName('cin') . ' LIKE '. $db->quote($searchword) . ' OR ' . $db->quoteName('name') . ' LIKE ' . $db->quote('%'.$searchword.'%'));
    $db->setQuery($query);
    $rows = $db->loadObjectList();
    $results = $rows;
    foreach ($results as $result):
        $name = $result->name;
        $cin = $result->cin;
        $rpo_id = $result->rpo_organizations_id;
        $company_demise = $result->terminated_on;
    endforeach;

    $view = $this->getView('basic','html');
    $view->results = $results;
    $view->display();
    //$this->setRedirect(JRoute::_('index.php?option=com_regne&view=basic', false));
}

And that's where my problem starts. I do not know how to get search results (array).

1

Use your controller only for redirection purpose. For database stuff use your view model. Make sure your view name, controller file name, model file name are same.

Form:

<form method="post" class="form-inline">
    <input name="company" id="form_hladat" maxlength="150" class="inputbox search-query input-medium" type="search" placeholder="hladat" />
    <input type="hidden" name="task" value="search.mySearch" />
    <input type="hidden" name="option" value="com_regne" />
</form>

components/com_regne/controllers/search.php

function mySearch() {
    $jinput = JFactory::getApplication()->input;
    $term = $jinput->get('company', '', 'STRING');
    $this->setRedirect(JRoute::_('index.php?option=com_regne&view=search&q=' . $term, false));
}

components/com_regne/models/search.php

function getSearch() {

    $term = JRequest::getVar('q');
    $db = JFactory::getDbo();       
    $query = $db->getQuery(true)
        ->select('*')
        ->from($db->quoteName('corporate_bodies'));

    if (!empty($term)) {
        $query->where(
            [$db->quoteName('cin') . ' = ' . $db->quote($term),
            $db->quoteName('name') . ' LIKE ' . $db->q('%' . $db->escape($term, true) . '%', false)],
            'OR');
    }

    return $db->setQuery($query)->loadObjectList();
}

components/com_regne/views/search/view.html.php in display function

$this->results = $this->get('Search');

components/com_regne/views/search/tmpl/default.php

$this->results will contain your entire search results.

  • So what is the null function? Did you mean is_null? BTW, I prefer to use if(isset($x) && !empty($x)) or something like that. – Farahmand Nov 11 '17 at 12:17
  • I have edited the controller and model function as well. Now have a check – user2980415 Nov 12 '17 at 1:51
0

In the display function of the view, you should see the results in $this->results. It should work. If it doesn't display any data, then this means that most likely your $results array is empty. By the way, your foreach loop has no effect whatsoever on $results. In fact, if this is the whole code, then it has no effect whatsoever.

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.