Tuesday, 15 March 2011

Showing a comma seperated list of Database ID's in that order PHP/MySQL -



Showing a comma seperated list of Database ID's in that order PHP/MySQL -

i have database 2 tables looks this:

categories

name | items --------------------------------- @ home | 3,4,8,9 @ office | 10,3,2,1

items

name | id -------------------------------- prepare auto | 3 mow lawn | 4 garbage | 8 laundry | 9 desk clean | 10

in categories table comma separated numbers represent list of items id.

how sql select selects of items in specific category (e.g. "at home") , orders them in same order items column.

the database has been simplified create question easier understand.

also if can think of improve way organize database type of application know.

customized order critical project

if possible, create 3rd table normalize db, or @ edit below :-)

categories name | id --------------------------------- @ home | 1 @ office | 2 items name | id -------------------------------- prepare auto | 3 mow lawn | 4 garbage | 8 laundry | 9 desk clean | 10 links cat_id | item_id -------------------------------- 1 | 3 1 | 4 1 | 8 1 | 9 2 | 10 2 etc.

then go:

select items.name items left bring together links on items.id = links.item_id items.cat_id = 1 order items.id

i realized operated on assumption every items can in multiple categories. if want 1 item --> 1 category, there's no need 3rd table, add together field items, , 1 custom-ordering:

items

name | id | cat_id | order ----------------------------------------- prepare auto | 3 | 1 | 1 mow lawn | 4 | 2 | 1 kill cat | 9 | 1 | 2 select name items cat_id = 1 order order

php mysql

No comments:

Post a Comment