thinkPHP多表查询及分页功能实现方法示例
author:一佰互联 2019-04-25   click:163

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

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

// 异步请求试卷夹下某一个试卷夹的列表
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["re_title"] = str_replace($vv, "<font color="red">" . $vv . "</font>", $value["re_title"]);
        }
        if (strpos($value["rta_title"], $vv) !== FALSE) {
          $value["rta_title"] = str_replace($vv, "<font color="red">" . $vv . "</font>", $value["rta_title"]);
        }
      }
    }
    $value["re_created"] = date("Y-m-d", $value["re_created"]);
  }
  echo json_encode($list);
}

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

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