如何在Codeigniter中显示每个帖子的类别

时间:2018-11-17 19:06:44

标签: php codeigniter

我想为每个帖子显示类别。通过教程,我能够创建<div (mouseenter)="menuTrigger.openMenu()">Menu</div> <div #menuTrigger="matMenuTrigger" [matMenuTriggerFor]="menu"> <mat-menu #menu="matMenu" > <div (mouseleave)="menuTrigger.closeMenu()"> <button mat-menu-item>Item 1</button> <button mat-menu-item>Item 2</button> </div> </mat-menu> </div> ,但无法在每个帖子上显示类别。

这是我的帖子模型:

MY_Model

现在,我想按名称显示类别。这是我到目前为止的内容:

<?php
class Post_m extends MY_Model
{
    protected $_table_name = 'posts';
    protected $_order_by = 'pubdate desc, id desc';
    protected $_timestamps = TRUE;
    public $rules = array(
        'pubdate' => array(
            'field' => 'pubdate',
            'label' => 'Publication date',
            'rules' => 'trim|required|exact_length[10]'
        ),
        'title' => array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'trim|required|max_length[100]'
        ),
        'slug' => array(
            'field' => 'slug',
            'label' => 'Slug',
            'rules' => 'trim|required|max_length[100]|url_title'
        ),
        'body' => array(
            'field' => 'body',
            'label' => 'Body',
            'rules' => 'trim|required'
        )
    );

    public function get_new ()
    {
        $post = new stdClass();
        $post->title = '';
        $post->slug = '';
        $post->body = '';
        $post->name = '';
        $post->post_image = '';
        $post->pubdate = date('Y-m-d');
        return $post;
    }

    public function set_published(){
        $this->db->where('pubdate <=', date('Y-m-d'));
    }

    public function get_recent($limit = 3){

        // Fetch a limited number of recent articles
        $limit = (int) $limit;
        $this->set_published();
        $this->db->limit($limit);
        return parent::get();
    }

    public function get_categories()
    {
        $this->db->order_by('name');
        $query = $this->db->get('categories');
        return $query->result_array();
    }

}
?>

如果您注意到了,我添加了<div class="table-responsive"> <table class="table table-striped table-sm"> <thead> <tr> <th>#</th> <th>Title</th> <th>Posts</th> <th>Category</th> <th>Date</th> <th></th> </tr> </thead> <tbody> <?php if(count($posts)): foreach ($posts as $key => $post) if ($key >= 1) { ?> <tr> <td><?php echo $key; ?></td> <td><?php echo anchor('admin/posts/edit/' . $post->id, $post->title); ?></td> <td><?php echo e(limit_to_numwords(strip_tags($post->body), 10)); ?></td> <td><?php echo $post->category_id; ?></td> <td><?php echo $post->pubdate; ?></td> <td><?php echo btn_edit('admin/posts/edit/' . $post->id); ?></td> <td><?php echo btn_delete('admin/posts/delete/' . $post->id); ?></td> </tr> <?php } ?> <?php else: ?> <tr> <td class="center" colspan="3">We could not find any articles.</td> </tr> <?php endif; ?> </tbody> </table> </div> ,但它只显示INT而不显示类别名称。

请帮忙!

1 个答案:

答案 0 :(得分:0)

您还没有发布控制器,所以我不确定您如何使用当前模型,但是如果您还想显示每个帖子所在类别的名称,则需要使用联接,像这样:

public function get_posts() {

    $this->db->select('
      categories.id AS category_id,
      categories.name,
      posts.id,
      posts.title,
      posts.body,
      posts.pubdate
    ');
    $this->db->from('posts');
    $this->db->join('categories', 'posts.category_id = categories.id');
    return $this->db->get()->result_array();

}

它的作用是,它从posts表中获取所有行,并且对于每个post行,还从category表中获取相应的类别信息(包括名称)。它可以使用每个帖子行中的类别ID找到正确的类别信息。然后,它从两个表中返回信息。