• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to structure effectively news and category relationship ?

#1
i have two tables in database

CATEGORY table below if parent id is set to 0 is main category if it is set to any its subcategory
cat_id | name | parent_id
---------------------------------------
1 | Electronics | 0
2 | Computer | 1
3 | Computer | 1

NEWS table below

id | news_name | news_text | cat_id
---------------------------------------


i wonder how to make them related each others via common id ? Any ideas
Reply

#2
Thank you very much i will test it.
Reply

#3
(12-09-2014, 08:21 AM)paju89 Wrote: i have two tables in database  

CATEGORY table below  if parent id is set to 0 is main category if it is set to any its subcategory
cat_id      |   name        |   parent_id
---------------------------------------
1       |   Electronics |   0
2       |   Computer    |   1
3       |   Computer    |   1

NEWS table below

id      |   news_name        |   news_text |   cat_id
---------------------------------------


i wonder how to make them related each others via common id ? Any ideas

Where did my answer go Dodgy
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply

#4
i saw and when i replied it disappeared
Reply

#5
Please post it again
Reply

#6
(12-09-2014, 08:21 AM)paju89 Wrote: i have two tables in database  

CATEGORY table below  if parent id is set to 0 is main category if it is set to any its subcategory
cat_id      |   name        |   parent_id
---------------------------------------
1       |   Electronics |   0
2       |   Computer    |   1
3       |   Computer    |   1

NEWS table below

id      |   news_name        |   news_text |   cat_id
---------------------------------------


i wonder how to make them related each others via common id ? Any ideas

Try something like this:

PHP Code:
$this->db->select('category, news')
 
   ->from('category as t1')
 
   ->where('t1.cat_id'$id)
 
   ->join('news as t2''t1.cat_id = t2.cat_id''LEFT')
 
   ->get(); 
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply

#7
If you just want to join all articles to the associated category you just need a quey like that.

PHP Code:
$this->db->from('category')
     ->
join('news''category.cat_id = news.cat_id')
     ->
where('category.cat_id'$cat_id)
     ->
get(); 

cat_id is the matching key for both tables.

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.