Returning and using multidimensional array of records from database in CodeIgniter 2.0

倾然丶 夕夏残阳落幕 提交于 2019-12-24 02:18:47

问题


Hey guys ! Well I was trying out codeigniter but it seems to me that I have made some kind of mess while trying to retrieve and display the data from the tables here is the code snippet.

I want to retrieve all the articles stored in my article table along with that I need to pull out all the tags associated with each article from the relationship table and the tag table called articleTagRelation and tags respectively

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

回答1:


First you don't need the foreach at all to get the tag information, it comes back from a query_result.

like this...

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

Then to build your article, do this with 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;
}

The last thing to note is that when you now reference $r->tags that is an array, so you can foreach it to process all tags, OR reference an index like $r->tags[3]->tagId




回答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" only works if you return results as an object, use "$r->articletags['tagId']" instead.



来源:https://stackoverflow.com/questions/5987951/returning-and-using-multidimensional-array-of-records-from-database-in-codeignit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!