Welcome Guest, Not a member yet? Register   Sign In
2 tables, need to select a value from the 2nd to help with the 1st
#1

[eluser]fancms[/eluser]
The way I have my database set up I have a news table and a news category table. When a news article is created, it saves the category assigned to it by the news category id number in a column called cat.

What I'm having trouble with is displaying the name of the category while viewing all the news articles. Currently it only shows the id #; I need to figure out how to show it by its name. I'm not sure if this would require a second query or not.

I am using Template Lite for my templating engine; not sure if that will make a difference. I'm really stumped here; hope someone can point me the right way Smile
#2

[eluser]xwero[/eluser]
select t1.title,t1.content,t2.category from articles as t1, categories as t2 where t1.category_id=t2.id
#3

[eluser]fancms[/eluser]
Thanks so much, xwero Big Grin Much appreciated!!

For anyone else who might happen to question how to set this up using Template Lite, here's what my code looks like:

In Controller:
Code:
$query = $this->db->query("SELECT t1.subject,t1.article,t2.name from news as t1, newscats as t2 where t1.cat=t2.id");
$this->tlite->assign("articles", $query->result_array());

In Template File:
Code:
{foreach item=art from=$articles}
{$art.subject}<br />
{$art.name}<br /> #<--That would be the category name; took me a bit to figure that out :)
{$art.article}
{foreachelse}
No articles
{/foreach}




Theme © iAndrew 2016 - Forum software by © MyBB