Document

Multi-Select Dropdown Filter in PHP with Database Search

Filters are used to narrow down the results based on particular criteria. We have already created a PHP filter for a list page to shortlist the database results based on the selected filter options.

Search filter with a combo field will allow multi-select. So, we can create a search to filter data based on the bunch of options selected by the user.

I have created PHP search filter form with a multi-select box showing the countries list. I have a database table tbl_user to store the user’s details like name, gender, country and more.

On submitting the filter by selecting the countries option, the database results will be fetched and listed based on the selected options. I have imploded the array of selected country names and pass them to the query IN clause to fetch the database results.

If you are checking for country state dependent dropdown check the linked article or you may also check the three levels of dependent dropdown lists.

The following screenshot shows the result of the multi-select dropdown filter using PHP

multi-select-dropdown-filter-in-php-with-database-search-output

Database Script

The following script is used to create the database structure and dump the data required for this example. Before running this example on your local machine, import this script via your database client.

CREATE TABLE `tbl_user` ( `id` int(8) NOT NULL, `Name` varchar(255) NOT NULL, `Gender` varchar(10) NOT NULL, `Country` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `tbl_user` (`id`, `Name`, `Gender`, `Country`) VALUES (1, 'Jack', 'Female', 'Canada'), (2, 'Jane', 'Female', 'Mexico'), (3, 'Emmanuel', 'Male', 'USA'), (4, 'Franck', 'Male', 'USA'), (5, 'Kevin Tomas', 'Male', 'Haiti'), (6, 'Tim Dillon', 'Male', 'Haiti'); ALTER TABLE `tbl_user` ADD PRIMARY KEY (`id`); ALTER TABLE `tbl_user` MODIFY `id` int(8) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; COMMIT; 

HTML Search Filter with Multi-Select dropdown

I have created a HTML form with a multi-select dropdown. In this dropdown, I have listed the distinct country names from the tbl_user database.

The user can select multiple countries from this dropdown list. On submitting the filter options, it will be posted to the PHP to process the MySQL data fetch based on the selected values. After fetching the results from the database, it will be listed below the filter.

runQuery("SELECT DISTINCT Country FROM tbl_user ORDER BY Country ASC"); ?> 

MySQL SELECT Query to Filter Database Results Based on Selected Countries

After submitting the filter options to the PHP, the array of selected countries are iterated in a loop. The loop will create the imploded string from the selected countries array.

Читайте также:  Php date number to string

This string value will be within the IN clause of the SELECT query to fetch data based on the multiple values. Below code shows how to fetch MySQL result based on multiple countries.

   $i ++; > $query = $query . " WHERE country in (" . $selectedOption . ")"; $result = $db_handle->runQuery($query); > if (! empty($result)) < foreach ($result as $key =>$value) < ?>    ?>
Name Gender Country
?>

Источник

Поиск с выбором из списка select

Есть в база данных rubrics, в которой 4 таблицы: monuments, parks, places, theaters (id, title, text, img).
Сделала поиск на сайте, он ищет по одной таблице по слову и выводит статью.
Как сделать, чтобы из списка выбрать и в зависимости от этого делать поиск в нужной таблице (допустим из списка выбрать parks и поиск делать в таблице parks). И как можно сделать поиск по двум полям (не только text, как сейчас у меня).

form method="POST" action="do_search.php"> input type="search" name="search" placeholder="Поиск по рубрикам" required=""> select name="search_s" id="search_s" required=""> option selected disabled>--Выберите рубрику--/option> option name="monuments" value="monuments">Памятники Одессы/option> option name="search_s" id="theaters" value="theaters">Театры Одессы/option> option name="search_s" id="parks" value="parks">Парки Одессы/option> option name="search_s" id="places" value="places">Красивые места/option> /select> button name="search_btn">Найти/button> /form>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 $connection = mysqli_connect("localhost", "root", "", "rubrics"); if (isset($_POST['search_btn'])) { $search = $_POST['search']; $query_rubrics =mysqli_query($connection, "SELECT * FROM `parks` WHERE `text` LIKE'%$search%'"); }else { $query_rubrics = mysqli_query($connection, "SELECT * FROM parks"); } ?>       $query_rubrics; while ($new = mysqli_fetch_assoc($query_rubrics)) { ?>  echo $new['title']?> 

echo $new['text']?>

echo $new['img']?> } ?>

Источник

Webslesson

PHP, MySql, Jquery, AngularJS, Ajax, Codeigniter, Laravel Tutorial

Thursday, 16 April 2020

With this post we have come with unique topic like How can we make Dynamic Dependent Select box with Searching option with PHP script using jQuery and Ajax. We have seen many tutorial on simple dynamic dependent drop download list box with PHP script using ajax. So in simple dynamic select box, for select option we have to scroll select box for select particular option, and after select particular option, then other select box which data has been depend on the value which we have select parent select box. But in Dynamic dependent select box with searching option, user has not scroll select box, but they have to enter value in search box, which they want to select from select box and other dependent select box will be fill with data and from that select box user can also enter search query for select option. So this is main benefits of dynamic dependent select box with search option.

For make dynamic dependent select box with search option, here we have use Bootstrap Select plugin. Bootstrap select plugin is a jquery plugin and by using this plugin we can create select box and menus by using Bootstrap library and this plugin will add extra feature like live search, multiple selection of option, customize select box style, and select or deselect all option in select box. If you have use Bootstrap library in your web development then you can easily use this plugin for make dynamic dependent select box with search option.

After convert simple select box into searchable select box by using Bootstrap select plugin. Now for convert that searchable select box into dynamic select box. So here we will use PHP script for fill dynamic data into this searchable select box. After this we want to make dynamic dependent searchable select box, for this here we have use Ajax. So by using Ajax child select box will be filled with data after selecting value from parent select box. Below you can find complete source code for how to make dynamic dependent select box with search option in PHP by using jQuery and Ajax.

Source Code

Database

 -- -- Table structure for table `tbl_industry` -- CREATE TABLE `tbl_industry` ( `industry_id` int(11) NOT NULL, `industry_name` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -------------------------------------------------------- -- -- Table structure for table `tbl_sub_industry` -- CREATE TABLE `tbl_sub_industry` ( `sub_industry_id` int(11) NOT NULL, `industry_id` int(11) NOT NULL, `sub_industry_name` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Indexes for dumped tables -- -- -- Indexes for table `tbl_industry` -- ALTER TABLE `tbl_industry` ADD PRIMARY KEY (`industry_id`); -- -- Indexes for table `tbl_sub_industry` -- ALTER TABLE `tbl_sub_industry` ADD PRIMARY KEY (`sub_industry_id`), ADD KEY `industry_id` (`industry_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tbl_industry` -- ALTER TABLE `tbl_industry` MODIFY `industry_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=78; -- -- AUTO_INCREMENT for table `tbl_sub_industry` -- ALTER TABLE `tbl_sub_industry` MODIFY `sub_industry_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4414; 

index.php

          

Dynamic Dependent Searchable Select Box with PHP Ajax jQuery


Select Data

load_data.php

 prepare($query); $statement->execute(); $data = $statement->fetchAll(); foreach($data as $row) < $output[] = array( 'id' =>$row["industry_id"], 'name' => $row["industry_name"] ); > echo json_encode($output); > else < $query = " SELECT * FROM tbl_sub_industry WHERE industry_id = '".$_POST["category_id"]."' ORDER BY sub_industry_name ASC "; $statement = $connect->prepare($query); $statement->execute(); $data = $statement->fetchAll(); foreach($data as $row) < $output[] = array( 'id' =>$row["sub_industry_id"], 'name' => $row["sub_industry_name"] ); > echo json_encode($output); > > ?> 

Источник

Select с поиском php

Here I explain how to create Select Box with Search option using PHP & MySQL database. Actually drop down search box is little bit hard to implement in our projects. However it’s possible when we are analyze about the source code. A lot plugin and functionalities are available to built search box with relevant keyword. Totally two methods are available, one is without database retrieve data from client side. And another one get through MySQL table database.

The choice is your wish based on the project requirements. Already I was uploaded one article which is helps to create Live Search functionalities using Ajax and PHP programming language. Once checkout the article after that you get some idea like how built search box in drop down features.

select 2 ajax live search

The above image is the output result when I execute this code on my system. The entire source code is available on end of the section. However read my below steps to setup your code in perfect way then only all the functionalities are executed fine without any issues.

Create Project – Select Box with Search Option

Let’s see the steps for how create drop down select box option value with live search text box. Reason is with help of search button we have to easily search the particular existing value in our database. For that clients are demand this one into developers. Me also later that add this functionality for one of my client who have stock management system project using PHP.

SQL queries are important for communicate into server side. Another one is without SQL, we can do with help of Select2 plugin which is has lot of pre-built functions such as search box, tagging, data sets, infinite scrolling etc (jQuery replacement).

Features

  1. Live Search
  2. Customize CSS
  3. Datalist
  4. Responsive
  5. Not Found Message when data is not existing
  6. Autocomplete disabled

and more some unique benefits are there in select box project. The live demo available on our previous article of PHP Ajax Live Search Code. If you are looking for demo then once visit the link to get more idea about the exercises.

In below I add the output screen image of Select2 and data list drop down select box using PHP & MySQL database. Suppose the example not matched on your existing or new project ? Just contact us and then we are try to sort out your issues. Because we are a team who have 7 plus years experience in software industries.

select 2 no results found

Source Code

Above all methods and code explanations are helps to create your drop down search box using PHP language. WordPress also good when you have no idea about PHP but core language has lot of features when compared to WordPress content management system platform.

Источник

Оцените статью