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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:54:35+00:00 2026-06-15T15:54:35+00:00

How do I arrange <ul> & <li> for recursive PHP function? I made as

  • 0

How do I arrange <ul> & <li> for recursive PHP function? I made as follows, but does not work properly :

<?php
echo '<ul>';
$sql = mysql_query("select * from categories where parent_id = 0");
while($rs=mysql_fetch_array($sql)){
   echo '<li>'.$rs['name'];
   echo show_subcategory(($rs['cat_id']));
   echo '</li>';
}
echo '</ul>';

function show_subcategory($category_id){
$sql = mysql_query("select * from categories where parent_id ='$category_id'");
if(mysql_num_rows()>0){
echo '<ul>';
   while($rs=mysql_fetch_array($sql)){
     echo  '<li>'.$rs['name'];
     echo show_subcategory(($rs['cat_id']));
     echo '</ul></li>';
   }
 }else{
  echo '</li>';
 }
}
?>

CREATE TABLE IF NOT EXISTS categories (
  cat_id int(11) NOT NULL AUTO_INCREMENT,
  parent_id int(11) NOT NULL DEFAULT '0',
  name varchar(20) NOT NULL,
  description text NOT NULL,
  slug varchar(50) NOT NULL,
  PRIMARY KEY (cat_id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


INSERT INTO categories (cat_id, parent_id, name, description, slug) VALUES
(1, 0, 'programming', 'Forum ', 'programming'),
(2, 1, 'php', 'php programming', 'php'),
(3, 1, 'python', '', ''),
(4, 1, 'java', '', ''),
(5, 1, 'visual basic', '', ''),
(6, 2, 'codeigniter', '', ''),
(7, 2, 'joomla', '', '');    
  • 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-15T15:54:36+00:00Added an answer on June 15, 2026 at 3:54 pm

    Since it’s a recursive function that’s supposed to output each category and a list of all sub-categories, you could move the “top-level” parent-category display into it as well (and start it by calling show_subcategory(0);.

    After that, we can remove the sub-closing </ul> and </li> tags and it would give us something similar to:

    function show_subcategory($category_id){
        $sql = mysql_query("select * from categories where parent_id ='$category_id'");
        if(mysql_num_rows()>0){
            echo '<ul>';
            while($rs = mysql_fetch_array($sql)){
                echo  '<li>' . $rs['name'];
                echo show_subcategory(($rs['cat_id']));
                echo '</li>';
            }
            echo '</ul>';
        }
    }
    
    show_subcategory(0);
    

    Side-note, not answer specific:
    I would recommend upgrading to the MySQLi or PDO extensions. Both support prepared statements which offer security against SQL-Injection attacks (which your current code is vulnerable to).

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

Sidebar

Related Questions

I have these rewriteRules which work fine: # 4 params RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2&what=$3&id=$4 [NC,L,QSA]
When I define my test as follows it works. TEST(MyService, WhenCalled_DoesTheRightThingTM) { // Arrange
I'm not sure if the title reflects what I'm asking here but thats best
i have this php code who auto thumb and arrange my photos to gallery:
So I'm generating an html table through my php to arrange my content. Unfortunately,
I need your opinion... I am developing an application in PHP & MYSQL (AZURE
please help me arrange these 3 buttons horizontally like this photoshopped image: Please take
how could I arrange the following div's by the value in the attribute amount
How do I arrange (in terms of depth) a view's subview to be in
What is a good way to arrange images in the footer as shown in

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.