I need to create a function that generates a loop inside the other only I haven’t defined the amount of loop(ie infinite)

Asked

Viewed 74 times

0

I need to create a function in PHP that generates a loop inside the other only I have not defined the loop (ie infinite), pq and a pyramid type system, ex: I have 1 dependent that have 4 dependent that in turn have 8 depends that have 2 more dependent and so on, until the end and I need to build this hierarchy or whole family.

And each dependent to have dependent, he becomes dependent but starts to have his own history from the point where he is not pending, to be clearer:

Meu Pai teve 5 filhos.

desses 5 filhos 1 teve 3 filhos.

e Desses 3 filhos veio mais 4 filhos.

and so on, you know? everyone can have as many children as necessary.

And this data has to be in a Mysql table of preference.

wordpress has 2 tables to do this, I think I understand the process, but I can not see a logic to solve the issue:

Table1:

CREATE TABLE `wp_terms` (
  `term_id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
  `slug` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
  `term_group` bigint(10) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;


INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES
(1, 'Sem categoria', 'sem-categoria', 0),
(2, 'opção pai primeiro', 'pai-primeiro', 0),
(3, 'opção pai segundo', 'pai segundo', 0),
(4, 'opção pai terceiro', 'pai-terceiro', 0),
(5, 'filho do pai terceiro', 'mini-turtles', 0),
(6, 'neto do pai terceiro', 'neto-pai-terceiro', 0);

and table 2:

CREATE TABLE `wp_term_taxonomy` (
  `term_taxonomy_id` bigint(20) UNSIGNED NOT NULL,
  `term_id` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
  `taxonomy` varchar(32) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
  `description` longtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `parent` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
  `count` bigint(20) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;


INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
(1, 1, 'category', '', 0, 0),
(2, 2, 'category', '', 0, 0),
(3, 3, 'category', '', 0, 0),
(4, 4, 'category', '', 0, 0),
(5, 5, 'category', '', 4, 0),
(6, 6, 'category', '', 5, 0);

Well what happens here: When we use any form for inserting values in a table saved in the two, and in the second table is the link with the first field 'Parent'.

summarizing: wanted a function that transformed the data in the table in the matrix below:

$menu  = array(
                   'casarão'=>array(
                          'mansão',
                          'cluble'
                      ), 
                   'casa'=>array(
                            'casebre'=>array(
                                        'casa da mãe jona'=>array(
                                                'casa do pai joão'=>array(
                                                        'casa do filho josé'=>array(
                                                                'casa do neto josefino'
                                                        )
                                                )
                                        )
                            )
                   ), 
                   'casinha'=>array(
                             'casarinha',
                             'casabela'
                   )
       );  
  • 4

    It looks like you need a recursive function. Can you give more details of your specific case? Do you already have the database structure? Already have some PHP code trying to deal with this kind of structure?

  • yes, I have the table

  • now I’m at work, but when I get home I’ll post the database and everything

  • but I’ll try to simplify: I have several options(parents) in a certain table and all these parent functions they can also have daughter sub-options and these daughter sub-options can also have children and so on infinitely. a scheme of categories equal to wordpress, in wordpress agent can register endless categories and sub-categories and so on.

  • You can use Phpmyadmin’s php array generator

  • and how do I do it my friend? ie where is this generator? I can implement it in a function to generate me this dynamically?

  • 1

    You have a tree, right? To rescue a tree in a query (Mysql 8) accepted this proposed solution. Behold this here also

Show 2 more comments
No answers

Browser other questions tagged

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