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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:27:23+00:00 2026-06-06T22:27:23+00:00

this is my database structure for my terms table (categories): id | name |

  • 0

this is my database structure for my terms table (categories):

   id   |   name   |   parent_id   |   level   |   position   
--------------------------------------------------------------
   1    |   term 1 |   NULL        |   0       |   1
   2    |   term 2 |   1           |   1       |   1
   3    |   term 3 |   1           |   1       |   2
   4    |   term 4 |   NULL        |   0       |   2
   5    |   term 5 |   4           |   1       |   1

so terms 2 and 3 are 1st level children of 1 and 5 is a first lst level child of 4

this is my query: (and it’s not correct, this should be fixed)

SELECT
    `id`,
    `name`
FROM
    `terms`
ORDER BY
    `position` ASC,
    `level` ASC

this is my php:

$terms = array();

// query part

if(!$this->_db->resultRows > 0)
  return $terms;

while($d = $this->_db->fetch($r))
{
  $terms[$d->id] = new Term($d->id);
}     

return $terms;

current result:

term 1
  term 2
  term 5
term 4
 term 3

but the result should be:

term 1
  term 2
  term 3
term 4
  term 5

I don’t know how to alter the query to get the correct result
the goal is to output this in a (multiple) select box
I know how to do it with nested lists, but you can’t nest a select

  • 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-06T22:27:24+00:00Added an answer on June 6, 2026 at 10:27 pm

    Okay, so there are many ways to deal with this type of problem. All the ways that I will present do not require there to be a level column in the table. In my opinion that is redundant data since it can be deduced from the information found in the other columns.

    If you know the maximum “level” will only be 1 (maximum depth is 2) you can use a query like this:

    SELECT
            t.`id`,
            t.`name`,
            IF(p.`position` IS NULL, t.`position`*{$row_count}, p.`position`*{$row_count}+t.`position`) AS display_order
    FROM
            `terms` t
            LEFT JOIN `terms` p ON p.`id` = t.`parent_id`
    ORDER BY
            display_order
    

    where $row_count is calculated:

    SELECT COUNT(*) FROM `terms`
    

    There are ways to modify this SQL to make it work with more levels (depth), but the query needs to get bigger with each maximum level/depth it will support.

    If you are uncertain about the number of levels you will have, then you should probably just do something like this:

    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $sth = $dbh->prepare('SELECT id,name FROM terms WHERE parent_id IS NULL ORDER BY position');
    $sth->execute(array(NULL));
    $terms = $sth->fetchAll();
    
    $sql = 'SELECT id,name FROM terms WHERE parent_id = ? ORDER BY position';
    $terms_to_check = $terms;
    $terms = array();
    while(count($terms_to_check))
    {
            $k = array_shift($terms_to_check);
            $terms[] = $k;
            $sth = $dbh->prepare($sql);
            $sth->execute(array($k['id']));
            $results = $sth->fetchAll();
            $terms_to_check = array_merge($results, $terms_to_check);
    }
    

    (By the way I recommend using PDO.)

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

Sidebar

Related Questions

I have this database structure, SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `announces` (
My database structure looks something like this: Person Id Name FieldA FieldB Phone Id
I have created a table on an Oracle 10g database with this structure :
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
This is my Database structure, One-to-One mapping in MySQL: This is my java file:
enter code here My problem is this: in this database the junction table contains
During my work on this database application, I've apparently managed to corrupt a form
How do I import this database correctly? https://github.com/samanz/cakecart Every time I import then I
I have a production database where usage statistics reside. This database is responsible for
Here's a SQL query that I slightly scrubbed (just the column/table/database names). I kept

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.