yii原始sql语句分页 [ 2.0 版本 ]
--控制器
$connection = Yii::$app->db;
$sql = "SELECT user_id,SUM(consumption) as consumption,SUM(achievement)
as achievement,SUM(total_bonus) as total_bonus,SUM(fudao_prize)
as fudao_prize,SUM(a_bonus) as a_bonus,SUM(income) as income
FROM ol_bonus GROUP BY user_id";
$command = $connection->createCommand($sql)->queryAll();
$pages = new Pagination(['totalCount' => count($command)]);
$list = $connection->createCommand($sql." limit ".$pages->limit." offset ".$pages->offset."")->queryAll();
$dataprovider = new ArrayDataProvider([
'allModels' => $list,
]);
return $this->render('check',[
'bonus' => $dataprovider,
'pages' => $pages,
]);
--视图
<?php if (isset($bonus)):?>
<?php foreach ($bonus as $key=>$ad): ?>
<tr>
<td><?=$ad['user_id'] ?></td>
<td><?=$ad['consumption'] ?></td>
<td><?=$ad['achievement'] ?></td>
<td><?=$ad['total_bonus'] ?></td>
<td><?=$ad['fudao_prize'] ?></td>
<td><?=$ad['a_bonus'] ?></td>
<td><?=$ad['income'] ?></td>
<?php endforeach; ?>
<?php endif; ?>
<?php
echo LinkPager::widget([
'pagination' => $pages,
]);
?>
共 3 个回答
-
$sql = "SELECT user_id,SUM(consumption) as consumption,SUM(achievement) as achievement,SUM(total_bonus) as total_bonus,SUM(fudao_prize) as fudao_prize,SUM(a_bonus) as a_bonus,SUM(income) as income FROM ol_bonus GROUP BY user_id"; $command = $connection->createCommand($sql)->queryAll(); ...count($command)...
这里把
$sql
改成$sql = "select count(distinct user_id) from ol_bonus"
更好吧共 2 条回复 -
wl515230506 回答于 2017-01-05 17:09 举报
要
**$dataprovider = new ArrayDataProvider(['allModels' => $list, ]); **
这个没用
俗已入骨
注册时间:2016-05-22
最后登录:2017-03-31
在线时长:2小时25分
最后登录:2017-03-31
在线时长:2小时25分
- 粉丝1
- 金钱20
- 威望0
- 积分40