Reputation: 1102
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
Upvotes: 2
Views: 2830
Reputation: 62392
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
Upvotes: 3
Reputation: 131
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.
Upvotes: 0