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

  • Home
  • SEARCH
  • 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 8736611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:20:38+00:00 2026-06-13T10:20:38+00:00

Please provide a simple solution to the following problem in concise PHP type code

  • 0

Please provide a simple solution to the following problem in concise PHP type code (just needs to loosely follow PHP type syntax):

We want to be able to output a nested list of items – items in the list can be sub-items of other items in the list. A simple SQL table would have the following fields:

  1. itemID – int.
  2. item – varchar.
  3. subItemOfID – int.

A small subset of data would look something like this:

itemID  item    subItemOfID
1   Item1   0
2   Item2   0
3   Item3   0
4   Item10  1
5   Item11  1
6   Item12  1
7   Item100 4
8   Item101 4
9   Item102 4
10  Item30  3
11  Item31  3
12  Item32  3

The output would need to look something like this (nested lists sorted by item):

•   Item1
    o       Item10
           Item100
           Item101
           Item102
    o       Item11
    o       Item12
•   Item2
•   Item3
    o       Item30
    o       Item31
    o       Item32

I would get this if the SubItemOfID row was in descending order but the previous examples I used had a regex in it on the item which I was always told this was a bad way of doing something with tables because varchars can change. I need help! This is what I have so far:

<?php
$table = array (
'itemID' => array( 
                    1,2,3,4,5,6,7,8,9,10,11,12
                ),
'item' => array(
                    'Item1','Item2','Item3','Item10','Item11','Item12','Item100','Item101','Item102','Item30','Item31','Item32'
                ),
'subItemOfID' => array(
                    0,0,0,1,1,1,4,4,4,3,3,3
                )
                );

foreach($table as $header => $column) {
    foreach($column as $data) {
        if($header == 'item') {
            $fixed = intval(str_replace('Item','',$data));      
            $str_len = strlen($fixed);
            echo '<ul>';
            if($str_len === 1) {
                echo '<li>'.$data.'</li>';
            }
            echo '<ul>';
            if($str_len === 2  ) {
                echo '<li>'.$data.'</li>';              
            }
            echo'<ul>';
            if($str_len ===3) {
                echo '<li>'.$data.'</li>';
            }
            echo '</ul>';
            echo '</ul>';
            echo '</ul>';       
        }
    }
}
?>
enter code here
  • 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-06-13T10:20:40+00:00Added an answer on June 13, 2026 at 10:20 am

    With this query you can fetch up to 6 lvls of categories:

    $sql = "SELECT c1.item AS item,
                  IF(c6.item IS NOT NULL, 6,
                      IF(c5.item IS NOT NULL, 5,
                         IF(c4.item IS NOT NULL, 4,
                            IF(c3.item IS NOT NULL, 3,
                               IF(c2.item IS NOT NULL, 2, 1))))) AS lvl,
                  if (c6.item IS NOT NULL, CONCAT(c6.itemId, c5.itemId, c4.itemId, c3.itemId, c2.itemId, c1.itemId),
                      IF(c5.item IS NOT NULL, CONCAT(c5.itemId, c4.itemId, c3.itemId, c2.itemId, c1.itemId),
                         IF(c4.item IS NOT NULL, CONCAT(c4.itemId, c3.itemId, c2.itemId, c1.itemId),
                            IF(c3.item IS NOT NULL, CONCAT(c3.itemId, c2.itemId, c1.itemId),
                               IF(c2.item IS NOT NULL, CONCAT(c2.itemId, c1.itemId), c1.itemId))))) AS code,
           FROM categories c1
           LEFT JOIN categories c2 ON c2.itemId = c1.subItemOfID
           LEFT JOIN categories c3 ON c3.itemId = c2.subItemOfID
           LEFT JOIN categories c4 ON c4.itemId = c3.subItemOfID
           LEFT JOIN categories c5 ON c5.itemId = c4.subItemOfID
           LEFT JOIN categories c6 ON c6.itemId = c5.subItemOfID
           GROUP BY c1.itemId
           ORDER BY code";
    $q = mysql_query($sql);
    while (false !== ($row = mysql_fetch_assoc($q))) {
        echo str_repeat('---', $row['lvl'])
           . $row['title']
           . '<br />';
    }
    

    What does it do: selects every category and its parent categories (if exist), then generate code field to sort them.

    Sorry, I inattentively read the question.

    Here is the solution in php:

    $table = array (
        'itemID' => array(
            1,2,3,4,5,6,7,8,9,10,11,12
        ),
        'item' => array(
            'Item1','Item2','Item3','Item10','Item11','Item12','Item100','Item101','Item102','Item30','Item31','Item32'
        ),
        'subItemOfID' => array(
            0,0,0,1,1,1,4,4,4,3,3,3
        )
    );
    outputChildrenRecursive(0, $table);
    
    /**
     * Outputs child items recursively
     *
     * @param integer $parentItemID Parent item itemID
     * @param array   $table        Table data
     */
    function outputChildrenRecursive($parentItemID, array $table)
    {
        // Get children
        $children = array();
        foreach ($table['itemID'] as $key => $value) {
            if ($table['subItemOfID'][$key] == $parentItemID) {
                $children[] = $key;
            }
        }
    
        // Output children and grandchildren
        if (!empty($children)) {
            echo '<ul>';
            foreach ($children as $childKey) {
                echo '<li>';
                echo $table['item'][$childKey];
                // Prevent infinite recursion if itemID equals subItemOfID
                if ($table['itemID'][$childKey] != $table['subItemOfID'][$childKey]) {
                    outputChildrenRecursive($table['itemID'][$childKey], $table);
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please provide a simple example of how to consume a REST service
I'm new to this kabeja package so please can some one provide code example
Maybe this is a silly problem with a very simple solution but still is
I have the following simple script to test the mkdir() function in PHP: <?php
PROBLEM: Ok, I've been TRYING to follow the sample code on the MySQL Forge
I need a solution for the following problem: My project has two jars in
Please provide a good, thorough tutorial about Java class loading , focusing on how
Please provide the diff b/w ref and out parameters in C#
please provide me the possibility of my issue. Can we customize the push notification
Can someone please provide an example of how to store, and read xml data

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.