我们如何插入“lft”和“正确”我们从其他来源接收的数据的嵌套集模型的值?

发布于 2024-10-03 07:56:45 字数 407 浏览 0 评论 0原文

我必须为我的 Web 应用程序管理一些分层数据。数据由四种类型的消息组成。我决定使用嵌套列表模型,因为数据中可以有任意数量的子节点。我需要存储的数据来自外部源,如何将值添加到“lft”和“lft”中? 'rgt' 字段将其插入到 mySQL 中。
已编辑 这就是我的应用程序使用curl 从 Twitter 获取的消息(时间线)的显示方式。

foreach ($xml->entry as $status) {
       echo'<li>'.$status->content.'</li>';
   }

现在,尽管直接打印,我仍然需要将“$status->content”的内容存储到数据库中,以便我可以在父子层次结构中操作这些内容,即打印。

I have to manage some hierarchical data for my web application. The data consist of messages of four types. I decided to use nested-list model because there can be arbitrary number of child nodes in the data. The data that I need to store is coming from an external source, how do I add values to the 'lft' & 'rgt' fields for it to be inserted into mySQL.
EDITED
This is how I display messages(timeline) that my application fetches from Twitter using curl.

foreach ($xml->entry as $status) {
       echo'<li>'.$status->content.'</li>';
   }

Now in spite of direct printing, I need to store the contents of '$status->content' into database so that I can manipulate those i.e print in a parent-child hierachy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-10-10 07:56:45

一种可能性是编写一个递归函数或一个过程来遍历要导入的数据集。

一切都取决于其他来源的结构。如果它是一个经典的父 id 层次结构,那么在 Java 世界中您可以本着以下精神做一些事情:

public class Importer {

    public static db = new Database();

    public static void processOtherSource(ArrayList<Object> data, int lft) {
        int siblings = 0;
        for (Object element : data) {
            int children = 0;
            if (element.getClass().getName().equals("ArrayList")) {
                children = Importer.processOtherSource(element, lft+1, rgt);
            }
            int rgt = lft + children * 2 + 1;
            Importer.db.insert(element, lft, rgt);
            siblings++;
        }
        return siblings;
    }

    public void main(String[] args) {
        Importer.processOtherSource(dataToImport, 1);
    }

}

One possibility is to write a recursive function or a procedure that goes through the data set to be imported.

Everything depends on the structure of the other source. If it's a classical parent id hierarchy, then in the Java world you could do something in the spirit of:

public class Importer {

    public static db = new Database();

    public static void processOtherSource(ArrayList<Object> data, int lft) {
        int siblings = 0;
        for (Object element : data) {
            int children = 0;
            if (element.getClass().getName().equals("ArrayList")) {
                children = Importer.processOtherSource(element, lft+1, rgt);
            }
            int rgt = lft + children * 2 + 1;
            Importer.db.insert(element, lft, rgt);
            siblings++;
        }
        return siblings;
    }

    public void main(String[] args) {
        Importer.processOtherSource(dataToImport, 1);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文