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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:29:16+00:00 2026-05-14T02:29:16+00:00

How would I create a nested list, I currently have this public function getNav($cat,$subcat){

  • 0

How would I create a nested list, I currently have this

public function getNav($cat,$subcat){
    //gets all sub categories for a specific category
    if(!$this->checkValue($cat)) return false;  //checks data

    $query = false;
    if($cat=='NULL'){
        $sql = "SELECT itemID, title, parent, url, description, image 
                FROM p_cat 
                WHERE deleted = 0
                AND parent is NULL
                ORDER BY position;";
        $query = $this->db->query($sql) or die($this->db->error);
    }else{
        //die($cat);
        $sql = "SET @parent = (SELECT c.itemID FROM p_cat c WHERE url = '".$this->sql($cat)."' AND deleted = 0);

                SELECT c1.itemID, c1.title, c1.parent, c1.url, c1.description, c1.image, (SELECT c2.url FROM p_cat c2 WHERE c2.itemID = c1.parent LIMIT 1) as parentUrl
                FROM p_cat c1
                WHERE c1.deleted = 0
                AND c1.parent = @parent
                ORDER BY c1.position;";
        $query = $this->db->multi_query($sql) or die($this->db->error);
        $this->db->store_result(); $this->db->next_result();
        $query = $this->db->store_result();
    }
    return $query;
}


public function getNav($cat=false, $subcat=false){
        //gets a list of all categories form this level, if $cat is false it returns top level nav

    if($cat==false || strtolower($cat)=='all-products') $cat='NULL';
    $ds = $this->data->getNav($cat, $subcat);
    $nav = $ds ? $ds : false;
    $html = '';

    //create html
    if($nav){
        $html = '<ul>';
        //var_dump($nav->fetch_assoc());
        while($row = $nav->fetch_assoc()){
            $url = isset($row['parentUrl']) ? $row['parentUrl'].'/'.$row['url'] : $row['url'];
            $current = $subcat==$row['url'] ? ' class="current"' : '';
            $html .= '<li'.$current.'><a href="/'.$url.'/">'.$row['title'].'</a></li>';
        }
        $html .='</ul>';
    }
    return $html;
}

The sql returns parents and children, for each parent I need the child to nest in a list.

  • 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-14T02:29:17+00:00Added an answer on May 14, 2026 at 2:29 am

    It’s been a while since I’ve done any PHP but wondering if this will work. Inside your while loop, could you just recurse and add the output of getNav($cat = $row[‘url’]) again in a new <li>, kind of like:

    while($row = $nav->fetch_assoc()){
        $url = isset($row['parentUrl']) ? $row['parentUrl'].'/'.$row['url'] : $row['url'];
        $current = $subcat==$row['url'] ? ' class="current"' : '';
        $html .= '<li'.$current.'><a href="/'.$url.'/">'.$row['title'].'</a>';
        /* Add subNav HTML */
        $html .= $this->getNav($row['url']);
        $html .= '</li>';
    }
    

    It seems that “getNav” refers to both the function to get the SQL as well as the function that renders the nav HTML – maybe change the HTML getNav function name to getNavHtml just for clarity.
    Also seems like the $subcat argument may be able to be removed from the SQL getNav function since it is never used.

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

Sidebar

Related Questions

Imagine a nested list as below. [A,ABBA,ABABA] I would like to create a function
I need to create this function flat that's supposed to re-contract a new list
How would I create a nested grouping for the table below, using LINQ? I
I would like create my own collection that has all the attributes of python
Little help need with jquery sortable. I have a nested list like so: <div
Anyone know how to create a threaded / nested comment system? I would like
I'm trying to create a horizontal list from a nested list markup, as an
I normally would create a limited rights user and run the process under that
I'd like to know how one would create an application that starts in the
How does the code looks that would create an object of class: string myClass

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.