if ($this->is_login() || (!empty($token) && $this->auth($token))) {
if ($this->is_login() || (!empty($token) && $this->auth($token))) {
$sql = "SELECT *,(SELECT name FROM on_categorys WHERE id = a.fid LIMIT 1) AS fname,(SELECT COUNT(id) FROM on_links WHERE fid = a.id) AS link_num FROM on_categorys as a ORDER BY weight DESC,id DESC LIMIT {$limit} OFFSET {$offset}";
$sql = "SELECT *,(SELECT name FROM on_categorys WHERE id = a.fid LIMIT 1) AS fname,(SELECT COUNT(id) FROM on_links WHERE fid = a.id) AS link_num FROM on_categorys as a ORDER BY fid ASC, weight DESC, id ASC";
//统计总数
$count = $this->db->count('on_categorys', '*');
$count = $this->db->count('on_categorys', '*');
}
} elseif (!empty($token)) {
//如果存在token,则验证
else if( !empty($token) ) {
$this->auth($token);
$this->auth($token);
//查询所有分类
$sql = "SELECT *,(SELECT name FROM on_categorys WHERE id = a.fid LIMIT 1) AS fname,(SELECT COUNT(id) FROM on_links WHERE fid = a.id) AS link_num FROM on_categorys as a ORDER BY fid ASC, weight DESC, id ASC";
$sql = "SELECT *,(SELECT name FROM on_categorys WHERE id = a.fid LIMIT 1) AS fname,(SELECT COUNT(id) FROM on_links WHERE fid = a.id) AS link_num FROM on_categorys as a ORDER BY weight DESC,id DESC LIMIT {$limit} OFFSET {$offset}";
//统计总数
$count = $this->db->count('on_categorys', '*');
$count = $this->db->count('on_categorys', '*');
} else {
$sql = "SELECT *,(SELECT name FROM on_categorys WHERE id = a.fid LIMIT 1) AS fname,(SELECT COUNT(id) FROM on_links WHERE fid = a.id) AS link_num FROM on_categorys as a WHERE property = 0 ORDER BY fid ASC, weight DESC, id ASC";
$sql = "SELECT *,(SELECT name FROM on_categorys WHERE id = a.fid LIMIT 1) AS fname,(SELECT COUNT(id) FROM on_links WHERE fid = a.id) AS link_num FROM on_categorys as a WHERE property = 0 ORDER BY weight DESC,id DESC LIMIT {$limit} OFFSET {$offset}";
//统计总数
$count = $this->db->count('on_categorys','*',[
"property" => 0
]);
}
}
//原生查询
// 对每个子分类组按 weight 排序
$datas = $this->db->query($sql)->fetchAll();
foreach ($categories_by_fid as &$sub_categories) {
$datas = [
usort($sub_categories, function ($a, $b) {
return $b['weight'] <=> $a['weight'];
});
}
// 将子分类插入到对应父级分类后
$final_sorted_categories = [];
foreach ($sorted_categories as $parent_category) {
$final_sorted_categories[] = $parent_category;
if (isset($categories_by_fid[$parent_category['id']])) {