Base.php 2.1 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. $good =db('good')->select();
  9. //标签栏一个工具对应多个标签处理-----------------------------------
  10. $tags_id = input('id');
  11. // 找到所有标签,对其进行格式化处理
  12. $tags_data = db('tags')->select();
  13. $tags = [];
  14. foreach ($tags_data as $value) {
  15. $tags[$value['id']] = ['tagname'=>$value['tagname'], 'children'=>[]];
  16. }
  17. // 找到所有商品,判断商品是否在标签内
  18. $ta = db('good')->field('id, title, tags_id')->select();
  19. foreach ($ta as $val) {
  20. $tag_ids = explode(',', $val['tags_id']);
  21. if ($tag_ids) {
  22. foreach($tag_ids as $tid) {
  23. if($tags[$tid]) {
  24. $tags[$tid]['children'][] = $val;
  25. }
  26. }
  27. }
  28. }
  29. //end---------------------------------------------------------
  30. $this->assign([
  31. 'data'=>$data,
  32. 'tags'=>$tags,
  33. 'good'=>$good,
  34. ]);
  35. return view();
  36. }
  37. public function praise(){//点赞
  38. $id = input('id');//获取当前点赞id
  39. $res = db('good')->where(array('id'=>$id))->field('likes')->find();
  40. if ($res) {
  41. $res1 = db('good')->where('id',$id)->setInc('likes');//setInc:对指定字段进行加操作,setDec:对指定字段进行减操作
  42. if($res1){
  43. return 1;//点赞成功
  44. }else{
  45. return 2;//失败
  46. }
  47. }
  48. }
  49. public function alls(){//所有工具
  50. $cid=db('good')
  51. ->alias('a')
  52. ->field('a.*,b.catename')
  53. ->join('column b','a.column_id=b.id')
  54. ->order('sort desc')
  55. ->select();
  56. $this->assign([
  57. 'cid'=>$cid,
  58. ]);
  59. return view('indexs/index');
  60. }
  61. }