확인의 코드를 통해 살펴 보자 : 그래서보기에
// These are the datamaper objects
$articles = new Article_model();
$comments = new Comment_model();
// Get all of the articles ordered by pubdate and sorted desc
$articles->order_by('pubdate', 'desc')->get();
// $id now contains the articles
$id = $articles->id;
// Count the article id's for each comment that is associated with the article_id
$noOfComments = $comments->where('article_id', $id)->count();
// Create an array to store data
$recent_articles = array();
// Loop over the articles and create an array of information
foreach ($articles as $article)
{
$single_article = array(
'id' => $article->id,
'title' => $article->title,
'pubdate' => $article->pubdate,
'text' => $content,
'cover_img' => $article->cover_img,
'noOfComments' => $noOfComments,
);
// push the data to $recent_articles
array_push($recent_articles, $single_article);
}
당신이 $의 single_article 및 $ recent_articles하고 통과 $의 noOfComments을 사용하는 배열의의 모두 동일 할 것 그래서 당신은 당신이 가진 결과를 얻고 있습니다.
편집 :
$noOfComments = $comments->where('article_id', $id)->count();
나는 당신의 의견의 총 수의 수를 반환이 $comments->where() function
에 article id
를 통과 참조 모든. 그것이 모두하고있는 일이며, 구식인지 신 구속인지 구별하지 못합니다.
구식과 신구법을 구별하는 것이 어떻게됩니까? $comments->where()
기능은 어떻게 생겼습니까? 여러 개의 ID (배열)를 받아들이도록 확장하여 함수에서 비교하고 두 배열 (배열)을 반환하여 배열에 추가 할 수 있습니까?
$articles = new Article_model(); // IS YOUR TABLE REALLY NAMED 'article_models' ?
$comments = new Comment_model(); // IS YOUR TABLE REALLY NAMED 'comment_models' ?
$articles->order_by('pubdate', 'desc')->get_iterated();
$nr_of_articles = $articles->result_count();
if (count($nr_of_articles) > 0)
{
foreach ($articles as $article)
{
$recent_articles[] = $article->to_array();
// OR, IF LIKE ME YOU PREFER ID'S AS KEYS
$article_id = $article->id;
$recent_articles[$article_id] = $article->to_array();
$related_comments = $article->comment_model->all_to_array(); // ASSUMING YOUR TABLE IS NAMED 'comment_models'
$recent_articles[$article_id]['related_comments'] = $related_comments;
}
unset($articles); // REMEMBER TO FREE YOUR SERVER'S RAM! DATAMAPPER OBJECTS ARE HEAVY
}
어떤 이유로
$related_comments = $article->comment_model->all_to_array();
이 에게
이 작동하지 않는 경우 : 댓글 가정
구문 분석 템플릿과 함께 $ recent_articles를 전달하기 만하면됩니다. {articles} {/ articles} 루프를 만들어 표시 할 수있게되었습니다. 그럼 여기서 해결책이 무엇일까요? :) – Unsparing
이전에 코멘트가 추가되도록 새 배열 요소를 추가하십시오. 지금 당신은 당신의 질의로 전달 된 article_id에서 카운트를 잡는 중입니다, 당신은 그것을 고칠 필요가 있습니다. – Skewled
어떻게 보이게 될지 예를 들어 주시겠습니까? – Unsparing