Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've come to a solution that I'm fairly happy with.</p> <p>Firstly created a view containing the searchable address fields concatenated:</p> <pre><code>CREATE VIEW address_concat AS SELECT address_id, CONCAT_WS(' ', address_name, address_line_1, address_line_2, suburb, postcode, state, country) AS full_address FROM address </code></pre> <p>When a request with a search string is received, I parse it in PHP and use the view to find matches (the following code is cleaned up and removes non-relevant things - the data is sanitised, etc):</p> <pre><code>$terms = explode(' ', preg_replace('/\s+/', ' ', $_GET['find_address'])); //get array of terms $where = " WHERE full_address LIKE '%".implode("%' AND full_address LIKE '%", $terms)."%' "; //turn array into WHERE clause //query database to find matches $result = $db-&gt;query("SELECT a.* FROM address a JOIN address_concat ac ON a.address_id = ac.address_id ".$where." ORDER BY IFNULL(IF(address_name = '', NULL, address_name), address_line_1)"); if ($result-&gt;num_rows &gt; 0) { //construct output $output = '&lt;ul&gt;'; while ($row = $result-&gt;fetch_assoc()) $output .= '&lt;li val_id="'.$row['address_id'].'"&gt;'.make_address(...).'&lt;/li&gt;'; $output .= '&lt;/ul&gt;'; } else $output = '&lt;p&gt;No matches found.&lt;/p&gt;'; </code></pre> <p>On the client side, I used some wisdom from <a href="https://stackoverflow.com/questions/4220126/run-javascript-function-when-user-finishes-typing-instead-of-on-key-up">other</a> <a href="https://stackoverflow.com/questions/14042193/how-to-trigger-an-event-in-input-text-after-i-stop-typing-writting">questions</a> to make it only query after a pause in typing.</p> <pre><code>var typingTimer; $('input#filterbox').bind('input', function () { var textfield = $(this); //code to hide/show "clear text field" box eliminated, etc clearTimeout(typingTimer); typingTimer = setTimeout(function () {doFilterUpdate(textfield);}, 750); }); function doFilterUpdate (target) { if (target.val().length &gt;= 3) $('div#resultlist').load('inc/ajax.php?find_address=' + encodeURIComponent(target.val())); else $('div#resultlist').html(''); } </code></pre> <p>I'm still not certain if creating the view is necessary/more efficient than just writing a query on the address table that uses CONCAT_WS in the WHERE clause... it does make the query a lot simpler, though :P</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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