Write numeric topic (1 1.2, 1.2.1) in MySQL with PHP

1

I have the following problem: I need to write in a database some constraints, which have their item numbers and subitem as in this example:

  • Test 1
  • Test 2

    2.1 Test

  • I tried to do with numeric(4,2) or float but when it happens that a condition has more subitems, for example 2.2.1, it no longer works. Can anyone help me?

        
    asked by anonymous 21.02.2018 / 22:15

    1 answer

    1

    None of these approaches is correct. Let's say you're putting the logs like this:

    numero | nome
    1      | Teste 1
    2      | Teste 2
    2.1    | Teste
    2.1.1  | Outro teste
    

    The solution would be to create two new columns, one to be the new primary key and one to the parent registry id:

    id | id_pai | numero | nome
    1  | null   |      1 | Teste 1
    2  | null   |      2 | Teste 2
    3  | 2      |      1 | Teste
    4  | 3      |      1 | Outro teste
    

    Obviously, the id_pai field would be a foreign key for the id field of the same table. The id field would be the primary key. The number is formed by traversing the parent, grandfather, etc record until you reach a null and go concatenating the values of the numero field that are found.

        
    21.02.2018 / 22:36