Lee
Well-known member
I'm a beginner to PHP and i'm building a little script just to learn a little, using code igniter.
I have the following function;
and the following template;
What happens is, if the article has no comments nothing is selected. But if the article has more than one comment, it is selected over and over again. No doubt i'm making a very silly mistake here, but I was under the impression what I was doing is selecting the articles and then the comments that relate to each article. What actually appears to be happening is i'm selecting the article over and over again for each comment.
What I want to do is select all the articles, then if there is any comments - display those too.
Any help for a new comer to PHP?
I have the following function;
PHP:
public function index()
{
$this->db->select('*');
$this->db->from('articles');
$this->db->join('comments', 'comments.article_id = articles.id');
$query = $this->db->get();
$data = array(
'article_entries' => $query->result_array(),
);
$this->load->view('header');
$this->parser->parse('index_page', $data);
$this->load->view('footer');
}
and the following template;
HTML:
{article_entries}
<h1>{title}</h1>
<h1>{subtitle}</h1>
<p>{content}</p>
<hr />
{comment} by {username}
{/article_entries}
What happens is, if the article has no comments nothing is selected. But if the article has more than one comment, it is selected over and over again. No doubt i'm making a very silly mistake here, but I was under the impression what I was doing is selecting the articles and then the comments that relate to each article. What actually appears to be happening is i'm selecting the article over and over again for each comment.
What I want to do is select all the articles, then if there is any comments - display those too.
Any help for a new comer to PHP?