Home php教程 PHP开发 Yii framework search paging module writing method

Yii framework search paging module writing method

Dec 23, 2016 pm 05:10 PM

Controller layer

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

<?PHP

namespace frontend\controllers;

header(&#39;content-type:text/html;charset=utf-8&#39;);

use Yii;

use yii\base\InvalidParamException;

use yii\web\BadRequestHttpException;

use yii\web\Controller;

use yii\filters\VerbFilter;

use yii\filters\AccessControl;

use common\models\LoginForm;

use frontend\models\PasswordResetRequestForm;

use frontend\models\ResetPasswordForm;

use frontend\models\SignupForm;

use frontend\models\ContactForm;

use frontend\models\Goods; //加载jidian 表的model

use yii\data\Pagination; //yii框架中使用分页

use frontend\web\myclass\QRcode;//加载生成二维码类

/**

 * Site controller

 */

class GoodsController extends Controller

{

  public $enableCsrfValidation = false;

  //商品展示列表

  public function actionGoodslist()

  {

  //接收过来搜索的条件

  $w=yii::$app->request->get(&#39;goods_name&#39;);

  //分页

  $test=new Goods();  //实例化model模型

  $arr=$test->find()->where([&#39;like&#39;,&#39;goods_name&#39;,"$w"]); //加上搜索的条件where

  $pages = new Pagination([

    &#39;totalCount&#39; => $arr->count(),

    &#39;pageSize&#39;  => 4 //每页显示条数

  ]);

  $models = $arr->offset($pages->offset)

    ->limit($pages->limit)

    ->all();

  return $this->render(&#39;goodslist&#39;, [ //前台的页面

    &#39;data&#39; => $models,

    &#39;pages&#39; => $pages,

    &#39;where&#39; =>$w   //把搜索的条件显示到前面

  ]);

      

  }

}

Copy after login

View layer

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

<?php

use yii\widgets\ActiveForm;

use yii\helpers\Url;

use yii\helpers\Html;

use yii\widgets\LinkPager;

?>

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

  <title>商品的展示列表</title>

</head>

<body>

<?php

$form=ActiveForm::begin([

  &#39;action&#39;=>Url::toRoute([&#39;goods/goodslist&#39;]),

  &#39;method&#39;=>&#39;get&#39;,

]);

echo &#39;搜索&#39;," ",Html::input(&#39;text&#39;,&#39;goods_name&#39;,$where);

// echo &#39;年龄&#39;," ",Html::input(&#39;text&#39;,&#39;age&#39;,$where[&#39;age&#39;]);

echo Html::submitButton(&#39;搜索&#39;);

ActiveForm::end();

?>

  <table>

  <?php foreach ($data as $key => $val): ?>

    <tr>

      <td>商品名称是:<?= $val[&#39;goods_name&#39;]?></td>

    </tr>

  <?php endforeach ?>

  </table>

</body>

</html>

<?php

// use yii\widgets\LinkPager;

echo LinkPager::widget([

  &#39;pagination&#39; => $pages,

  &#39;nextPageLabel&#39; => &#39;下一页&#39;,

  &#39;prevPageLabel&#39; => &#39;上一页&#39;,

]);

?>

Copy after login

Model layer

1

2

3

4

5

6

<?php

namespace frontend\models;

use Yii;

class Goods extends \yii\db\ActiveRecord

{

}

Copy after login

The above is the Yii framework search paging module writing method introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message , the editor will reply to everyone in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to Yii framework search paging module writing methods, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24