Categories > TinyButStrong general >

Turn array into simple menu system

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Col
Date: 2009-02-08
Time: 05:45

Turn array into simple menu system

I'm trying to turn an array similar to this:
$menu = array(
   'Heading 1' => array(
      'Menu Choice 1' => 'head1choice1.php',
      'Menu Choice 2' => 'head1choice2.php',
      'Menu Choice 3' => 'head1choice3.php'
   ),
   'Heading 2' => array(
      'Menu Choice 4' => 'head2choice4.php',
      'Menu Choice 5' => 'head2choice5.php'
   ),
   'Heading 3' => array(
      'Menu Choice 6' => 'head3choice6.php'
   )
);

into this:
Heading 1                Heading 2            Heading 3
Menu Choice 1      Menu Choice 4     Menu Choice 6
Menu Choice 2      Menu Choice 5
Menu Choice 3

Each Menu Choice item will be a link to a script. By simply changing the array the menu will be updated. I have had a go at implementing this using sub-blocks, dynamic columns and groupings without success. If I need to change the format of the array above to make this work that's fine. If anybody has an ideas I'd love to hear them. Thanks.

Col
By: TomH
Date: 2009-02-08
Time: 07:39

Re: Turn array into simple menu system

Hi Col,

Can't see what you're doing, post the php file and the html template that you're using for the menu.
By: Col
Date: 2009-02-08
Time: 08:52

Re: Turn array into simple menu system

Hi Tom,

From the php array above I'd like to form this html code:
<table>
   <tr>
      <td>Heading 1</td>
      <td>Heading 2</td>
      <td>Heading 3</td>
   </tr>
   <tr>
      <td><a ref="head1choice1.php">Menu Choice 1</a></td>
      <td><a ref="head2choice4.php">Menu Choice 4</a></td>
      <td><a ref="head3choice6.php">Menu Choice 6</a></td>
   </tr>
   <tr>
      <td><a ref="head1choice2.php">Menu Choice 2</a></td>
      <td><a ref="head2choice5.php">Menu Choice 5</a></td>
   </tr>
   <tr>
      <td><a ref="head1choice3.php">Menu Choice 3</a></td>
   </tr>
</table>

How can I achieve this using TBS? Note that the format of the originating array is not set in stone; I am happy to change that. Also the above table is not set in stone either. I am happy to implement it another way as long as the general lay out is similar to outlined above. Hopefully that has shed more light on my understated original problem. If not just yell. :)

Col
By: TomH
Date: 2009-02-08
Time: 13:58

Re: Turn array into simple menu system

Col,

My apologies, when you mentioned that you 'had a go at implementing'... I assumed you had code that you tried yourself to create a solution, that's what I was referring to.

If no, then to learn, you could read and test the examples at
http://www.tinybutstrong.com/examples.php (note the box at the upper left) where you can see both the php and html source for learning how do do specific tasks.

The best way to learn TBS is to learning how those examples work by applying them to data from your own situation, then you will be far ahead of most new to TBS.

Sorry, I am too busy to write the code for you, but will be happy to help you debug it when you have some work to share.
By: Col
Date: 2009-02-09
Time: 02:55

Re: Turn array into simple menu system

No worries Tom. I actually wasn't looking for a hand out of solution code but rather a pointer. I was hoping that someone was going to tell me I wasn't fully understanding the way blocks or header groups or something else worked. I did have code from my own trial and error testing but it was a mishmash of reworked TBS code from the examples page.

Anyway I had decided to split my headings array (from original post) out into 3 separate arrays and place them each in a table which would be nested inside another table. Obviously not really the solution I was after as I couldn't just add a new heading to my heading array and have that change automatically reflected without changing the table within the html code. Through starting to do this though I actually arrived at the solution I was originally after. As is usually the way with TBS it is actually quite simple and I'm a little embarrassed that I didn't work it out while testing with the sub-block code. Anyway, for others searching this forum in future here is what I did.

The heading array is as defined above.

PHP code:
$tbs->MergeBlock('blkHeadings', $menu);
$tbs->MergeBlock('blkMenuItems', 'array', 'menu[%p1%]');

HTML code:
<table>
   <tr>
      <td valign="top"><center>[blkHeadings;block=td]
         <table>
            <tr>
               <td><strong><center>[blkHeadings.$]</center></b></td>
            </tr><tr>
               <td><center><a href="[blkMenuItems.val;block=tr;p1=[blkHeadings.$]]">[blkMenuItems.$]</a></td>
            </tr>
         </table>
      </td>
   </tr>
</table>

Hope that helps someone.
By: Skrol29
Date: 2009-02-09
Time: 15:26

Re: Turn array into simple menu system

Hi,

I would do it the same way.