Home  >  Article  >  Backend Development  >  How to achieve search effect in php

How to achieve search effect in php

藏色散人
藏色散人Original
2021-12-13 10:51:584069browse

php method to achieve search effect: 1. Initialize the query conditions; 2. Call the query method; 3. Calculate the number of data displayed on the page; 4. In the "Search" menu of the setting, call "protected function _search" (){...}" search method.

How to achieve search effect in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to achieve search effect in php?

PHP search query function implementation:

I encountered a problem today: when doing the "search" function, the query cannot be performed after entering the query conditions.

What I am doing is to display the content in the data table package on the homepage, but there is a condition. The content displayed on the homepage must also be: field status=0, and data with printing=0 can be displayed in the homepage list. come out.

There is a "search" function on the page. After entering the conditions, the query will be carried out based on the conditions.

For general search, just give one in the homepage display list method index():

$map=array();//初始化查询条件
$map=$this->_search();//调用查询方法
$total = $this->Model->where ($map)->count(); //这个主要是用来计算页面显示数据条数的
if ($total == 0) {
$_list = '';
} else {
$_list = $this->Model->where ($map)->limit( $post_data ['first'] . ',' . $post_data ['rows'] )->select();
}

Then, just write a _search():

such as:

protected function _search(){
$map = array ();
$post_data = I ( 'post.' );
if ($post_data ['packageid'] != '') {
$map ['packageid'] = array (
'like',
'%' . $post_data ['packageid'] . '%' 
);
}
return $map;
}

Finally, call this search method in the "Search" menu of settings.

However, when I do this, while searching, I also need to make sure to search in the data with field status=0 and printing=0.

I have been thinking about where to add this restriction. After various attempts and inquiries, I found out. Just add the restriction conditions directly to the SQL statement (as shown in red below). (When I tried it myself, I kept adding conditions in the blue area below, and failed again and again!)

$map=array();
$map=$this->_search();
$total = $this->Model->where ($map)->where(array('status' =>0,'print_status'=>0))->count();
if ($total == 0) {
$_list = '';
} else {
$_list = $this->Model->where ($map)->where(array('status' =>0,'print_status'=>0))->limit( $post_data ['first'] . ',' . $post_data ['rows'] )->select();
}

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to achieve search effect in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn