Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might be confusing AutoComplete and Search Term Highlighting. Do you want to:</p> <ol> <li>Slowly reveal possible text choices as the user types more letters (i.e. Google Suggestions) -OR-</li> <li>Highlight the already searched for term in a search results page</li> </ol> <p>Or maybe you want to do both, but it looks like you're talking about Highlighting from the sample code, although your question is about AutoComplete...</p> <p>Anyway here's some code for Autocomplete of the description and Search Term highlighting:</p> <pre><code>&lt;?php $q = filter_var($_REQUEST['q'], FILTER_SANITIZE_STRING); $query = (isset($q) &amp;&amp; !empty($q)) ? $q : ""; ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8" /&gt; &lt;title&gt;Autocomplete/SearchHighlighting Example&lt;/title&gt; &lt;link type="text/css" href="css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" /&gt; &lt;style type="text/css"&gt; span.highlighted { background-color: #FFF6C6; font-weight: bold; } span.term0 { color: #161633; } span.term1 { color: #331616; } span.term2 { color: #163316; } &lt;/style&gt; &lt;script type="text/javascript" src="js/jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="js/jquery-ui-1.8.20.custom.min.js"&gt;&lt;/script&gt; &lt;!--[if lt IE 9]&gt; &lt;script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt; &lt;![endif]--&gt; &lt;style type="text/css"&gt; header, section, footer, aside, nav, article, figure, figcaption, audio, video, canvas { display:block; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="search" name="search"&gt; &lt;label for="q"&gt;Search: &lt;/label&gt;&lt;input type="text" id="q" name="q" value="&lt;?php echo $query; ?&gt;" placeholder="Enter a search term" /&gt; &lt;/form&gt; &lt;div id="RecentEdition"&gt; &lt;?php $schools = simplexml_load_file('ecoles.xml'); foreach ($schools-&gt;RecentEdition as $RecentEdition): foreach ($RecentEdition-&gt;school as $school): ?&gt; &lt;figure&gt; &lt;img src='&lt;?php echo "{$school-&gt;image} \n"; ?&gt;' title='' /&gt; &lt;figcaption&gt; &lt;h3&gt;Contents&lt;/h3&gt; &lt;p class="over"&gt; &lt;ul id="results"&gt; &lt;?php foreach ($school-&gt;content as $content): ?&gt; &lt;?php foreach ($content-&gt;chap as $chap): ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo "{$chap['link']} \n"; ?&gt;"&gt;&lt;?php echo "{$chap} \n"; ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;?php endforeach; ?&gt; &lt;/ul&gt; &lt;/p&gt; &lt;p class="desc"&gt;&lt;?php echo "{$school-&gt;description} \n"; ?&gt;&lt;/p&gt; &lt;p class="go"&gt; &lt;a href="&lt;?php echo "{$school-&gt;link} \n"; ?&gt;"&gt;View &amp;#187;&lt;/a&gt; &lt;/p&gt; &lt;/figcaption&gt; &lt;/figure&gt; &lt;?php endforeach; ?&gt; &lt;?php endforeach; ?&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $(function() { //Autocomplete $("#q").autocomplete({ //define callback to format results source: function(req, add){ //pass request to server $.getJSON('search.php?callback=?', req, function(data) { var suggestions = []; //create array for response objects //process response $.each(data, function(i, terms){ suggestions.push(terms.term); }); add(suggestions); //pass array to callback }); }, //define select handler: Search Term Highlighting function select: function(match, keywords) { var rawSearchString = $('#q').val().replace(/[a-zA-Z0-9\?\&amp;\=\%\#]+s\=(\w+)(\&amp;.*)?/,"$1"); // Return sanitized search string if it exists (term should be FIRST URL PARAMETER) var searchString = rawSearchString.replace(/ /g,"\|").replace("?term=",""); // Replace '+' and '%20' with '|' for regex var searchTerms = searchString.split('|'); // Split search terms on '|' and iterate over resulting array for (var j in searchTerms) { var regex = new RegExp("&gt;([^&lt;]*)?("+searchTerms[j]+")([^&gt;]*)?&lt;", "ig"); // this regex is the secret, it prevents text within tag declarations from being affected var tempHTML = $('#RecentEdition').html(); // Do regex replace $('#RecentEdition').html(tempHTML.replace(regex, '&gt;$1&lt;span class="highlighted term'+j+'"&gt;$2&lt;/span&gt;$3&lt;')); // Inject span with class of 'highlighted termX' for term highlighting } } }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And the PHP search file (call it "search.php") using xPath: </p> <pre><code>&lt;?php header('content-type: application/json; charset=utf-8'); $term = urldecode($_REQUEST['term']); $callback = $_GET["callback"]; $html = new DOMDocument(); @$html-&gt;load("ecoles.xml"); $xpath = new DOMXPath($html); $query = "//schools/RecentEdition/school/description[contains(.,'".$term."')]"; $nodelist = $xpath-&gt;query($query); $i = 0; foreach ($nodelist as $n) { $result = trim($n-&gt;nodeValue); $resultArray[$i] = array( "term" =&gt; str_replace('"',"'",substr($result,strpos($result,$term),25)) ); $i++; } $resultJSON = json_encode($resultArray); echo $callback."(".$resultJSON.")"; ?&gt; </code></pre> <p>DEMO here: <a href="http://bcmoney-mobiletv.com/widgets/autocomplete/" rel="nofollow">http://bcmoney-mobiletv.com/widgets/autocomplete/</a></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. 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