Base.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. class Base extends Controller
  5. {
  6. public function _initialize(){
  7. $data = db('column')->select();
  8. // $tags = db('tags')->select();
  9. $good =db('good')->select();
  10. //标签栏一个工具对应多个标签处理-----------------------------------
  11. $tags_id = input('id');
  12. // 找到所有标签,对其进行格式化处理
  13. $tags_data = db('tags')->select();
  14. $tags = [];
  15. foreach ($tags_data as $value) {
  16. $tags[$value['id']] = ['tagname'=>$value['tagname'], 'children'=>[]];
  17. }
  18. // 找到所有商品,判断商品是否在标签内
  19. $ta = db('good')->field('id, title, tags_id')->select();
  20. foreach ($ta as $val) {
  21. $tag_ids = explode(',', $val['tags_id']);
  22. if ($tag_ids) {
  23. foreach($tag_ids as $tid) {
  24. if($tags[$tid]) {
  25. $tags[$tid]['children'][] = $val;
  26. }
  27. }
  28. }
  29. }
  30. //end---------------------------------------------------------
  31. $this->assign([
  32. 'data'=>$data,
  33. 'tags'=>$tags,
  34. 'good'=>$good,
  35. ]);
  36. return view();
  37. }
  38. public function praise(){//点赞
  39. $id = input('id');//获取当前点赞id
  40. $res = db('good')->where(array('id'=>$id))->field('likes')->find();
  41. if ($res) {
  42. $res1 = db('good')->where('id',$id)->setInc('likes');//setInc:对指定字段进行加操作,setDec:对指定字段进行减操作
  43. if($res1){
  44. return 1;//点赞成功
  45. }else{
  46. return 2;//失败
  47. }
  48. }
  49. }
  50. }