zf Framework db class paging sample sharing

  • 2021-01-25 07:21:44
  • OfStack

A paging example of the zf framework


<?php
isset($_GET['page']) ? $page = $_GET['page'] : $page = 1;
// The introduction of Loader class ( Autoload classes )
require_once("Zend/Loader.php");
// use Loader Classes introduce 1 a Db class 
Zend_Loader::loadClass("Zend_Db");
// The introduction of Zend_Db The state of the machine 
Zend_Loader::loadClass("Zend_Db_Statement_Pdo");
// Configure database connection information 
$Config = array('host' => '127.0.0.1' ,
    'username' => 'root' , 
    'password' => '111' , 
    'dbname' => 'test',
    'profiler' => "true"
    );
// tell Zend_Db Class operates on the database and database configuration information 
$Db = Zend_Db::factory('PDO_Mysql' , $Config); 
// Execute the encoding statement  
$Db -> query("set names utf8");
//-----------------------------------------------
// use fetchOne() Method to obtain the total number of entries in the table 
$Total = $Db -> fetchOne("select count(*) from gongsi");
// Define the number of displays per page 
$B = 50;
// Find the total pages 
$A = ceil($Total/$B);
//----- The following for 1 Series of query table, fetch result set, paging and other operations 
$Select = $Db ->select();
$Select -> from('sanguo',array('s_sheng as  provinces ','sum(s_gongzi) as  The total wages ','min(s_gongzi) as  The minimum wage ','max(s_gongzi) as  The highest salary ','avg(s_gongzi) as  The average salary '));
// $Select -> Where('s_gongzi>=3000');
// $Select -> Where("s_sheng=' hebei '");
// $Select -> order('s_sheng asc');
// $Select -> order('s_gongzi desc');
$Select -> group('s_sheng');  // grouping 
//$Select -> having(' The highest salary >10000');  // Additional conditions 
$Select -> order(' The highest salary  desc');  // The sorting 
$Select -> limit(0,0); // The interception 
$Select -> limitPage($page, $B); // paging 
/*SQL Statement equivalent to: 
select s_sheng as  provinces ,sum(s_gongzi) as  The highest salary  from sanguo group by s_sheng having  The highest salary >10000 order by  The highest salary  desc limit 0,10;
*/
$Result = $Db->fetchAll($Select);
echo "<table border='1' align='center' width='960' style='text-align:center'>";
echo "<tr><th> provinces </th><th> The total wages </th><th> The minimum wage </th><th> The highest salary </th><th> The average salary </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> Home page </a>&nbsp;&nbsp;";
if ($page>1) 
{
 echo "<a href=?page=". ($page-1) ."> On the page </a>&nbsp;&nbsp;";
}
for ($i=1; $i <=15 ; $i++) 
{ 
 echo "<a href=?page=$i>".$i."</a>&nbsp;&nbsp;";
}
if ($page<$Total) 
{
 echo "<a href=?page=". ($page+1) ."> On the next page </a>&nbsp;&nbsp;";
}
echo "<a href=?page=" . $A ."> At the end of the page </a>";
echo "</td>";
echo "</tr>";
echo "</table>";
?>


Related articles: