当前位置:Gxlcms > php框架 > thinkPHP多表查询及分页功能实现方法示例

thinkPHP多表查询及分页功能实现方法示例

时间:2021-07-01 10:21:17 帮助过:14人阅读

本文实例讲述了thinkPHP多表查询及分页功能实现方法。分享给大家供大家参考,具体如下:

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

  1. // 异步请求试卷夹下某一个试卷夹的列表
  2. public function ajaxLists() {
  3. extract($_GET);
  4. $page = intval($_GET['p']);
  5. $prefix = C('DB_PREFIX');
  6. $collect = $prefix . 'collect';
  7. $resource = $prefix . 'resource';
  8. if ($keyword) {
  9. $arr = preg_split('/ /', $keyword);
  10. // 搜索标签
  11. foreach ($arr as $value) {
  12. $id = A('Home/Papers')->trunWordToId(array($value));
  13. if ($id) {
  14. $where['resource.rta_id'][] = array('LIKE', '%,' . $id . ',%');
  15. }
  16. $where['resource.re_title'][] = array('LIKE', '%' . $value . '%');
  17. }
  18. if ($where['resource.rta_id']) {
  19. $where['resource.rta_id'][] = 'AND';
  20. }
  21. if ($where['resource.re_title']) {
  22. $where['resource.re_title'][] = 'OR';
  23. }
  24. if ($where['resource.re_title'] && $where['resource.rta_id']) {
  25. $where['_logic'] = 'OR';
  26. }
  27. }
  28. if ($where) {
  29. $map['_complex'] = $where;
  30. }
  31. $map['collect.a_id'] = $this->authInfo['a_id'];
  32. $map['_string'] = 'collect.col_object_id = resource.re_id';
  33. // 总数
  34. $count = M()->table("$collect collect, $resource resource")->where($map)->count();
  35. // 总页数
  36. $regNum = ceil($count/6);
  37. // 验证当前请求页码是否大于总页数
  38. $page = $page > $regNum ? $regNum : $page;
  39. // 引入ajax分页库
  40. import("@.ORG.Util.AjaxPage");
  41. $Page = new AjaxPage($count, 6);
  42. $list['page'] = trim($Page->show());
  43. $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();
  44. // 获取试卷的标签
  45. $wheree['rta_id'] = array('IN', trim(str_replace(',,', ',', implode('', getValueByField($list['list'], 'rta_id'))), ','));
  46. $tag = setArrayByField(M('ResourceTag')->where($wheree)->field('rta_id,rta_title')->select(), 'rta_id');
  47. // 把标签和试卷对应
  48. foreach ($list['list'] as $key => &$value) {
  49. $str = '';
  50. foreach ($tag as $k => $v) {
  51. if (strpos($value['rta_id'], ',' . $k . ',') !== FALSE) {
  52. $str .= ' | ' . $v['rta_title'];
  53. }
  54. $value['rta_title'] = trim($str, ' |');
  55. }
  56. if ($keyword) {
  57. foreach ($arr as $vv) {
  58. if (strpos($value['re_title'], $vv) !== FALSE) {
  59. $value['re_title'] = str_replace($vv, '<font color=\'red\'>' . $vv . '</font>', $value['re_title']);
  60. }
  61. if (strpos($value['rta_title'], $vv) !== FALSE) {
  62. $value['rta_title'] = str_replace($vv, '<font color=\'red\'>' . $vv . '</font>', $value['rta_title']);
  63. }
  64. }
  65. }
  66. $value['re_created'] = date('Y-m-d', $value['re_created']);
  67. }
  68. echo json_encode($list);
  69. }

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

人气教程排行