기사 목록 표시 기능로그인

기사 목록 표시 기능

1. 데이터 준비

모든 기사 카테고리, 모든 기사 세부정보에 대한 정보를 준비하고 쿼리를 위한 데이터베이스를 운영해야 합니다

새 index.php 페이지를 생성합니다. 코드는 다음과 같습니다.

<?php
header("Content-Type:text/html;charset=utf-8");
//获取要查询的分类ID,0表示全部
$cid=isset($_GET['cid'])?intval($_GET['cid']):0;
//获取查询列表条件
$where='';
if($cid) $where="where cid=$cid";
//初始化数据库操作类
require './init.php';
//载入分页类
require './page.class.php';
//获取当前页码号
$page=isset($_GET['page'])?intval($_GET['page']):1;
//拼接查询条件
//获取总记录数
$sql="select count(*) as total from cms_article $where";
$results=$db->fetchRow($sql);
$total=$results['total'];
//实例化分页类
$Page=new Page($total,4,$page); //Page(总记录数,每页显示条数,当前页)
$limit=$Page->getLimit();  //获取分页链接条件
$page_html=$Page->showPage(); //获取分页html链接
//var_dump($total);die();
//分页获取文章列表
$sql="select id,title,content,author,addtime,cid from cms_article $where order by addtime DESC limit $limit";
$articles=$db->fetchAll($sql);
foreach ($articles as $k=>$v){
    //mb_substr(内容,开始位置,截取长度,字符集)
    $articles[$k]['content']=mb_substr(trim(strip_tags($v['content'])),0,150,'utf-8').'......';
}
$sql="select name from cms_category ORDER BY sort";
$categories=$db->fetchAll($sql);
//var_dump($categories);die();
require './indexHtml.php';

코드는 페이징 클래스 및 데이터베이스가 여러 쿼리 작업을 수행했습니다.

에서 얻은 데이터에는

페이지 매김 정보: $page_html

모든 기사 분류 정보: $categories

기사 세부정보: $ 기사

위 정보가 프런트 엔드 페이지에 번갈아 표시됩니다.

2. 프런트 엔드 표시 페이지 코드:

새 indexHtml.php 페이지

페이지가 다음과 같이 표시됩니다.

微信图片_20180306163519.png

제목 표시줄 분류 데이터 탐색:

微信图片_20180306163851.png

최신 기사 데이터 탐색:

微信图片_20180306164711.png

데이터베이스에 몇 가지 데이터를 더 삽입하면 페이지 매김 효과 페이지가 다음과 같이 표시됩니다.

微信图片_20180306165346.png


다음 섹션
<?php echo "文章详情显示页面";
코스웨어