Расширенный поиск с помощью ajax, php и mysql

Я строю расширенный поиск с помощью ajax, php и mysql. ниже приведен рабочий код для формы поиска (html) вызова AJAX и сценария php, который обрабатывает запрос. Тем не менее, проблема, с которой я сталкиваюсь, заключается в том, как сделать динамический запрос MySQL.

Например, если пользователь выполняет поиск «Hilton Hotel» в поисковой строке, в качестве industry_types и «Отель» как sub_industry_type и выбирает «Лондон» в качестве города. Как я могу написать запрос, который будет принимать во внимание все эти входные данные?

Обратите внимание: в search.php я включил запрос для обработки ключевого слова, представленного в user_search_input вход.

Index.php (HTML):

<form class="user_search_form" action="" method="post">
<input type="text" class="user_search_input" placeholder="search" />

<select class="industry_types" name="industry_types" id="industry_types">
<option value="">Select an industry</option>
<option value="Accommodation">Accommodation</option>
<option value="Food_Drink">Food & Drink</option>
<option value="Entertainment">Entertainment</option>
<option value="Retail">Retail</option>
<option value="Telecommunications">Telecommunications</option>
<option value="Transportation">Transportation</option>
</select><select class="Accommodation sub_industry_type" name="Accommodation" id="sub_industry_type">
<option value="0">Select Accommodation subcategory</option>
<option value="Bed and Breakfasts">Bed and Breakfasts</option>
<option value="Hotel">Hotels</option>
<option value="Hotel">Hostels</option>
<option value="Resturant">Resorts</option>
<option value="Serviced apartments">Serviced apartments</option>
</select>

<select class="Food_Drink sub_industry_type" name="Food_Drink" id="sub_industry_type">
<option value="0">Select Cuisine subcategory</option>
<option value="Resturant">Restaurants</option>
<option value="Cafes">Cafes</option>
<option value="Bars and Pubs">Bars and Pubs</option>
<option value="Tourist Agency">Night clubs</option>
</select><select class="Entertainment sub_industry_type" name="Entertainment" id="sub_industry_type">
<option value="0">Select Entertainment subcategory</option>
<option value="Performing Arts">Performing Arts</option>
<option value="Cinemas">Cinemas</option>
<option value="Resorts and Casinos">Resorts and Casinos</option>
<option value="Amusement Parks and Attractions">Amusement Parks</option>
<option value="Museums">Museums</option>
<option value="Outdoor activities">Outdoor activities</option>
<option value="Media and Entertainment Other">Entertainment Other</option>
</select>

<select class="Retail sub_industry_type" name="Retail" id="sub_industry_type">
<option value="0">Select Retail subcategory</option>
<option value="Beer, Wine and Liquor Stores">Beer, Wine and Liquor Shops</option>
<option value="Clothing and Shoe Stores">Clothing and Shoe Shops</option>
<option value="Electronic Shops">Electronic Shops </option>
<option value="Jewelry, Luggage, and Accessories">Jewelry, Luggage, and Accessories</option>
<option value="Sporting Goods, Hobby, Books and Music Stores">Sporting Goods, Hobby, Books and Music Stores</option>
<option value="Internet Businesses">Internet Businesses</option>
<option value="Retail Others">Retail Others</option>
</select>

<select class="Telecommunications sub_industry_type" name="Telecommunications" id="sub_industry_type">
<option value="0">Select Telecommunications subcategory</option>
<option value="Telephone Service Providers and Carriers">Telephone Service Providers</option>
<option value="Telecommunications Other">Telecommunications Other</option>
</select>

<select class="Transportation sub_industry_type" name="Transportation" id="sub_industry_type">
<option value="0">Select Transportation subcategory</option>
<option value="Car Rental">Car Rentals</option>
<option value="Taxi, Buses and Transit Systems">Taxi Services</option>
<option value="Travel Agents">Travel Agents</option>
</select>

<select name="select_city" class="select_city">
<option value="">City</option>
<option value="">London</option>
<option value="">New York</option>
<option value="">Paris</option>
</select>

<input type="submit" value="search"  class="search_btn" />
</form>

Index.php (AJAX):

$('.user_search_form').submit(function(e){
e.preventDefault();

var $search_input = $('.user_search_input').val();
var $industry_general = $('.industry_types').val();
var $industry_spesific = $('.sub_industry_type').val();
var $selected_city = $('.select_city').val();var url = "search.php";

$.ajax({    //create an ajax request to load_page.php
type: "POST",
url: url,
data:{search_input: $search_input, industry_general: $industry_general, industry_spesific: $industry_spesific, selected_city: $selected_city},
dataType: "html",   //expect html to be returned
success: function(date){
$('#search_results').html(date);}
})
});

search.php (выпуск):

<?php include 'includes/db_connect.php';

$search_input = $_POST['search_input'];
$industry_general = $_POST['industry_general'];
$industry_spesific = $_POST['industry_spesific'];
$selected_city = $_POST['selected_city'];

if(!empty($search_input)){

$sql = $dbh->prepare("SELECT * FROM table WHERE company_name LIKE :company_name OR company_keywords LIKE :company_keywords OR company_city LIKE :company_city OR company_email LIKE :company_email");

$sql->bindValue(':company_name', '%' . $search_input . '%', PDO::PARAM_STR);
$sql->bindValue(':company_keywords', '%' . $search_input . '%', PDO::PARAM_STR);
$sql->bindValue(':company_city', '%' . $search_input . '%', PDO::PARAM_STR);
$sql->bindValue(':company_email', '%' . $search_input . '%', PDO::PARAM_STR);if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}

}

elseif(!empty($industry_general)){

// if industry_general is not empty
}

elseif(!empty($industry_spesific)){

// if industry_spesific is not empty
}

elseif(!empty($selected_city)){

// if selected_city is not empty
}?>

-1

Решение

Как насчет этого фрагмента?

// columns that should contain either of the search_input words
$searchTermCols = [
'company_name',
'company_keywords',
'company_city',
'company_email'
];

$data = $_POST; // copy $_POST array to a variable so you can safely alter keys/values
$searchInput = $data['search_input']; // tmp store search_input so you can unset it for the rest of the code
unset($data['search_input']); // unset unnecessary keys (if you have any). like in your case you want to do different stuff with that key=>value
//unset($data['some_other_unwanted_input_doing_other_stuff']); // or another one
$searchInputParts = preg_split('/\s+/', $searchInput); // explode by whitespace
$searchInput = implode('|',$searchInputParts); // for REGEXP search in mysql to search each word occurrence in a column

foreach($data as $key=>$val) { // loop through copied array
$sqlParts1[] = $key.' LIKE :'.$key;
}
foreach($searchTermCols as $key) { // loop through columns that should contain either of the words
$sqlParts2[] = $key.' REGEXP :'.$key;
}

$andParts = implode(' AND ',$sqlParts1); // these options must exists in the columns
$orParts = implode(' OR ',$sqlParts2); // these options must exists in the columns

$qryStr = "SELECT * FROM table WHERE ";
$qryStr .= "($andParts) AND ($orParts)"; // concatenate both similar but different queries and append to query
$sql = $dbh->prepare($qryStr);

foreach($data as $key=>$val) {
$sql->bindValue(':'.$key, '%' . trim($val) . '%', PDO::PARAM_STR);
}
foreach($searchTermCols as $key) {
$sql->bindValue(':'.$key, $searchInput, PDO::PARAM_STR);
}

if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}

ОБНОВИТЬ

Вы можете скопировать $ _POST в другую переменную и сбросить ключи, которые вам не нужны в запросе. Посмотрите первые две добавленные строки и переменную $ data, установленную для циклического прохождения.

ОБНОВЛЕНИЕ 2 Важно ..

Атрибуты имени вашей формы ввода должны соответствовать столбцам таблицы базы данных. Так, например:

<input type="text" name="company_name" id="company_name">

Если имя столбца вашей таблицы базы данных — «company_name».

ОБНОВЛЕНИЕ 3

Для развлечения я добавил требования, изложенные в вашем комментарии. У меня нет вашей структуры таблицы, поэтому я не проверял ее …

2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector