Home php教程 php手册 php search (query) function

php search (query) function

Nov 30, 2016 pm 11:59 PM

I encountered a problem today: when using 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.

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 index() method of displaying the list on the homepage:

$map=array();//Initialize query conditions

$map=$this->_search();//Call the query method

$total = $this->Model->where ($map)->count(); //This is mainly used to calculate the number of data items displayed on the page

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

Then, just write a _search():

For example:

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();
}

I would like to share it with you all.

Novices are learning, please leave comments and advice.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)