Jump to content

Multiple Drop Down Selections


hybrid kill3r

Recommended Posts

I have a form that let's a user insert a page with the ability to select categories. I want them to have the ability to select multiple categories. I have a select drop down menu with the "multiple" attribute applied(<select name='category' multiple>).My question is, how would I get the categories they selected and display them like below:

1, 2, 3, 4, 5
The numbers are the ids of the categories, which is the value of the selections, and will be located in one column of the table that the current record is being created in.Another question...These categories are fetched from a database table(categories). The table that these records are being inserted to(pages) with this form has a column called pageID and a column called CategoryID. What I want to do is to have a page for a specific category that will fetch a list of all the pages that are under this category. Example: a page has categories of 1, 3, and 5. The user is viewing category 5. The category page should contain a list of content pages from category five. How would I create a query that would do this?
Link to comment
Share on other sites

For the multiple select, the easiest way is to name it as an array and then in PHP you'll have all of the values in an array:<select name='category[]'In PHP you get the array from $_POST and you can either loop through it and do something with each value, or join them together in a comma-separated string using implode, e.g.:$cats = implode(',', $_POST['category']);For the database issue, it's best not to store a comma-separated list of IDs in a single field, it's better to set up another table that has one column for each of the IDs, so you would need one column for the page ID, and one for the category ID. If your page is in 5 categories then you would have 5 records in that table with the same page ID and each category ID. That makes it a lot easier to get all of the items relating to a certain category.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...