Assign an Arttigo in more than one php category

Asked

Viewed 45 times

0

I have to enter a register in the database assigned to more than one category , how could I do this ? In case would appear the available category options in my bank and I would select them with a checkbox

  • Wouldn’t it be the opposite, an article having several categories? usually create an associative table who has the id of the article and the category, for grabs the descriptions use joins in sql.

1 answer

0


In this case your tables in the bank should have more or less this structure:

|--------artigo----------|  
|id - titulo - nome - etc|  
|------------------------|  

|---categoria---|  
|id - nome - etc|  
|---------------|  

|-------artigo_categoria------|  
|id - id_artigo - id_categoria|  
|-----------------------------|  

With this you get an article to have more than one category. When you do select just use a inner join to associate.

select * from artigo
inner join artigo_categoria on (artigo.id = artigo_categoria.id_artigo)
inner join categoria on (artigo_categoria.id_categoria = categoria.id)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.