EC-CUBE レビュー一覧をページング対応

■レビュー一覧ページをページング対応

前回作成したレビュー一覧ページだと登録されている全データが表示されるため
データが増えるとページが重たくなったりページが縦長になるのでページング処理を
導入してカスタマイズする

手順:

レビュー情報取得処理をページング対応に実装する
テンプレートにページャーを表示させる処理を実装する

■レビュー情報取得処理をページング対応に実装する

define('REVIEW_DISP_NUMBER', 10);  // レビューページの表示件数
表示件数のデフォルト値をdefineにて定義
    /**
     * Page のアクション.
     *
     * @return void
     */
    function action()
    {
      // レビュー情報を取得
      $this->arrReview = $this->lfGetReviewData();
    }


  /**
   * 商品毎のレビュー情報を取得する
   *
   * @return レビュー情報
   */
  function lfGetReviewData()
  {
      // パラメーター管理クラス
      $objFormParam = new SC_FormParam_Ex();
      // パラメーター情報の初期化
      $this->lfInitParam($objFormParam);
      // 値の設定
      $objFormParam->setParam($_REQUEST);
      // 入力値の変換
      $objFormParam->convParam();
      // 値の取得
      $this->arrForm = $objFormParam->getHashArray();
      
      // 現在のページを取得
      $this->tpl_pageno   = $this->arrForm['pageno'];
      // 有効レビュー件数を取得
      $objQuery   =& SC_Query_Ex::getSingletonInstance();
      $this->tpl_linemax = $objQuery->count("dtb_review", "del_flg = 0 AND status = 1 ORDER BY create_date DESC");
      // 表示件数
      $this->disp_number  = $this->lfGetDisplayNum($this->arrForm['disp_number']);
      $urlParam           = "pageno=#page#";
      
      // モバイルの場合に検索条件をURLの引数に追加
      if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_MOBILE) {
          $urlParam .= "&mode={$this->mode}";
      }
      
      $objNavi = new SC_PageNavi_Ex(
        $this->tpl_pageno,
        $this->tpl_linemax,
        $this->disp_number,
        'return;',
        NAVI_PMAX,
        $urlParam,
        SC_Display_Ex::detectDevice() !== DEVICE_TYPE_MOBILE
      );
      
      // ページナビをアサイン
      $strnavi = $objNavi->strnavi;
      $this->tpl_strnavi = empty($strnavi)?' ':$strnavi;
      
      // 商品ごとのレビュー情報を取得する
      $objQuery->setLimitOffset($this->disp_number, $objNavi->start_row);
      $col = "t1.create_date, t1.reviewer_url, t1.reviewer_name, t1.recommend_level, t1.title, t1.comment, t2.product_id, t2.name, t2.main_list_image";
      $from = "dtb_review as t1 left join dtb_products as t2 using (product_id)";
      $where = "t1.del_flg = 0 AND t1.status = 1 ORDER BY t1.create_date DESC";
      $arr_review = $objQuery->select($col, $from, $where, $arrval);
      return $arr_review;
  }
  
    /**
     * パラメーター情報の初期化
     *
     * @param  array $objFormParam フォームパラメータークラス
     * @return void
     */
    public function lfInitParam(&$objFormParam)
    {
        // 抽出条件
        $objFormParam->addParam('ページ番号', 'pageno', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
    }


    /**
     * パラメーターの読み込み
     *
     * @return void
     */
    public function lfGetDisplayNum($display_number)
    {
        // 表示件数
        return (SC_Utils_Ex::sfIsInt($display_number))
            ? $display_number
            : REVIEW_DISP_NUMBER;
    }
今後の拡張も考えてlfInitParam関数でフォームパラメーター管理する
表示件数の制御や表示順の変更等

■テンプレートにページャーを表示させる処理を実装する

<div class="navi"><!--{$tpl_strnavi}--></div>
<h2>レビュー一覧</h2>
<!--{if count($arrReview) > 0}-->
  <ul>
    <!--{section name=cnt loop=$arrReview}-->
      <li>
        <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrReview[cnt].product_id|u}-->">
          <img src="<!--{$smarty.const.ROOT_URLPATH}-->resize_image.php?image=<!--{$arrReview[cnt].main_list_image|sfNoImageMainList|h}-->&amp;width=120&amp;height=120" alt="<!--{$arrReview[cnt].name|h}-->" />
        </a>
        <a href="<!--{$smarty.const.HTTP_URL}-->products/detail<!--{$arrReview[cnt].product_id|u}-->.php"><!--{$arrReview[cnt].name|h}--></a>
        <p class="voicetitle"><!--{$arrReview[cnt].title|h}--></p>
        <p class="voicedate"><!--{$arrReview[cnt].create_date|sfDispDBDate:false}--> 投稿者:<!--{if $arrReview[cnt].reviewer_url}--><a href="<!--{$arrReview[cnt].reviewer_url}-->" target="_blank"><!--{$arrReview[cnt].reviewer_name|h}--></a><!--{else}--><!--{$arrReview[cnt].reviewer_name|h}--><!--{/if}--> おすすめレベル:<span class="recommend_level"><!--{assign var=level value=$arrReview[cnt].recommend_level}--><!--{$arrRECOMMEND[$level]|h}--></span></p>
        <p class="voicecomment"><!--{$arrReview[cnt].comment|h|nl2br}--></p>
      </li>
      <hr />
    <!--{/section}-->
  </ul>
<!--{/if}-->
<div class="navi"><!--{$tpl_strnavi}--></div>

■レビューページ(1ページ目)
ec_cube_061

■レビューページ(2ページ目)
ec_cube_062

Comments are closed.