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 8679013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:51:58+00:00 2026-06-12T20:51:58+00:00

I am using this function to display UL list: function getList($arResult){ $diff = 0;

  • 0

I am using this function to display UL list:

function getList($arResult){
    $diff = 0;
    foreach($arResult as $result){
        if($lastlevel != $result[depth]){
        if($lastlevel < $result[depth]){
            $html .= "<ul>\n";
            $diff++;
        }
        else {
            $html .= "</li>\n</ul>\n</li>\n";
            $diff--;
        }       
        }
        else
        $html .= "</li>\n";

        $html .= "<li>$result[cat_name]";

        $lastlevel = $result[depth];
    }
    $html .= str_repeat("</li>\n</ul>\n", $diff);
    return $html;
    }

The $arResult contains a tree like this:

Electronics
  TV
  Video
    Old Movies
    New Movies
    Hollywood
      Old hollywood
      New HollyWood
      Rotten Hollywood
    Other Movies
  Mobile
    Nokia
    Apple
    Micromax
  Laptop
  Other

The above function prints everything under Electronics->Tv while the truth is that under Electronics there should be TV, Video, Mobile Laptop, Other ..

This function somehow is putting ul li in incorrect mode.

The Array Dump is here:

array(10) {
  [0]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "197"
    ["cat_name"]=>
    string(13) "Student Corner"
    ["cat_nicename"]=>
    string(13) "student-corder"
    ["parent"]=>
    string(1) "0"
    ["post_count"]=>
    string(1) "0"
    ["depth"]=>
    string(1) "0"
  }
  [1]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "198"
    ["cat_name"]=>
    string(6) "GujCET"
    ["cat_nicename"]=>
    string(13) "gujcet-gujrat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "0"
    ["depth"]=>
    string(1) "1"
  }
  [2]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "199"
    ["cat_name"]=>
    string(13) "Sample Papers"
    ["cat_nicename"]=>
    string(20) "sample-papers-gujcet"
    ["parent"]=>
    string(3) "198"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "2"
  }
  [3]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "200"
    ["cat_name"]=>
    string(8) "Syllabus"
    ["cat_nicename"]=>
    string(15) "syllabus-gujcet"
    ["parent"]=>
    string(3) "198"
    ["post_count"]=>
    string(1) "8"
    ["depth"]=>
    string(1) "2"
  }
  [4]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "201"
    ["cat_name"]=>
    string(4) "News"
    ["cat_nicename"]=>
    string(11) "news-gujrat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "1"
  }
  [5]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "202"
    ["cat_name"]=>
    string(13) "Question Bank"
    ["cat_nicename"]=>
    string(20) "question-bank-gujrat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "0"
    ["depth"]=>
    string(1) "1"
  }
  [6]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "203"
    ["cat_name"]=>
    string(7) "Class X"
    ["cat_nicename"]=>
    string(28) "class-x-gujrat-question-bank"
    ["parent"]=>
    string(3) "202"
    ["post_count"]=>
    string(1) "2"
    ["depth"]=>
    string(1) "2"
  }
  [7]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "204"
    ["cat_name"]=>
    string(9) "Class XII"
    ["cat_nicename"]=>
    string(30) "class-xii-gujrat-question-bank"
    ["parent"]=>
    string(3) "202"
    ["post_count"]=>
    string(1) "4"
    ["depth"]=>
    string(1) "2"
  }
  [8]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "205"
    ["cat_name"]=>
    string(7) "Results"
    ["cat_nicename"]=>
    string(15) "results-gujarat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "1"
  }
  [9]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "206"
    ["cat_name"]=>
    string(10) "About GSEB"
    ["cat_nicename"]=>
    string(10) "about-gseb"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "1"
  }
}
  • 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-12T20:51:59+00:00Added an answer on June 12, 2026 at 8:51 pm

    Way too complicated a function. Something along these lines does it:

    function treeList(array $data) {
        $list = '<ul>';
    
        foreach ($data as $item) {
            $list .= '<li>';
            $list .= $item['name'];
            if (!empty($item['children'])) {
                $list .= treeList($item['children']);
            }
            $list .= '</li>';
        }
    
        $list .= '</ul>';
    
        return $list;
    }
    
    $data = array(
        array(
            'name'     => 'Foo',
            'children' => array(
                array(
                    'name' => 'Bar'
                )
            )
        )
    );
    
    echo treeList($data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code to display a grid $('#table1 tr').bind('click', shows); function shows()
I am using this function to list items from table helps depending on parameters:
I need to write some function using ML, this function receives the list of
I am using this function try to make a request to the server. I
I am using this function (that I found on this forum) to calculate the
I am using this function to redirect to a random post. I am trying
Hi am using this function that works by a single ID ( 52000121 )
I am using this function to get the featured images: <a href=# rel=prettyPhoto> <?php
I am using this function but it is not working. I'm using StoryBoard and
I am using this function to close existing form and open a new form.

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.