Welcome Guest, Not a member yet? Register   Sign In
query big problem
#1

[eluser]polish[/eluser]
Hi!

I hava a big problem in my blog witch one sql query! I have 2 tables:
news (news_id, news_title, news_text, news_author, news_date, news_time),
comments (com_id, news_id, com_title, com_text, com_author, com_date)
and i need a one sql query (normal or ActiveRecord) who give me only ONE news and ALL comments for this news!

This query
SELECT distinct comments.*, news.news_text FROM comments INNER JOIN news ON comments.news_id = news.news_id WHERE comments.news_id=".$news_id
work but when i have two or more comments for one news the news is shown so many times how many is comments!


This is my model function:
function get_comments_for_news($news_id)
{
return $this->db->query("SELECT distinct comments.*, news.news_text FROM comments INNER JOIN news ON comments.news_id = news.news_id WHERE comments.news_id=".$news_id);
}

Controller function:
function show()
{
$news_id = $this->uri->segment(3);
IF(isset($news_id) and is_numeric($news_id))
{
$this->load->model('Comments');
$query = $this->Comments->get_comments_for_news($news_id);
$content = '';
if ($query->num_rows() > 0)
{
foreach($query->result() as $item)
{
$content .= $this->load->view('com_loop', $item, True);
}
}
else
{
$content = '<h1><center>No Comments</center></h1>';
}
$this->response['content'] = $content;
}
else
{
$this->response['content'] = '<h1><center>Bad link</center></h1>';
}
$this->load->view('index', $this->response);
}

And this is my view:
&lt;?PHP echo $news_text; ?&gt;<br /><br />
<img src="&lt;?php echo base_url(); ?&gt;images/ico_wtk.gif" alt="Ikonka" />
<div class="comtyt">&lt;?PHP echo $com_title ?&gt;</div><br />
<div class="text">&lt;?PHP echo $com_text; ?&gt;</div>
<div class="comtime">
<span class="newsat">~&lt;?PHP echo $com_author; ?&gt;&nbsp;&lt;?PHP echo $com_date; ?&gt;</span>
<span class="comlink" style="padding-right: 10px;"></span>
</div>

Any ideas???


Messages In This Thread
query big problem - by El Forum - 04-24-2009, 03:45 AM
query big problem - by El Forum - 04-24-2009, 04:18 AM
query big problem - by El Forum - 04-24-2009, 04:22 AM
query big problem - by El Forum - 04-24-2009, 04:33 AM
query big problem - by El Forum - 04-24-2009, 04:34 AM
query big problem - by El Forum - 04-24-2009, 04:49 AM
query big problem - by El Forum - 04-24-2009, 04:54 AM
query big problem - by El Forum - 04-24-2009, 08:48 AM
query big problem - by El Forum - 04-24-2009, 11:57 AM
query big problem - by El Forum - 04-25-2009, 12:44 AM
query big problem - by El Forum - 04-25-2009, 02:32 AM
query big problem - by El Forum - 04-26-2009, 03:35 AM
query big problem - by El Forum - 04-26-2009, 04:53 AM
query big problem - by El Forum - 04-26-2009, 05:09 AM
query big problem - by El Forum - 04-27-2009, 12:46 AM
query big problem - by El Forum - 04-27-2009, 01:04 AM
query big problem - by El Forum - 04-27-2009, 05:54 AM
query big problem - by El Forum - 04-27-2009, 06:16 AM
query big problem - by El Forum - 04-27-2009, 01:04 PM
query big problem - by El Forum - 04-28-2009, 06:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB