Home>Article>Backend Development> TP5 simply implements a multi-level product filtering function similar to Taobao (code example)
The content of this article is about TP5’s simple implementation of a Taobao-like multi-level product filtering function (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
When I was working on a project a few days ago, I came across a need for a multi-level product classification filtering function. I have never done this before, and after checking a lot of information, I finally made it happen. Although the implementation is not elegant enough, at least it is effective, and I am still very happy. A rookie has a heart to become a master. I would like to share with you children’s shoes here, and you are welcome to give me some guidance.
Let’s talk about the principle first: PHP implements multi-level filtering mainly by using a link to get parameters, and the parameters contained in each tag are transmitted to the background controller through a link. After receiving the parameters, the controller then assigns the value back to Template, and query the corresponding data according to the parameters and output it to the front desk. It is not difficult to implement. When I talk about it, I feel like I have no idea at all at the beginning. Haha...
The following is an example of the front-end code:
类型: 0, 'mode' => $a, 'price'=>$c])}">全部 1, 'mode' => $a, 'price'=>$c])}">官方新闻 2, 'mode' => $a, 'price'=>$c])}">移动应用 3, 'mode' => $a, 'price'=>$c])}">微信公众号 4, 'mode' => $a, 'price'=>$c])}">Android开发 模式: $b, 'mode' => '0', 'price'=>$c])}">全部 $b, 'mode' => '1', 'price'=>$c])}">模式1 $b, 'mode' => '2', 'price'=>$c])}">模式2 $b, 'mode' => '3', 'price'=>$c])}">模式3 $b, 'mode' => '4', 'price'=>$c])}">模式4 $b, 'mode' => '5', 'price'=>$c])}">模式5 $b, 'mode' => '6', 'price'=>$c])}">模式6 预算价格: $b, 'mode' => $a, 'price'=>'0'])}">全部 $b, 'mode' => $a, 'price'=>'1'])}">600以下 $b, 'mode' => $a, 'price'=>'600'])}">600-1000 $b, 'mode' => $a, 'price'=>'1000'])}">1000-5000 $b, 'mode' => $a, 'price'=>'5000'])}">5000以上
Everyone can definitely understand this code, I will explain it a little more, for example, when When the user clicks on everything in the category for the first time, the category field of 0 will be passed to the background. The background receives the judgmentand assigns the value back to the front desk. If the category field value is 0, all category data is queried. If the user clicks everything in the mode for the second time, then because the category value is assigned from the background, it will be 0 and the mode will be 0. If you click all in the price for the third time, all three label values will be 0. At this time, the background determines the conditions. If all are 0, then all are queried and the template is traversed and displayed. Draw inferences from one example, and the same goes for other labels.In actual operation, it will definitely not be so stupid to write the values one by one in the tag. Just use volist to traverse and get the value. Everyone understands it.
Because it is multi-level filtering, there must be more than one where method condition, so the values of the where method must be spliced. I am a TPer. I will use the TP5 method as an example:
$b = input('category') ;//Received category id
$a = input('mode'); //Received mode id
$c = input('price'); //Received price range value
$ where = ['category'=>$category,'mode'=>$mode,'price'=>$price];//Splicing where condition
$data = model('table name')- >where($where)->select(); //Query data
return $this->fetch('',[
'data'=>$data,
'c '=>$c,
'b'=>$b,
'a'=>$a
]); //Template assignment
Of course, the actual situation It is necessary to set conditions to judge the data of three values, and set where conditions according to the values. For example:
if ($a == 0 && $b == 0 && $c == 0) {//条件全部为空,即显示所有 $where = ''; } elseif ($a == 0 && $b != 0 && $c != 0) {//模式为全部范围,分类和价格单独指定。 $where = ['b'=>$b,'c'=>$c]; }
Because my code is relatively redundant, I won’t show my shame...
The final effect is like this:
The style in the picture can be judged in the tag. If the parameter value received by the page is the same as the value in the current tag, the style will be highlighted for the tag. So far, that's it. If any students have better ideas, please feel free to enlighten me.
The above is the detailed content of TP5 simply implements a multi-level product filtering function similar to Taobao (code example). For more information, please follow other related articles on the PHP Chinese website!