前端应用文章模块并显示
This commit is contained in:
@@ -11,8 +11,66 @@ use think\facade\Config;
|
||||
|
||||
class Index extends Base
|
||||
{
|
||||
// 首页
|
||||
public function index()
|
||||
{
|
||||
// 获取文章列表
|
||||
$articles = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$articleList = [];
|
||||
foreach ($articles as $article) {
|
||||
$cate = $article['cate'];
|
||||
$cateName = Db::table('yz_article_category')
|
||||
->where('id', $cate)
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->value('name');
|
||||
|
||||
if ($cateName) { // 只添加有效的分类
|
||||
if (!isset($articleList[$cateName])) {
|
||||
$articleList[$cateName] = [];
|
||||
}
|
||||
$articleList[$cateName][] = $article;
|
||||
}
|
||||
}
|
||||
|
||||
// 调试信息
|
||||
trace('Articles: ' . json_encode($articles, JSON_UNESCAPED_UNICODE));
|
||||
trace('ArticleList: ' . json_encode($articleList, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
// 将变量传递给视图
|
||||
View::assign([
|
||||
'articleList' => $articleList,
|
||||
]);
|
||||
return view('index');
|
||||
}
|
||||
|
||||
// 文章列表
|
||||
public function articlelist()
|
||||
{
|
||||
$articles = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$articleList = [];
|
||||
foreach ($articles as $article) {
|
||||
$cate = $article['cate'];
|
||||
$cateName = Db::table('yz_article_category')->where('id', $cate)->value('name');
|
||||
if (!isset($articleList[$cateName])) {
|
||||
$articleList[$cateName] = [];
|
||||
}
|
||||
$articleList[$cateName][] = $article;
|
||||
}
|
||||
|
||||
View::assign('articleList', $articleList);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user