从CodeIgniter 2.0中的数据库返回并使用多维数据记录 [英] Returning and using multidimensional array of records from database in CodeIgniter 2.0
问题描述
我正在尝试codeigniter,但似乎在我试图从表中检索和显示数据时,我有一些混乱
这里是代码片段。
我想检索存储在我的文章表中的所有文章,我需要从关系表和标签表中分别提取所有与每篇文章相关的标签articleTagRelation和标签
表结构:
文章表:articleID,articleContent,date
标签表:tagID,tagName
articleTagRelation:aricleID,tagID {两者的组合是我的主键}
CI模型:
article_model.php
public function getAllTags($ postId){
$ this-> db-> select('articleTagRelation.tagId as 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){
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); //我想获得一个所有的关联标签的数组。
$ post = array($ data ['row'],$ data ['articletags']);
}
} else {
echo'nothing found!';
}
return $ post;
}
我的控制器文件
article.php
I' m在索引函数中调用此函数
$ data ['rows'] = $ this-> blog_model-> getAllArticles();
然后通过传递数据数组加载视图
现在的东西变得凌乱
在我的视图
echo $ r-> articleId //工作正常
echo $ r-> articletags-> tagId //给我一个错误消息
任何人都可以帮我打印这些tagIds
你不需要foreach来获取标签信息,它从一个query_result返回。
像这样...
if($ qTag-> num_rows()> 0){
return $ qTag-> result();
}
else {
return array(); //如果没有标签则返回空数组
}
getAllArticles()
public function getAllArticles(){
//删除不必要的select和从
//下面完成同样的事情
$ this-> db-> order_by('date','desc');
$ query = $ this-> db-> get('Article');
if($ query-> num_rows()> 0){
//将结果存储在一个变量中,将返回
$ articles = $ query-> result();
//确保你foreach通过引用,所以你做的
//到interated $ article的更改将实际文章
// result
foreach($ articles as& $ article){
//为你的文章tags创建一个新属性并保存
//标签数组
$ article-> tags = $ this-> getAllTags($ article-> articleId);
}
} else {
echo'nothing found!';
}
return $ articles;
}
最后要注意的是,当你现在引用 $ r->标签
是一个数组,所以你可以 foreach
来处理所有标签,或者引用一个索引 $ r-> tags [3] - > tagId
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
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
这篇关于从CodeIgniter 2.0中的数据库返回并使用多维数据记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!