Rumah > pembangunan bahagian belakang > tutorial php > thinkPHP多表查询方法示例

thinkPHP多表查询方法示例

小云云
Lepaskan: 2023-03-20 07:58:02
asal
1325 orang telah melayarinya

本文主要和大家介绍了thinkPHP多表查询及分页功能实现方法,结合具体实例形式分析了thinkPHP多表查询以及查询结果的分页显示相关实现技巧,需要的朋友可以参考下,希望能帮助到大家。

项目业务逻辑为:教师上传试卷,设置答题卡,发布答题卡给相关的班级或群组,只有试卷关联的答题卡发布后,该试卷才能在系统试卷中搜索到,同时其他的老师也可以收藏。在前端的收藏模块中,有个业务是给个input框以提供搜索功能给用户,但是在事先设计的搜索表中,只有一处试卷ID是和试卷表关联的,如果用户搜索试卷题目那岂不要两表查询了,一开始我想到的方法是在收藏表中多加个字段,也就是把试卷题目的字段添加到收藏表中,业务完成。今天在处理题库分享的逻辑时,又发现了这个问题,看了下同事设计的分享表只有一个题库ID,于是我便把同事叫过来“纠正”,但事先我还是想听听同事的设计思路,同事说量表查询啊,我勒个去,看来我一张表查询使用习惯了,没有想到此种情况,被鄙视了,于是正视了自己的错误,当然了前提是说了下自己的思路,现在想来不怎么对,下面便给给出相关的tp代码。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

// 异步请求试卷夹下某一个试卷夹的列表

public function ajaxLists() {

  extract($_GET);

  $page = intval($_GET['p']);

  $prefix = C('DB_PREFIX');

  $collect = $prefix . 'collect';

  $resource = $prefix . 'resource';

  if ($keyword) {

    $arr = preg_split('/ /', $keyword);

    // 搜索标签

    foreach ($arr as $value) {

      $id = A('Home/Papers')->trunWordToId(array($value));

      if ($id) {

        $where['resource.rta_id'][] = array('LIKE', '%,' . $id . ',%');

      }

      $where['resource.re_title'][] = array('LIKE', '%' . $value . '%');

    }

    if ($where['resource.rta_id']) {

      $where['resource.rta_id'][] = 'AND';

    }

    if ($where['resource.re_title']) {

      $where['resource.re_title'][] = 'OR';

    }

    if ($where['resource.re_title'] && $where['resource.rta_id']) {

      $where['_logic'] = 'OR';

    }

  }

  if ($where) {

    $map['_complex'] = $where;

  }

  $map['collect.a_id'] = $this->authInfo['a_id'];

  $map['_string'] = 'collect.col_object_id = resource.re_id';

  // 总数

  $count = M()->table("$collect collect, $resource resource")->where($map)->count();

  // 总页数

  $regNum = ceil($count/6);

  // 验证当前请求页码是否大于总页数

  $page = $page > $regNum ? $regNum : $page;

  // 引入ajax分页库

  import("@.ORG.Util.AjaxPage");

  $Page = new AjaxPage($count, 6);

  $list['page'] = trim($Page->show());

  $list['list'] = M()->table("$collect collect, $resource resource")->where($map)->order('col_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->field('collect.col_id,collect.col_object_id,resource.re_id,resource.re_title,resource.re_created,resource.re_collect_count,resource.re_score_count,resource.re_score_num,resource.rta_id')->select();

  // 获取试卷的标签

  $wheree['rta_id'] = array('IN', trim(str_replace(',,', ',', implode('', getValueByField($list['list'], 'rta_id'))), ','));

  $tag = setArrayByField(M('ResourceTag')->where($wheree)->field('rta_id,rta_title')->select(), 'rta_id');

  // 把标签和试卷对应

  foreach ($list['list'] as $key => &$value) {

    $str = '';

    foreach ($tag as $k => $v) {

      if (strpos($value['rta_id'], ',' . $k . ',') !== FALSE) {

        $str .= ' | ' . $v['rta_title'];

      }

      $value['rta_title'] = trim($str, ' |');

    }

    if ($keyword) {

      foreach ($arr as $vv) {

        if (strpos($value['re_title'], $vv) !== FALSE) {

          $value[&#39;re_title&#39;] = str_replace($vv, &#39;<font color=\&#39;red\&#39;>&#39; . $vv . &#39;</font>&#39;, $value[&#39;re_title&#39;]);

        }

        if (strpos($value[&#39;rta_title&#39;], $vv) !== FALSE) {

          $value[&#39;rta_title&#39;] = str_replace($vv, &#39;<font color=\&#39;red\&#39;>&#39; . $vv . &#39;</font>&#39;, $value[&#39;rta_title&#39;]);

        }

      }

    }

    $value[&#39;re_created&#39;] = date(&#39;Y-m-d&#39;, $value[&#39;re_created&#39;]);

  }

  echo json_encode($list);

}

Salin selepas log masuk


相关推荐:

MySQL中的约束与多表查询以及子查询的实例详解

ThinkPHP 多表查询-如果字段A相同,则把字段B相加_PHP教程

thinkphp学习笔记之多表查询,thinkphp学习笔记之_PHP教程

Atas ialah kandungan terperinci thinkPHP多表查询方法示例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Isu terkini
ThinkPHP Mengapa menggunakan komposer?
daripada 1970-01-01 08:00:00
0
0
0
thinkphp memuat naik fail
daripada 1970-01-01 08:00:00
0
0
0
Bagaimanakah Thinkphp memanggil sambungan PHP?
daripada 1970-01-01 08:00:00
0
0
0
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan