简体   繁体   English

从数据库中进行下拉搜索,获取不同型号/品牌的图像并显示

[英]Dropdown search from database, get images for different model/brand and display it

My problem is to display an image that is associated with the mysql results... Say the result has ID=250 and Name=brand.model, then I want a piece of code to search in a folder for an image with the name id_brand.model.jpg on the server. 我的问题是显示与mysql结果关联的图像...说结果具有ID = 250和Name = brand.model,那么我想要一段代码在文件夹中搜索名称为id_brand的图像服务器上的.model.jpg。

Database structure is id, master, name. 数据库结构是id,master,name。 When user selects Brand + Model from dropdown it should get image from folder (could also get image from database, which one is better nowadays) and images all have unique name's what should be echoed as part name. 当用户从下拉菜单中选择“品牌+型号”时,它应该从文件夹中获取图像(也可以从数据库中获取图像,如今这种情况更好),并且图像均具有唯一名称,该名称应作为部件名称回显。

(Pictures to help understanding what i mean http://imgur.com/a/7XwVd ) (图片有助于理解我的意思http://imgur.com/a/7XwVd

Here's pastebin's to what i have coded yet. 这是我已经编码的pastebin。 http://pastebin.com/kQF2qP64 http://pastebin.com/kQF2qP64

Any help is appreciated. 任何帮助表示赞赏。

First you need to bind the change event of the select to send the name to a function to search the file, then append the file/s to the DOM: 首先,您需要绑定selectchange事件,以将名称发送给函数以搜索文件,然后将文件附加到DOM:

Javascript (Ajax) Javascript(Ajax)

// Every time a category is selected we request the files
$('#category').on('change', function() {
    // If we have an element with images loaded, lets delete it
    var searchResult = $("#search-result").empty();
    // Now we serialize the form data, add an id to the form
    var searchBrand = $('#gender').find('option:selected').text();
    var searchModel = $('#category').find('option:selected').text();
    var fileName = searchBrand + '.' + searchModel + '.jpg';
    var searchData = { filename: fileName }
    // Now we create the ajax request with the form data
    var request = $.getJSON('search_images.php', searchData);
    // If the request success we show the images
    request.done(function(data) {
        // For each image found we add a new image to the DOM
        $.each(data, function(index, image_uri) {
            // First let's create the image element
            var img = $("<img>", {
                "class": "result-image",
                "src": image_uri
            });
            // Then we append it to the DOM
            searchResult.append( img );
        });
    });
    // If the search fails, we notify that no results were found
    request.fails(function() {
        alert('No results found');
    });
});

PHP (search_images.php) PHP(search_images.php)

<?
// Get the file name
$filename = $_GET['filename'];
// Find all the images
$images = glob("images/file/path/*_{$filename}");
// Return the images as json
echo json_encode($images);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM