init
This commit is contained in:
87
app/index/controller/Search.php
Executable file
87
app/index/controller/Search.php
Executable file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use think\Loader;
|
||||
use think\Config;
|
||||
use think\Db;
|
||||
|
||||
class Search extends BaseController {
|
||||
|
||||
public function index() {
|
||||
$skeyword = $this->request->get('skeyword');
|
||||
|
||||
$search = [];
|
||||
$name_word = '';
|
||||
$brand_word = '';
|
||||
if ($skeyword != '') {
|
||||
$skeyword = trim($skeyword);
|
||||
$search['skeyword'] = $skeyword;
|
||||
|
||||
$pos = strpos($skeyword, '(');
|
||||
if ($pos === false) {
|
||||
$name_word = $brand_word = $skeyword;
|
||||
} else {
|
||||
$name_word = substr($skeyword, 0, $pos);
|
||||
$brand_word = substr($skeyword, $pos+1, -1);
|
||||
}
|
||||
|
||||
$arg_where = [
|
||||
'a.name|a.brand_id|a.keyword|b.sku' => ['like', '%' . $name_word . '%']
|
||||
];
|
||||
|
||||
Config::set('paginate.query', ['skeyword' => $skeyword]);
|
||||
}
|
||||
$arg_where['a.stat'] = 0;
|
||||
// $arg_where['b.stat'] = 0;
|
||||
$arg_where['a.country_code'] = $this->country_code;
|
||||
$arg_where['a.is_show'] = 0;
|
||||
|
||||
$field = ['a.id', 'a.name', 'a.description', 'a.list_bk_img' => 'picture', 'a.createtime','a.brand_id', 'b.sku', "'productdetail'" => 'link'];
|
||||
$dataObject = model('product')->alias('a')->join('product_sku b', 'a.brand_id=b.brand_id', 'LEFT')->field($field)->where($arg_where)->group('a.id')->paginate(12);
|
||||
// echo model('product')->getLastSql();die;
|
||||
$list = $dataObject->items();
|
||||
// tiaoshi($list);die;
|
||||
$arr_name = mb_str_split(strtolower($name_word));
|
||||
$arr_brand = mb_str_split(strtolower($brand_word));
|
||||
|
||||
if (!empty($skeyword))
|
||||
{
|
||||
foreach ($list as $key => $value)
|
||||
{
|
||||
$weight = 0;
|
||||
$curr_arr_name = mb_str_split(strtolower($value['name']));
|
||||
$curr_arr_brand = mb_str_split(strtolower($value['brand_id']));
|
||||
$weight += count(array_intersect($arr_name, $curr_arr_name));
|
||||
$weight += count(array_intersect($arr_brand, $curr_arr_brand));
|
||||
$list[$key] = $value;
|
||||
$list[$key]['weight'] = $weight;
|
||||
}
|
||||
|
||||
$len = count($list);
|
||||
for ($i=0; $i<$len-1; $i++)
|
||||
{
|
||||
for ($j=0; $j<$len-1-$i; $j++)
|
||||
{
|
||||
if ($list[$j]['weight'] < $list[$j+1]['weight'])
|
||||
{
|
||||
$tmp = $list[$j];
|
||||
$list[$j] = $list[$j+1];
|
||||
$list[$j+1] = $tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// tiaoshi($list);die;
|
||||
|
||||
$value = [
|
||||
'list' => $list,
|
||||
'page' => $dataObject->render(),
|
||||
];
|
||||
$value['search'] = $search;
|
||||
$value['name_word'] = $name_word;
|
||||
$value['brand_word'] = $brand_word;
|
||||
$this->assign($value);
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user