PHP文章数据渲染
页面效果
数据库查询结果
查询所有数据 php代码
$posts = baixiu_fetch_all("select
posts.id as posts_id,
posts.title as posts_title,
posts.created as posts_created,
categories.name as categories_name,
categories.slug as categories_slug,
posts.`status`as posts_status,
users.slug as users_slug,
users.nickname as users_nickname
from posts
inner join categories on posts.category_id= categories.id
inner join users on posts.category_id = users.id
where {
$where}
order by posts.created desc
limit {
$offset},{
$size}
");
html页面全部数据渲染
<?php foreach ($posts as $item): ?>
<tr>
<td class="text-center"><input type="checkbox"></td>
<td><?php echo $item['posts_title']?></td>
<!-- <td><?php// echo get_users($item['user_id']);?></td> -->
<!-- <td><?php //echo get_categories($item['category_id'])?></td> -->
<td><?php echo $item['categories_name'];?></td>
<td><?php echo $item['categories_slug']?></td>
<td class="text-center"><?php echo xiu_convert_date($item['posts_created'])?></td>
<td class="text-center"><?php echo xiu_convert_status($item['posts_status'])?></td>
<td class="text-center">
<a href="javascript:;" class="btn btn-default btn-xs">编辑</a>
<a href="/admin/posts-delete.php?id=<?php echo $item['posts_id'];?>" class="btn btn-danger btn-xs">删除</a>
</td>
</tr>
<?php endforeach?>
时间格式处理 php 代码
** 转换时间格式
* @param 参数 $created
* @return
*/
function xiu_convert_date ($created) {
$timestamp = strtotime($created);
return date('Y年m月d日<b\r>H:i:s', $timestamp);
}
时间格式处理 HTML 代码
<td class="text-center"><?php echo xiu_convert_date($item['posts_created'])?></td>
php三选状态处理
/** 转换时间格式 多种状态
* @param 参数 $created
* @return
*/
function xiu_convert_status ($status) {
$dict = array(
'published' => '已发布',
'drafted' => '草稿',
'trashed' => '回收站'
);
//返回有值的数据
return isset($dict[$status]) ? $dict[$status] : '未知';
}
HTML 三选状态处理
<td class="text-center"><?php echo xiu_convert_status($item['posts_status'])?></td>
//实现分页查询
效果图
php分页,分类
//实现分页功能
$page = empty($_GET['page'])? 1 : (int)$_GET['page'];
$size = 20;
//计算出越过多少条
$offset = ($page-1) * $size;
//限制-1值
if ($page < 1){
header('location:/admin/posts.php?page=1');
}
//计算出展示页码 总共5页
$begin = $page - 2;
$end = $begin + 4;
//判断临界点 大于0 小于最多纪录
$begin = $begin < 1 ? 1 : $begin;
$begin = $end - 4;
$where = '1 = 1';
//接收分类的分页处理
$search = '';
//传了参数就用选择语句 意思是没有all 并且存在分类,就选择分类 否则就不分类
if (isset($_GET['categories']) && $_GET['categories'] !=='all' ){
$where .= ' and categories.id='. $_GET['categories'];
$search .='&categories=' . $_GET['categories']; //ID
}
//选择分类
if (isset($_GET['status']) && $_GET['status'] !=='all' ){
$where .= " and posts.status='{
$_GET['status']}'";
$search .