PHP Service-side search, functional improvements

Source: Internet
Author: User
Tags php class

PHP, has always been, service-side search, I am writing a bunch of conditions to judge, the search conditions are good, the condition of a lot, the code is a little ugly;

Looked very uncomfortable. I saw an article in the Garden Today (http://www.cnblogs.com/xqin/archive/2013/06/03/3114634.html).

However, he wrote it in net. But it was inspired by this article. Changed my search, though it doesn't look perfect right now. But the code doesn't look so ugly.

$data = Array ();
$data = Fixer::input (' Post ')
->specialchars (' username,nickname,regdate_startdate,regdate_enddate ')
->get ();

$searchSetting = Array (
Array (' field ' = ' username ', ' cp ' = ' like '),
Array (' field ' = ' nickname ', ' cp ' = ' like '),
Array (' field ' = ' regdate ', ' cp ' = ' daterange '),
);

$map = Service (' Search ')->loadsearchsettings ($searchSetting, $data);

The above code is the data submitted by the client first, sorted out first. Includes escape and type conversions. Because the search is over there, the data is not processed. The data is processed on the periphery.

$searchSetting This array is the configuration of each search, the value of the other CP is what we need to write. I've only written equal, like, daterange. Just used these.

After this execution, the code in the controller is very small, it does not look so messy, directly $map as a Where condition

Class Searchservice
{

/** $searchSetting = Array (
Array (' field ' = ' username ', ' cp ' = ' like ', ' variable ' = ' uname '),
);
* The root of the dynamic search configuration passed over, generate the corresponding query conditions, return to
* @param array $searchSettings [description]
* @return [Type] [description]
*/
Public Function loadsearchsettings ($searchSettings = Array (), $data)
{
$map = Array ();
if (Empty ($searchSettings) | |! Is_array ($searchSettings)) return $map;
foreach ($searchSettings as $item)
{
$CP = Isset ($item [' CP ']) && $item [' CP ']? Trim ($item [' CP ']): ';
if (empty ($CP)) continue;
if (method_exists (' CP ', $CP)) {
$s = Call_user_func_array (Array (' CP ', $CP), Array ($item, $data));
if ($s) $map [] = $s;
}
}

$whereData = Array ();
if ($MAP)
{
foreach ($map as $key = $item)
{
if (Is_array ($item))
{
foreach ($item as $field = $value)
{
$whereData [$field] = $value;
}
}
}
}

return $whereData;
}


}

Cp.php class

<?php
/**
* Search for various logic
* Note that no data is processed, data is processed on the periphery, including type conversions.
* @package Framework
*/
Class CP
{
Const FIELD_NAME = ' FIELD ';
Const VARIABLE = ' VARIABLE ';

/**
* Equal
* @param [Type] $fieldConfig [description]
* @param array $data [description]
* @return [Type] [description]
*/
public static function equal ($fieldConfig, $data = Array ())
{
$map = Array ();

if (! isset ($fieldConfig [self::field_name])) return $map;
$fieldName = $fieldConfig [Self::field_name];
$getfield = Self::getfield ($fieldConfig);
if (Isset ($data [$getfield]) && $data [$getfield])
{
$map [$fieldName] = $data [$getfield];
}
return $map;
}

/**
* like query
* @param [Type] $fieldConfig [description]
* @param array $data [description]
* @return [ Type] [description]
*/
public static function like ($fieldConfig, $data = Array ())
{
$map = array ();
IM Port (' ORG. Util.input ');

if (! isset ($fieldConfig [self::field_name])) return $map;
$fieldName = $fieldConfig [Self::field_name];
$getfield = Self::getfield ($fieldConfig);
if (isset ($data [$getfield]) && $data [$getfield])
{
$map [$fieldName] = array (' Like ', input::forsear CH ($data [$getfield]);
}
return $map;
}


/**
* Like query
* @param [Type] $fieldConfig [description]
* @param array $data [description]
* @return [Type] [description]
*/
public static function DateRange ($fieldConfig, $data = Array ())
{
$map = Array ();

if (! isset ($fieldConfig [self::field_name])) return $map;
$fieldName = $fieldConfig [Self::field_name];
$getfield = Self::getfield ($fieldConfig);

if (Isset ($data [$getfield. ' _start ') && $data [$getfield. ' _start '])
{
$map [$getfield] = Array (' EGT ', Strtotime ($data [$getfield. ') _start ']));
}
if (Isset ($data [$getfield. ' _end ') && $data [$getfield. ' _end '])
{
$map [$getfield] = Array (' ELT ', Strtotime ($data [$getfield. ') _end ']));
}

return $map;
}


/**
* Get field Name
* @param [Type] $fieldConfig [description]
* @return [Type] [description]
*/
Private Function GetField ($fieldConfig)
{
$field = Self::field_name;
if (Isset ($fieldConfig [self::variable]))
{
$field = self::variable;
}

return $fieldConfig [$field];
}

}

This puts the former logic into the Cp.php class. What rules to add, directly in this class to add the line.

Although not very advanced, but suitable for the line.

PHP Service-side search, functional improvements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.