douji1877 2018-12-16 09:45
浏览 21

如何构建并打印到浏览器类别树?

So I want to build a category script for products.

How I can build the category tree?

I have a DB like this...

CREATE TABLE categories (
    cat_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    parent_id integer NOT NULL,
    cat_name CHAR(24) NULL DEFAULT NULL,
    cat_position tinyint NOT NULL,
    icon TEXT NULL DEFAULT NULL,
    keywords text NOT NULL, //for search
    active boolean NULL DEFAULT true,
    cat_description CHAR(128) NULL DEFAULT NULL
);

But I can't understand how it works because 1 product may be found in many categories...

I am coding on GoLang

Now I do like this...

rows, err := db.Query("SELECT cat_id, parent_id, cat_name FROM categories WHERE active = true ORDER BY Parent_id ASC")
if err != nil {
    http.Error(w, http.StatusText(500), 500)
    return
}
defer rows.Close()

Then I put rows into template...

{{range .}}
<p><a href="/show?getinfo={{ .Cat_id}}">{{ .Cat_id}}</a> - {{ .Parent_id}} - {{ .Cat_name}} <a href="/show?getinfo={{ .Cat_id}}">Показать</a>
{{end}}

In the end I would like to get something similar to this

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?