溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用thinkPHP怎么實(shí)現(xiàn)一個多表查詢和分頁功能

發(fā)布時間:2021-04-15 16:05:53 來源:億速云 閱讀:135 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹使用thinkPHP怎么實(shí)現(xiàn)一個多表查詢和分頁功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

// 異步請求試卷夾下某一個試卷夾的列表
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);
    // 搜索標(biāo)簽
    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';
  // 總數(shù)
  $count = M()->table("$collect collect, $resource resource")->where($map)->count();
  // 總頁數(shù)
  $regNum = ceil($count/6);
  // 驗(yàn)證當(dāng)前請求頁碼是否大于總頁數(shù)
  $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();
  // 獲取試卷的標(biāo)簽
  $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');
  // 把標(biāo)簽和試卷對應(yīng)
  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);
}

關(guān)于使用thinkPHP怎么實(shí)現(xiàn)一個多表查詢和分頁功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI