Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 768887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:15:00+00:00 2026-05-14T18:15:00+00:00

I’ve tried using nested sets, and they become very difficult to maintain when dealing

  • 0

I’ve tried using nested sets, and they become very difficult to maintain when dealing with multiple trees and lots of other complications.. I’d like to give PHP’s SPL library a stab at this (btw, we are PHP 5.3, MySQL 5.1).

Given two datasets:

The Groups:

+-------+--------+---------------------+---------------+
| id    | parent | Category Name       | child_key     |
+-------+--------+---------------------+---------------+
| 11133 |   7707 | Really Cool Products| 47054         |
|  7709 |   7708 | 24" Monitors        | 57910         |
|  7713 |   7710 | Hot Tubs            | 35585         |
|  7716 |   7710 | Hot Dogs            | 00395         |
| 11133 |   7707 | Really Cool Products| 66647         |
|  7715 |   7710 | Suction Cups        | 08396         |
+-------+--------+---------------------+---------------+

The Items

+------------+------------+-----------+----------+---------+
| child_key  | totalprice | totalcost | totalqty | onorder | (jan, feb, mar..)
+------------+------------+-----------+----------+---------+
| 24171      |      10.50 |     20.10 |      200 |     100 |
| 35685      |      10.50 |     20.10 |      200 |     100 |
| 76505      |      10.50 |     20.10 |      200 |     100 |
| 04365      |      10.50 |     20.10 |      200 |     100 |
| 01975      |      10.50 |     20.10 |      200 |     100 |
| 12150      |      10.50 |     20.10 |      200 |     100 |
| 40060      |      10.50 |     20.10 |      200 |     100 |
| 08396      |      10.50 |     20.10 |      200 |     100 |
+------------+------------+-----------+----------+---------+

The figures are actually much more complicated than this (I am actually aggregating a variable amount of months or years over the past 15yrs, so there may need to be 20 columns of aggregated results).

EDIT: @Gordon I suppose there are a number of ways to go about getting the resultset.. Ideally, I would like the RecursiveIterator stuff to give me something that I could spit into a view that would have all of the business logic (aggregation of leaf nodes, etc) done already.. so the result would look something like this (note that the depth in an adjacency list is arbitrary):

+------------+------------+-----------+----------+---------+
| Name       | totalprice | totalcost | totalqty | onorder | (jan, feb, mar..)
+------------+------------+-----------+----------+---------+
| Monitors   |      36.00 |     60.40 |      800 |     400 | (category)
| --24"      |      22.00 |     40.20 |      400 |     200 | (category)
| ---04365   |      10.50 |     20.10 |      200 |     100 | (item)
| ---04366   |      11.50 |     20.10 |      200 |     100 | (item)
| --22"      |      1.50  |     10.10 |      200 |     100 | (category)
| ---04365   |      1.50  |     10.10 |      200 |     100 | (item)
| -01234     |      12.50 |     10.10 |      200 |     100 | (item)
+------------+------------+-----------+----------+---------+

:END EDIT

I have been trying to figure out RecursiveIterator and IteratorAggregate, but I am having a difficult time finding real world examples that are generic enough to really wrap my head around these classes.

Can someone give me a head start?

EDIT:

A highly detailed solution is not needed here.. It just seems unclear where (if at all) I might utilize Iterator, RecursiveIterator, RecursiveIteratorIterator, etc. to come up with a clean, scalable solution to aggregate hierarchical data.

:END EDIT

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:15:00+00:00Added an answer on May 14, 2026 at 6:15 pm

    What do you really want: nicer and shorter php code or more optimized and fast requests? If your case is the latter, you should really look into nested sets as they allow general operations on a tree (select all descendants, move branch, etc.) to be done by a single SQL request.

    For concrete examples of implementation try google for “Zend_Db_NestedSet” proposal. It has support for “multi-root” trees, so you can theoretically use one table to keep generic hierarchical relations for different collections of data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 452k
  • Answers 452k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, your loop could either be how Ignacio has done… May 15, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer In Java there is no language specific way to have… May 15, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer If I got your question correctly you can check out… May 15, 2026 at 9:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.