Base.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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')->where('tags_id','<>','')->select();//查询出tags_id不为空的所有数据
  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. public function alls(){//所有工具
  51. $cid=db('good')
  52. ->alias('a')
  53. ->field('a.*,b.catename')
  54. ->join('column b','a.column_id=b.id')
  55. ->order('sort desc')
  56. ->select();
  57. $this->assign([
  58. 'cid'=>$cid,
  59. ]);
  60. return view('indexs/index');
  61. }
  62. }