自动填充的每个结果的图片

时间:2017-05-20 08:38:56

标签: php mysql image web autocomplete

我如何拥有每个结果的图像?我搜索了许多网站和许多教程,但我无法理解这些教程。

这是我的索引

 <nav>
        <div class="nav-wrapper">
          <form>
            <div class="input-field">
              <input id="vyhledat" class="form-control input-lg" type="search" placeholder="Vyhledat film..." />
              <label class="label-icon" for="search"><i class="material-icons">search</i></label>
              <i class="material-icons">close</i>
            </div>
          </form>
        </div>
      </nav>

这里是我的剧本

<!--  js -->
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
    $(document).ready(function(){

        $('#vyhledat').autocomplete({
            source: "php/vyhledat.php",
            minLength: 3,
            select: function(event, ui) {
                var url = ui.item.id;
                if (url != '#') {
                    location.href = url
                }
            },
            open: function(event, ui) {
                $(".ui-autocomplete").css("z-index", 1000)
            }
        })

    });
    </script>

这是我的PHP代码

$keyword = trim($_REQUEST['term']); // input

$sugg_json = array(); 
$json_row = array(); 


$keyword = preg_replace('/\s+/', ' ', $keyword); 

$query = 'SELECT id, nazev, href, obrazek FROM filmy WHERE nazev LIKE :term'; // query

$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':term'=>"%$keyword%"));

if ( $stmt->rowCount()>0 ) {

    while($recResult = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $json_row["id"] = $recResult['href'];
        $json_row["value"] = $recResult['nazev'];
        $json_row["label"] = $recResult['nazev'];
        array_push($sugg_json, $json_row);
    }

} else {
    $json_row["id"] = "#";
    $json_row["value"] = "";
    $json_row["label"] = "Žádný výsledek";
    array_push($sugg_json, $json_row);
}

$jsonOutput = json_encode($sugg_json, JSON_UNESCAPED_SLASHES); 
print $jsonOutput;

当然还有与我的数据库的连接。

$DBhost = "localhost";
$DBuser = "jirkua";
$DBpass = "koralek1";
$DBname = "jirkua";

try {
    $DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
    $DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
    die($ex->getMessage());
}`

0 个答案:

没有答案