`
xieye
  • 浏览: 803874 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

php的zf1框架的db类以及分页组件使用

    博客分类:
  • PHP
阅读更多
首先,用composer安装zf1的部分组件
"require": {
        "zf1/zend-db":"1.12.11",
        "zf1/zend-paginator":"1.12.11"
    }


注意,与另外两个分页组件比较看,zf1的分页组件是最简单 的,因为它智能的判断了总数!

建表:
CREATE TABLE `test_databases` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `db_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '库名',
  `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '测试用户id',
  `created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  `updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB


请自行插入一百条数据。

程序1.php如下:
<?php
require __DIR__ . "/../vendor/autoload.php";
$params = array ('host'     => '127.0.0.1',
		'username' => 'root',
		'password' => 'root',
		'dbname'   => 'test1',
		'charset'  => 'utf8mb4',
		
);

$page=1;
if (isset($_GET['page']))
	$page = intval($_GET['page']);
if ($page<1) {
	$page=1;
}

$db = Zend_Db::factory('PDO_MYSQL', $params);

/**
 * 构造基础查询,每个查询该函数的内容都不一样,
 * 用函数的好处是 : 查询代码不重复。
 */
$select = $db->select();
//这里也可以join之类
// 。。。 总之可以比较复杂,这个select。
$select->from("test_databases",["db_name","user_id"]); //参数null不可以省略。否则全选。
$select->where("id<100");
$select->order("id asc" );
$paginator = Zend_Paginator::factory($select);

//设置分页对象,包括 当前页,每页显示几个结果,页码导航显示几个链接。
$paginator->setCurrentPageNumber($page) // 设置当前页的页码
          ->setItemCountPerPage(4) // 每页显示4条数据
          ->setPageRange(7); // 7个链接,这是较常规的,也可以9个链接。

//打印当前页的结果集。
$result = $paginator->getCurrentItems();
foreach ($result as $v) {
	echo $v["db_name"]." = " .$v["user_id"] ."<br>"; 
}
// 分页链接对象
$page_obj =  $paginator->getPages(); 
// 下面是$page_obj的详细信息。
// first           : integer 第一页的页码,固定为1
// firstItemNumber : integer 无用
// firstPageInRange: integer 中间的连续链接中的第一个数字页码
// current         : integer 当前页码
// currentItemCount: integer 无用
// itemCountPerPage: integer 无用
// last            : integer      最后页的页码
// lastItemNumber  : integer 无用
// lastPageInRange : integer 中间的连续链接中的最后一个数字页码
// next            : integer    下页的页码
// pageCount       : integer 总页数
// pagesInRange    : array 中间链接的页码数组,每个元素就是一个整型的页码
// previous        : integer 上页的页码
// totalItemCount  : integer 无用,结果集总数

echo default_css();
echo get_page_url($page_obj);
// END



// 重要的构造分页链接的代码。
function get_page_url($page_obj) {
	$html='';
	if ($page_obj->pageCount) {
		//上页
		if (isset($page_obj->previous)) {
			$html.='<a href="'. get_url($page_obj->previous) .
			   '" rel="prev">Previous</a>';
		}else {
			$html.='<span class="disabled">Previous</span>';
		}
		
		//首页,若与中间连接重复,则不显示
		if ($page_obj->first < $page_obj->firstPageInRange ) {
			if ($page_obj->current== $page_obj->first) {
				$html.='<span class="current">'. $page_obj->current .'</span>';
			}else {
				$html .= '<a href="'. get_url($page_obj->first) .
				   '" >'. $page_obj->first .'</a>';
			}
		}
		
		if ($page_obj->firstPageInRange - $page_obj->first > 1) {
			$html .=  '<span class="dots">...</span>';
		}
		
		//中间连接
		foreach ($page_obj->pagesInRange as $v) {
			if ($page_obj->current== $v) {
				$html.='<span class="current">'. $page_obj->current .'</span>';
			}else {
				$html .= '<a href="'. get_url($v) . '" >'. $v .'</a>';
			}
		}
		
	    if ($page_obj->last - $page_obj->lastPageInRange > 1) {
			$html .=  '<span class="dots">...</span>';
		} 
		
	    //末页,若与中间连接重复,则不显示
		if ($page_obj->last > $page_obj->lastPageInRange ) {
			if ($page_obj->current== $page_obj->last) {
				$html.='<span class="current">'. $page_obj->current .'</span>';
			}else {
				$html .= '<a href="'. get_url($page_obj->last) .
				   '" >'. $page_obj->last .'</a>';
			}
		}
		
		//下页
		if (isset($page_obj->next)) {
			$html.='<a href="'. get_url($page_obj->next) .
			'" rel="next">Next</a>';
		}else {
			$html.='<span class="disabled">Next</span>';
		}
		$html = "<div class='pagerfanta'><nav>{$html}</nav></div>";
	}
	return $html;
}



// 分页链接网址函数
function get_url($page) {
	return "/1.php?page=".$page;
}

// 分页链接样式表
 function default_css()
{
	$css=<<<css
        <style>
            .pagerfanta {
            }

            .pagerfanta a,
            .pagerfanta span {
                display: inline-block;
                border: 1px solid blue;
                color: blue;
                margin-right: .2em;
                padding: .25em .35em;
            }

            .pagerfanta a {
                text-decoration: none;
            }

            .pagerfanta a:hover {
                background: #ccf;
            }

            .pagerfanta .dots {
                border-width: 0;
            }

            .pagerfanta .current {
                background: #ccf;
                font-weight: bold;
            }

            .pagerfanta .disabled {
                border-color: #ccf;
                color: #ccf;
            }



            .pagerfanta a,
            .pagerfanta span {
                border-color: blue;
                color: blue;
            }

            .pagerfanta a:hover {
                background: #ccf;
            }

            .pagerfanta .current {
                background: #ccf;
            }

            .pagerfanta .disabled {
                border-color: #ccf;
                color: #cf;
            }
                    </style>
css;
	return $css;
}


浏览器效果如下:


  • 大小: 9.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics