W
W
WestDragon2014-12-22 10:13:51
Perl
WestDragon, 2014-12-22 10:13:51

How to create a tree in perl from a SQL query?

Good afternoon. Perhaps the title is written crookedly) (A clear influence of Monday morning after a night of work).
The situation is the following. I practice working with OTRS and have an idea to connect all requests with a telephony server. It was decided to write a phone book module, but then he stumbled on a seemingly small trifle: Forming a classifier list in the form of a tree. I understand that the question will most likely need to be solved through recursion. I'm almost new to perl, so don't kick too hard.
There is the following database structure:

<TableCreate Name="phonebook_menu">
            <Column Name="id" Required="true" PrimaryKey="true" AutoIncrement="true" Type="BIGINT"/>
            <Column Name="id_parent" Required="false" Type="BIGINT"/>
            <Column Name="name" Required="true" Size="250" Type="VARCHAR"/>
            <Column Name="created" Required="true" Type="DATE"/>
            <Column Name="created_by" Required="true" Type="INTEGER"/>
            <Column Name="changed" Required="true" Type="DATE"/>
            <Column Name="changed_by" Required="true" Type="INTEGER"/>
            <Column Name="valid_id" Required="true" Type="SMALLINT"/>
            <Unique Name="phonebook_menu_id">
                <UniqueColumn Name="id"/>
            </Unique>
            <Index Name="phonebook_menu_id">
                <IndexColumn Name="id"/>
            </Index>
            <ForeignKey ForeignTable="valid">
                <Reference Local="valid_id" Foreign="id"/>
            </ForeignKey>
            <ForeignKey ForeignTable="users">
                <Reference Local="created_by" Foreign="id"/>
                <Reference Local="changed_by" Foreign="id"/>
            </ForeignKey>
        </TableCreate>

  • id - everyone understands that this is a unique classifier number;
  • id_parent - if this is a top-level classifier (category), then it will be = 0, or if it is a nested classifier, then id corresponding to another classifier, name - classifier name;
  • valid_id - classifier hidden or not (0/1);
  • the rest does not affect the task in any way;

It is required to write an sql query for mysql or postgresql and a code that will process the query results and generate hashes of hashes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Falseclock, 2015-01-01
@Falseclock

The question is not correct. It's not clear exactly what you're after.
On Postgres, you can pull it out as a tree via XML
www.postgresql.org/docs/9.4/static/functions-xml.h...
and then parse as you like.

W
WestDragon, 2015-01-02
@WestDragon

The following is pursued: It is
necessary to get hashes of hashes in order to process it later and display the list of categories on the page in the form of a ul>li list. And then use the www.jstree.com/docs/json plugin .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question