【问题标题】:Returning and using multidimensional array of records from database in CodeIgniter 2.0在 CodeIgniter 2.0 中从数据库返回和使用多维记录数组
【发布时间】:2011-05-13 06:16:06
【问题描述】:

大家好! 好吧,我正在尝试使用 codeigniter,但在我看来,我在尝试从表中检索和显示数据时弄得一团糟 这是代码sn-p。

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

表结构: 文章表:articleID、articleContent、日期 标签表:tagID、tagName articleTagRelation : aricleID,tagID {两者的组合是我的主键} CI模型: article_model.php 公共函数 getAllTags($postId){ $this->db->select('articleTagRelation.tagId 作为 tagId, articleTagRelation.postId 作为 postId, article.tagName 作为 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) { 返回$标签; } } } 公共函数 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); // 我正在尝试获取所有关联标签的数组。 $post=array($data['row'],$data['articletags']); } }别的{ echo '没有找到!'; } 返回 $post; } 我的控制器文件 文章.php 我在索引函数中调用这个函数 $data['rows'] = $this->blog_model->getAllArticles(); 然后通过传递数据数组加载视图 现在事情变得混乱的部分 在我看来 echo $r->articleId // 工作正常 echo $r->articletags->tagId //给我一个错误信息 谁能帮我打印那些tagId

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    首先,您根本不需要 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 这样的索引

    【讨论】:

    • 谢谢@jondavidjohn。我浪费了很多时间来思考如何去做。一旦我用谷歌搜索并得到你的答案并在 2 分钟内第一次完成我的工作。非常感谢。
    【解决方案2】:
    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']"。

    【讨论】:

    • 我试过这个,但对我来说没有用:(谢谢你的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多