ThinkPHP的多表查询+分页范例
对于一个PHP程序员来说,多表查询是经常遇到的事,下面介绍一下ThinkPHP的多表查询+分页范例
<?php
$db = M( "Article" );
$fix = C( "DB_PREFIX" );
$table = $fix."article";
$table2 = $fix."article_category";
$page_size = 15; //每页显示记录数
$record_sum = count( $db -> field('art_id') -> where( "art_public='1'" ) -> select() );//记录总数
$Page = new ZYPage($record_sum, $page_size, 5);
$list = $db -> field( "$table.art_id,$table.art_title,$table.art_content,$table.art_description,$table2.cate_id,$table2.cate_name" ) ->
join( "$table2 on $table.cate_id=$table2.cate_id" ) ->
where( "$table.art_public='1'" ) ->
order( "$table.art_create_time desc,$table.art_id desc" ) ->
limit($Page->firstRow.",".$Page->listRows) ->
select();
$this -> assign( "article", $list); //输出文章列表
$show = $Page -> show();
$this -> assign( "page", $show); //输出分页
?>