1
The System:
I have a select
with some options and sub-options (I already explain). This select
is generated by php according to the categories that are registered in the database, that is, I have the CATEGORY 01 but in this category I register a subcategory, so it’s like Subcategory 01, if I register in that subcategory another subcategory, it looks like Sub-category 01 and so infinitely.
In the generation of HTML, select
, they all come as option
(not nested inside each other as in UL > LI > UL > LI, etc).
Select looks like this:
<select id="selecao">
<option value="cat1">Eletronicos</option>
<option value="sec1">Informática</option>
<option value="prod1">Monitores</option>
<option value="prod2">Gabinetes</option>
<option value="cat2">CATEGORIA 02</option>
</select>
I need to capture the value of the product category, that is, if I click on option
Monitors my variable in javascript will capture the value of the product category (are those marked as cat1 and cat2)
EDIT:
Structure of the database:
In the bank I have a table that used the categories, each category has an id, a parent_id and a Depth
parent_id tells which parent category it is associated with and Depth tells which category level
- Electronic has parent_id = null and Depth = 0
- Informatics is subcategory of Electronics so it has parent_id = 1 (pq id of Electronics in the database is 1) and Depth = 1
- Monitors is a subcategory of the Informatics subcategory and therefore has parent_id = 10 (because the Informatics id in the database is 10) and Depth = 2
This is the idea of relationship of the categories in the table.
I thank all who can help.
I did not find similar question that helps solve this problem, if you know of any that will help me, check here. Thanks
What if the guy selects "Electronics" or "Informatics"?
– Sam
Lougans, in this form in which all are in the same select will be considered as being "brothers" without different levels of kinship. How’s your structure at the bank? and a suggestion: that such break into several selects, depending on the selected parent category it displays another select child (if any) associated that parent?
– Matheus Cristian
@Sam, The same thing. The main categories are presented in Uppercase and the subcategory in normal way. This spacing is how it appears in the rendering of html on account of I want to see a way to do this if you have it of course. It is to avoid having to change all the code that has already been written by someone else hehe...
– Lougans de Matos
So regardless of the option checked, you want to catch cat1, cat2 etc..?
– Sam
@Sam the idea is to record in a variable of javascript the value of the main categories cat1, cat2. That’s right. If I click on Computer or monitors or any other that is within the "nesting" of the main category, it will capture the cat1
– Lougans de Matos
@Matheuscristian I edited my question asking how is the structure and relationships, see if I made sense kkkk
– Lougans de Matos
Oh yes, I understand better. If you just want to take the category name, how about you put it in the value itself, instead of "prod1" or "prod2" the "cat1" only? put the category itself into value
– Matheus Cristian
@Matheuscristian this will all come dynamically, right? I gave example with name of categories to exemplify here, but of course the value of these categories will be a number, an id that will reference there in the database, but I want to capture by javascript, rsrs... Unfortunately this code was not made by me initially and wanted to solve it in the current structure of the code (otherwise I will have to change a lot of things), if it is not possible :/
– Lougans de Matos
But the values will always be like this in this pattern: cat1, sec1, prod1, prod2, cat2 etc...?
– Sam
@Sam I said in the comment there to Matheus that I only put these names by example, hehe.. The value will be all numbers without differentiation between them, only an auto-increment value.
– Lougans de Matos