从CodeIgniter 2.0中的数据库返回和使用多维记录数组

时间:2011-05-13 06:16:06

标签: php codeigniter

嘿伙计们! 好吧,我正在尝试使用codeigniter,但在我看来,我在尝试检索和显示表中的数据时遇到了一些麻烦 这是代码片段。

我想要检索存储在我的文章表中的所有文章以及我需要从关系表中分别提取与每篇文章相关联的所有标签以及分别名为articleTagRelation和标签的标签表

Table structure :

Article table      : articleID, articleContent, date
Tags table         : tagID, tagName
articleTagRelation : aricleID,tagID {Combination of both is my primary key}
CI model :

article_model.php

    public function getAllTags($postId){
        $this->db->select('articleTagRelation.tagId as tagId, articleTagRelation.postId as postId, article.tagName as tagName,');
        $this->db->from('articleTagRelation');
        $this->db->join('Tags','Tags.tagId = articleTagRelation.tagId');
        $this->db->where('ArticleTagRelation.articleId',$postId);
        $qTag = $this->db->get();
        if($qTag->num_rows() > 0){
            foreach ($qTag->result() as $tag) {
                return $tag;
            }
        }
    }

    public function getAllArticles(){
        $this->db->select('*');
        $this->db->from('Article');
        $this->db->order_by('date','desc');
        $query=$this->db->get();
        if($query->num_rows()>0){
            foreach ($query->result() as $row) {
                $data['row'] = $row;
                $data['articletags'] = $this->getAllTags($row->articleId); // I'm trying to get get a array of all the associate tags.
                                $post=array($data['row'],$data['articletags']);
            }       
        }else{
            echo 'nothing found !';
        }
        return $post;
    }
my controller file
article.php
I'm calling this function in the index function
    $data['rows'] = $this->blog_model->getAllArticles();
and then loading the view by passing the data array 
now the part where things get messy 
in my view 
  
 echo $r->articleId // works fine
 echo $r->articletags->tagId //gives me a error message


Can any one help me out in printing those tagIds

2 个答案:

答案 0 :(得分:3)

首先,您根本不需要foreach来获取标记信息,它从query_result返回。

像这样......

if($qTag->num_rows() > 0){
    return $qTag->result();
}
else {
    return array();  //return empty array if no tags
}

然后要构建您的文章,请使用getAllArticles()

执行此操作
public function getAllArticles(){

    // removed uneccessary select and from
    // the below accomplishes the same thing
    $this->db->order_by('date','desc');
    $query = $this->db->get('Article');

    if ( $query->num_rows() > 0 ) {

        // store the result in a variable you will end up returning
        $articles = $query->result();

        // make sure you foreach by reference so that changes you make
        // to the interated $article will be made to the actual article
        // result
        foreach ($articles as &$article) {

            // create a new property of your article "tags" and save an
            // array of tags in it
            $article->tags = $this->getAllTags( $article->articleId );
        }

    } else {
        echo 'nothing found !';
    }

    return $articles;
}

最后要注意的是,当你现在引用作为数组的$r->tags时,你可以foreach来处理所有标记,或引用像$r->tags[3]->tagId这样的索引

答案 1 :(得分:0)

if($qTag->num_rows() > 0){
    foreach ($qTag->result() as $tag) {
          $tags[] = $tag; //this create the array with the result
    }
    return $tags;
}

“$ r-> articletags-> tagId”仅在您将结果作为对象返回时才有效,请使用“$ r-> articletags ['tagId']”。