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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:45:05+00:00 2026-05-31T01:45:05+00:00

Hold fire if you are confused by the question a second on the question

  • 0

Hold fire if you are confused by the question a second on the question because I’m not sure that it exactly makes sense.

SELECT * FROM `rules` ORDER BY `Rank_1` , `Rank_2` , `Rank_3` , `Rank_4` ASC

Yields:

http://img814.imageshack.us/img814/8396/climsyclipboardeh.png

Copy pasta:

0   0   0   0   0
1   0   0   0   1
1   1   0   0   1.1
1   1   1   0   1.1.1 
2   0   0   0   2
4   0   0   0   4
4   1   0   0   4.1
4   1   1   0   4.1.1 
4   1   1   9   4.1.1.9
4   1   1   10  4.1.1.10 
4   1   1   11  4.1.1.11

However this data is not quite in a form I need it to be in order to do something useful with it.

I want to loop through all the rules and depending on how ‘deep’ I am, do different things. For example I’d like to take RuleID = 0 and do <h1>, 1.1 have <h2> but when it comes to 4.1.x, open up a <ul> and give each rule an <li>.

For this I figure the best way is to select the data like an array where I’d end up with:

array( 4 =>
    array( 4.1 =>
        array( 4.1.1 => 'rule content')
    )
);

Then I could realise my depth and open up a <ul> tag, loop through printing out the rules at that depth etc.

What is really the best way to tackle this? I’ve been at it for ages and don’t have a clue where to go from here to be honest. I really want the data to be in the same table. I figure I could probably solve this if they were all in different tables but I don’t know that for sure it would be any easier.

I started down this route:

foreach($m as $rule) {
    $depth = count(explode('.', $rule['ruleid']));
    switch($depth) {
        case 1:
            echo '<h1>'.$rule['content'].'</h1>';
            break;
        case 2:
            echo '<h2>'.$rule['content'].'</h2>';
            break;
        case 3:
            echo '<strong>'.$rule['content'].'</strong>';
            break;
    }
    echo '<br />\n';
}

Then I realised this is just going to deal with each rule entry individually, whereas my solution needs some sort of ‘awareness’ of where it is in the rules, so it can know when to open a tag (such as a <ul>) and then close it again when it’s done echoing list items that might be present in a rule (such as "Don't do: <ul><li>this</li><li>or this</li></ul>")

Here’s an example of desired output from the table of data above:

0. Introduction

…

4. Chat

4.1. Do’s and do not’s

4.1.1. Do:

  • 4.1.1.9 Be polite
  • 4.1.1.10 Be patient
  • 4.1.1.11 Be smart

Hope some bright spark can help!

  • 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-31T01:45:06+00:00Added an answer on May 31, 2026 at 1:45 am

    Suppose, from your input data, you can produce an array like this:

    $data = array(
      '0'        => 'content 0',
      '1'        => 'content 1',
      '1.1'      => 'content 1.1',
      '1.1.1'    => 'content 1.1.1',
      '2'        => 'content 2',
      '4'        => 'content 4',
      '4.1'      => 'content 4.1',
      '4.1.1'    => 'content 4.1.1',
      '4.1.1.9'  => 'content 4.1.1.9',
      '4.1.1.10' => 'content 4.1.1.10',
      '4.1.1.11' => 'content 4.1.1.11',
    );
    

    which you could then transform to a tree-like structure with this:

    // this will be our root node
    // it has no content, but a place for children
    $struct = array(
      'children' => array()
    );
    
    foreach ($data as $ruleID => $content) {
      //                 /\
      //                 ||
      //     for every rule id in the input data we start at
      //     the root
      //          ||
      //          \/
      $parent =& $struct;
    
      // we split $ruleID at the dot to get a path, which we traverse
      //            ||
      //            \/
      foreach (explode('.', $ruleID) as $val) {
    
        // for every entry in the path we
        // check if it's available in the current parent
        //    ||
        //    \/
        if (!isset($parent['children'][$val])) {
          // if not, we create an empty entry
          $parent['children'][$val] = array(
            'content' => '',       // no content
            'children' => array()  // no children
          );
        }
    
        //                         /\
        //                         ||
        // and then use either the new, or allready existent parent
        // as the new parent
        //      ||
        //      ||  mind the assignment by reference! this
        //      \/  is the most important part
        $parent =& $parent['children'][$val];
      }
    
      // after the whole path is traversed, we can
      // finally add our content
      //                    ||
      //                    \/
      $parent['content'] = $content;
    }
    
    print_r($struct);
    

    demo: http://codepad.org/vtMPjGoa

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

Sidebar

Related Questions

basically i want to hold a parameter that retrieve value from $.post() call like
I have a Button that when I press it, it does not fire the
I want to disable the recent apps intent that is fired from the icon
Since type variables cannot hold poly-types, it seems that with Rank*Types we cannot re-use
I need to hold my every view in one layout, so that i can
I hate the JavaBeans pattern with a passion that burns like the fire of
How can i get hold of the instantiating object from a constructor in java?
Possible Duplicate: Way to make a UIButton continuously fire during a press-and-hold situation? i
Is it possible to hold an open TCP connection with a client, while the
If you press and hold a key in X11 while AutoRepeat is enabled, you

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.