Welcome Guest, Not a member yet? Register   Sign In
Join Query Help
#1

[eluser]Lee.[/eluser]
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;

Code:
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;

Code:
{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?


Messages In This Thread
Join Query Help - by El Forum - 05-28-2013, 07:58 AM
Join Query Help - by El Forum - 05-28-2013, 10:56 AM
Join Query Help - by El Forum - 05-28-2013, 11:39 AM
Join Query Help - by El Forum - 05-28-2013, 11:54 AM
Join Query Help - by El Forum - 05-28-2013, 12:00 PM
Join Query Help - by El Forum - 05-28-2013, 12:00 PM
Join Query Help - by El Forum - 05-28-2013, 12:05 PM
Join Query Help - by El Forum - 05-28-2013, 01:09 PM
Join Query Help - by El Forum - 05-28-2013, 02:42 PM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 05:10 AM
Join Query Help - by El Forum - 05-29-2013, 12:35 PM
Join Query Help - by El Forum - 05-29-2013, 12:36 PM
Join Query Help - by El Forum - 05-29-2013, 12:39 PM
Join Query Help - by El Forum - 05-30-2013, 09:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB