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>";
?>