5

I want to make something like this:

  • A1
    • B1
      • C1
        • D1
        • D2
        • D3
      • C2
      • C3
    • B2
      • C4
        • D10
        • D11
        • D12
      • C5
      • C6
    • B3
      • C7
      • C8
      • C9
        • D25
        • D26
        • D27

So it's always groups of three, with every level ascending by a letter. First level is A, second B, C, D, E and so forth. The numbers are also listed in ascending order. Level A can only reach 1, B has 3, C has 9, D has 27, and so forth.

This is really easy to generate manually, converting the letters to their ASCII equivalent, adding one and converting them to character equivalent again. Problem is, I have to loop it till S, for instance, and my mind is getting messier and messier trying to put loops within loops.

What I got (toLetter and toNumber literally does what they do):

  echo "<ul><li>";
    echo "A1";
     echo "<ul><li>";
      $b = toNumber(A);
      $b++;
      $b = toLetter($b);
      $bnum = 1 - 1;
      $bnum = $bnum * 3;
      $bnum++;

      echo $b;
      echo $bnum."</li>";
      $bnum++;
      echo "<li>".$b;
      echo $bnum."</li>";
      $bnum++;
      echo "<li>".$b;
      echo $bnum."</li>";

Making this:

  • A1
    • B1
    • B2
    • B3

I really can't figure out how to just loop everything so it can reach till Z.

4
  • 4
    $bnum = 1 - 1; what's this? Commented May 30, 2013 at 7:46
  • The first 1 should be the number of the listed item above (A1). So basically, what it does is subtract 1 from the listed item, multiply 3, then again add one to determine its own number (B1). Use it to (C6) for example to get (D16). Commented May 30, 2013 at 7:49
  • It does return 1, which is what I aimed for. Commented May 30, 2013 at 7:52
  • For example, we have B2 instead of A1. The right listed items under it should be C4, C5, and C6. So we take 2 from B2. 2-1=1 1*3= 3 3+1=4. That's 4 from C4. Commented May 30, 2013 at 7:58

2 Answers 2

3

A pretty simple version which only goes up to the 'C' level; increase as necessary.

<?php

function outputListItems($char) {
    static $index = array('A' => 1);

    if (!isset($index[$char])) {
        $index[$char] = 1;
    }

    for ($i = 0; $i < 3; $i++) {
        echo '<li>';
        echo $char;
        echo $index[$char]++;

        if ($char < 'C') {
            echo '<ul>';
            $nextChar = $char;
            outputListItems(++$nextChar);
            echo '</ul>';
        }

        echo '</li>';
    }
}

?>

<ul>
    <li>
        A1
        <ul><?php outputListItems('B'); ?></ul>
    </li>
</ul>

A is treated as special chase, since it only has one entry.

Sign up to request clarification or add additional context in comments.

Comments

1

in PHP, you can use ++ on a string, so you don't need to Letter/toNumber And to support unlimited nesting, you need a recursion (or at least it will be easier for you)

3 Comments

Recursion looks new to me, as I practically W3schooled myself in PHP. Where might I find more on this topic?
Oh god, leave w3schools it's bad. but I think this is a good explanation devzone.zend.com/283/recursion-in-php-tapping-unharnessed-power
This is one mean epic site. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.