CodeIgniter 3博客:不使用SQL Join显示作者姓名而非ID
实现方案:在CodeIgniter 3.1.8中不使用SQL JOIN显示作者姓名
没问题,我来帮你搞定这个需求——在不使用SQL JOIN的前提下,把博客文章里的作者ID替换成对应的作者姓名。下面是具体的实现步骤,完全适配CodeIgniter 3.1.8:
1. 完善模型层
首先我们需要两个模型,分别处理作者和文章的数据逻辑:
Authors_model(获取作者信息)
<?php class Authors_model extends CI_Model { // 根据ID获取作者姓名 public function get_author_by_id($author_id) { $this->db->select('first_name, last_name'); $this->db->where('id', $author_id); $query = $this->db->get('authors'); // 返回单条记录数组,为空则返回空数组避免报错 return $query->row_array() ?? []; } // 批量获取作者信息(优化用) public function get_authors_by_ids($author_ids) { $this->db->select('id, first_name, last_name'); $this->db->where_in('id', $author_ids); return $this->db->get('authors')->result_array(); } }
Posts_model(获取文章列表)
<?php class Posts_model extends CI_Model { // 获取所有文章 public function get_all_posts() { // 可根据需求添加排序、筛选条件,比如按发布时间倒序 $this->db->order_by('created_at', 'DESC'); $query = $this->db->get('posts'); return $query->result_array(); } }
2. 控制器层处理数据关联
在控制器中,我们需要把文章数据和作者姓名关联起来。这里提供两种方案:基础版(适合数据量小的场景)和优化版(适合数据量大的场景,减少数据库查询次数)。
基础版(单条查询)
<?php class Blog extends CI_Controller { public function __construct() { parent::__construct(); // 加载所需模型 $this->load->model('Posts_model'); $this->load->model('Authors_model'); } public function index() { // 获取所有文章数据 $posts = $this->Posts_model->get_all_posts(); // 遍历文章,为每篇文章匹配作者姓名 foreach ($posts as &$post) { if (!empty($post['author_id'])) { $author = $this->Authors_model->get_author_by_id($post['author_id']); // 拼接作者全名 $post['author_name'] = $author['first_name'] . ' ' . $author['last_name']; } else { // 处理无作者的情况 $post['author_name'] = 'Unknown Author'; } } // 将数据传递给视图 $data['posts'] = $posts; $this->load->view('blog/post_list', $data); } }
优化版(批量查询,推荐)
如果你的博客文章较多,基础版会多次查询数据库,效率较低。优化版通过一次查询获取所有需要的作者信息,再通过数组映射匹配:
public function index() { $posts = $this->Posts_model->get_all_posts(); // 收集所有非空的作者ID并去重 $author_ids = array_filter(array_column($posts, 'author_id')); $author_ids = array_unique($author_ids); $author_map = []; if (!empty($author_ids)) { // 批量获取作者信息 $authors = $this->Authors_model->get_authors_by_ids($author_ids); // 转成以作者ID为键的关联数组,方便快速查找 foreach ($authors as $author) { $author_map[$author['id']] = $author['first_name'] . ' ' . $author['last_name']; } } // 为文章匹配作者姓名 foreach ($posts as &$post) { $post['author_name'] = isset($author_map[$post['author_id']]) ? $author_map[$post['author_id']] : 'Unknown Author'; } $data['posts'] = $posts; $this->load->view('blog/post_list', $data); }
3. 视图层展示数据
最后在视图文件(application/views/blog/post_list.php)中,直接使用处理好的author_name字段即可:
<h1>我的博客</h1> <div class="post-container"> <?php foreach ($posts as $post): ?> <article class="blog-post"> <h2><?php echo htmlspecialchars($post['title']); ?></h2> <div class="post-meta"> <span>作者:<?php echo htmlspecialchars($post['author_name']); ?></span> <span>发布时间:<?php echo date('Y-m-d H:i', strtotime($post['created_at'])); ?></span> </div> <div class="post-content"> <?php echo nl2br(htmlspecialchars($post['content'])); ?> </div> <hr> </article> <?php endforeach; ?> </div>
注意事项
- 确保你的
posts表中有author_id字段,用于关联authors表的id; - 使用
htmlspecialchars()转义输出内容,防止XSS攻击; - 若需要处理作者不存在的情况(比如
author_id无效),视图中会显示“Unknown Author”,你可以根据需求修改这个默认值。
内容的提问来源于stack exchange,提问作者Razvan Zamfir
相关产品推荐
相关产品推荐

