欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

zf框架db类的分页示例分享

程序员文章站 2023-11-18 22:16:40
zf框架的分页示例复制代码 代码如下:

zf框架的分页示例

复制代码 代码如下:

<?php
isset($_get['page']) ? $page = $_get['page'] : $page = 1;
//引入loader类(自动加载类)
require_once("zend/loader.php");
//使用loader类引入一个db类
zend_loader::loadclass("zend_db");
//引入zend_db的状态器
zend_loader::loadclass("zend_db_statement_pdo");
//配置数据库连接信息
$config = array('host' => '127.0.0.1' ,
    'username' => 'root' ,
    'password' => '111' ,
    'dbname' => 'test',
    'profiler' => "true"
    );
//告诉zend_db类所操作的数据库和数据库配置信息
$db = zend_db::factory('pdo_mysql' , $config);
//执行编码语句
$db -> query("set names utf8");
//-----------------------------------------------
//使用fetchone()方法得出表的总条数
$total = $db -> fetchone("select count(*) from gongsi");
//定义每页显示条数
$b = 50;
//得出总页数
$a = ceil($total/$b);

//-----接下来为一系列的查询表、取结果集、分页等操作
$select = $db ->select();
$select -> from('sanguo',array('s_sheng as 省份','sum(s_gongzi) as 总工资','min(s_gongzi) as 最低工资','max(s_gongzi) as 最高工资','avg(s_gongzi) as 平均工资'));
// $select -> where('s_gongzi>=3000');
// $select -> where("s_sheng='河北'");
// $select -> order('s_sheng asc');
// $select -> order('s_gongzi desc');
$select -> group('s_sheng');  //分组
//$select -> having('最高工资>10000');  //附加条件
$select -> order('最高工资 desc');  //排序
$select -> limit(0,0); //截取
$select -> limitpage($page, $b); //分页
/*sql语句相当于:
select s_sheng as 省份,sum(s_gongzi) as 最高工资 from sanguo group by s_sheng having 最高工资>10000 order by 最高工资 desc limit 0,10;
*/
$result = $db->fetchall($select);

echo "<table border='1' align='center' width='960' style='text-align:center'>";
echo "<tr><th>省份</th><th>总工资</th><th>最低工资</th><th>最高工资</th><th>平均工资</th></tr>";
foreach ($result as $key => $value)
{
 echo "<tr>";
 foreach ($value as $key2 => $value2)
 {
  echo "<td>" . $value2 . "</td>";
 }
 echo "</tr>";
}
echo "<tr>";
echo "<td colspan='5'>";
echo "<a href=?page=1>首页</a>  ";
if ($page>1)
{
 echo "<a href=?page=". ($page-1) .">上页</a>  ";
}
for ($i=1; $i <=15 ; $i++)
{
 echo "<a href=?page=$i>".$i."</a>  ";
}
if ($page<$total)
{
 echo "<a href=?page=". ($page+1) .">下页</a>  ";
}
echo "<a href=?page=" . $a .">末页</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
?>