PageRenderTime 1ms CodeModel.GetById 102ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/demo/search.php

https://code.google.com/p/jquery-autocomplete/
PHP | 51 lines | 27 code | 6 blank | 18 comment | 6 complexity | d99fe4a6aad6e3f1bae4803072ac414d MD5 | raw file
 1<?php
 2
 3/*
 4 * Load sample data
 5 */
 6include 'data.php';
 7
 8/*
 9 * Results array
10 */
11$results = array();
12
13/*
14 * Autocomplete formatter
15 */
16function autocomplete_format($results) {
17    foreach ($results as $result) {
18        echo $result[0] . '|' . $result[1] . "\n";
19    }
20}
21
22/*
23 * Search for term if it is given
24 */
25if (isset($_GET['q'])) {
26    $q = strtolower($_GET['q']);
27    if ($q) {
28        foreach ($data as $key => $value) {
29            if (strpos(strtolower($key), $q) !== false) {
30                $results[] = array($key, $value);
31            }
32        }
33    }
34}
35
36/*
37 * Output format
38 */
39$output = 'autocomplete';
40if (isset($_GET['output'])) {
41    $output = strtolower($_GET['output']);
42}
43
44/*
45 * Output results
46 */
47if ($output === 'json') {
48    echo json_encode($results);
49} else {
50    echo autocomplete_format($results);
51}