Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP: Automatic search on pre-selected dropdown value
    primarykey
    data
    text
    <p>I have a search form which has, a text input box, three checkboxes and a preselected default value in a dropdown based on user data from the database. e.g. If a user lives in commune 1, then 1 is selected as a default value in the dropdown.</p> <p>I want CakePHP to perform a search in the database filtered by that value, and return a paginated result. That's easy enough when pushing a submit-button, but I want to perform the search on page load without user interaction.</p> <p>Now, in the controller I've tried to get the commune value from another place than the dropdown with:</p> <pre><code>if ($this-&gt;request-&gt;is('post)) { //Perform normal search with the other input fields included. } else { //Do the filtered search only by commune value, which I get from a function. } </code></pre> <p>Problem is, then the pagination will not work. This is expected as pagination uses GET. When I try to change page, it is not a post, and the search conditions will be set to that of only the commune value value again, and I get an error in the SQL statement.</p> <p>I'm sorry if my explanation above is a bit messy, but you'll have to excuse me since english is not my first language.</p> <p>I need suggestions of how to do this in another way. Is it at all possible? I suspect there is an easy solution to this, but I'm new to CakePHP, and can't seem to get it.</p> <h3>Performing search</h3> <pre><code>$conditions = $this-&gt;setSearchConditions($this-&gt;request-&gt;data); $res = $this-&gt;paginate('Ad', array($conditions)); $this-&gt;set('res', $res); //limit is set in public $paginate variable </code></pre> <h3>Setting the search conditons</h3> <pre><code>private function setSearchConditions($data) { $conditions = array(); // $this-&gt;log('Search: DATA', 'debug'); //$this-&gt;log($data, 'debug'); if ($this-&gt;request-&gt;is('post)) { //Submit-button is clicked, performing full search //$this-&gt;log('Dette er en post', 'debug'); if ($data['Ad']['searchField']) { //Text searchfield is not empty, adding title or description to search criteria $this-&gt;log('Søkefeltet er ikke tomt', 'debug'); $str_search = '%' . $data['Ad']['searchField'] . '%'; $conditions[] = array( 'OR' =&gt; array( 'Ad.title LIKE' =&gt; $str_search, 'Ad.description LIKE' =&gt; $str_search ) ); }//if if ($data['Ad']['commune_id']) { // Commune dropdown is not empty, adding commune_id to search criteria $conditions[] = array( 'Ad.commune_id' =&gt; $data['Ad']['commune_id'] ); }//if if ($data['Ad']['type_id']) { // Type checkboxes are not empty, adding type_id to search criteria $orArray = array(); foreach ($data['Ad']['type_id'] as $type) { $orArray[] = array('Ad.type_id' =&gt; $type); } $conditions[] = array( 'OR' =&gt; $orArray ); }//if } else { $conditions[] = array( 'Ad.commune_id' =&gt; $this-&gt;getDefaultCommune(); ): } return $conditions; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload