Categories > TinyButStrong general >

Main loop with 2 or more internal loops

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: SoftExpert
Date: 2006-08-21
Time: 14:50

Main loop with 2 or more internal loops

Hi,
I have this problem that I can't seem to solve now:
I must display a list of users and their rights as follows:

-----------------------------+
user_name                    |
----------+--------+---------+
          | groups | modules |
          +--------+---------+
          | group1 | moduleX |
          | group2 | moduleY |
          | ....   | ......  |
          | groupN |         |
          +--------+---------+
bla bla                      |
-----------------------------+

-----------------------------+
user_name                    |
----------+--------+---------+
          | groups | modules |
          +--------+---------+
          | group1 | moduleX |
          | group2 | moduleY |
          | ....   | ......  |
          | groupN |         |
          +--------+---------+
bla bla                      |
-----------------------------+

So there is a certain number of users, they belong to a
certain number of groups.
Also, they can override access to the modules by specifying
a correspondence user - module.

How can I setup the data structure, knowing that I have
a main loop an 2 internal loops, independent of each other?

Thank you for your replies
By: Skrol29
Date: 2006-08-21
Time: 22:54

Re: Main loop with 2 or more internal loops

If you're talking about the tables structure then I think the solution is this to use two link tables :

t_user_group
-  user_id
-  group_id

t_user_module
- user_id
- module_id
By: SoftExpert
Date: 2006-08-22
Time: 00:20

Re: Main loop with 2 or more internal loops

Thanks for the reply.
The situation is this:

<TABLE>
<TR>
<TD>
user 1
    <ul>loop1a</ul>  <ul>loop1b</ul>
</TD>
</TR>
<TR>
<TD>
user 2
    <ul>loop2a</ul>  <ul>loop2b</ul>
</TD>
</TR>
etc
</TABLE>

User should display some user info in a table
Loop1 is around an <li> tag in a separate <ul> object
Loop2 is around an <li> tag in a separate <ul> object

I still don't get it right: how should I define my data structure,
how I should call the template and what to put in the template.

Thank you!
By: Skrol29
Date: 2006-08-22
Time: 01:49

Re: Main loop with 2 or more internal loops

For the data structure, I suggest the solution above. This is a classic way to perform a n-to-n relation.

Since you have your solution for the data structure. You can merge them with TBS using a normal block for User records and two subblocks, one for Groups records and another for Module records.
Example:
<tr>
  <td>

     [user.user_name;block=tr]

     Groups:
     <ul>
       <li>[grp.grp_name;block=li;bmagnet=ul;p1=[user.user_id]]</li>
     </ul>

     Modules:
     <ul>
       <li>[mod.mod_name;block=li;bmagnet=ul;p1=[user.user_id]]</li>
     </ul>

  </td>
</tr>

And at the PHP side, it could be:
// main block
$TBS->MergeBlock('user','mysql','SELECT user_id, user_name FROM t_user');

// subblock Groups
$TBS->MergeBlock('grp','mysql','SELECT grp_name FROM t_group AS g INNER JOIN t_user_group AS l ON (g.grp_id=l.grp_id) WHERE (user_id=%p1%)');

// subblock for Modules
$TBS->MergeBlock('mod','mysql','SELECT mod_name FROM t_module AS m INNER JOIN t_user_module AS l ON (m.mod_id=l.mod_id) WHERE (user_id=%p1%)');

By: SoftExpert
Date: 2006-08-22
Time: 11:41

Re: Main loop with 2 or more internal loops

Sorry to bother you again, but this issue seems to be as stubborn as me ;-)
My data has to come in this format
Array
(
    [0] => Array
        (
            [id_user] => 1
            [u_username] => admin
            [u_fname] => Admin
            [u_lname] => ADMIN
            [groups] => Array
                (
                    [0] => Array
                        (
                            [id_user] => 1
                            [id_group] => 1
                            [grp_name] => g_admins
                            [has_rights] => t
                        )

                    [1] => Array
                        (
                            [id_user] => 1
                            [id_group] => 2
                            [grp_name] => g_users
                            [has_rights] => t
                        )

                    [2] => Array
                        (
                            [id_user] => 1
                            [id_group] => 3
                            [grp_name] => g_special
                            [has_rights] => t
                        )

                )

            [modules] => Array
                (
                    [0] => Array
                        (
                            [id_user] => 1
                            [id_module] => 1
                            [mod_name] => m_admin
                            [has_rights] => t
                        )

                    [1] => Array
                        (
                            [id_user] => 1
                            [id_module] => 2
                            [mod_name] => m_module
                            [has_rights] => t
                        )

                )

        )

    [1] => Array
        (
            [id_user] => 2
            [u_username] => username
            [u_fname] => User
            [u_lname] => USER
            [groups] => Array
                (
                    [0] => Array
                        (
                            [id_user] => 2
                            [id_group] => 1
                            [grp_name] => g_admins
                            [has_rights] => f
                        )

                    [1] => Array
                        (
                            [id_user] => 2
                            [id_group] => 2
                            [grp_name] => g_users
                            [has_rights] => t
                        )

                    [2] => Array
                        (
                            [id_user] => 2
                            [id_group] => 3
                            [grp_name] => g_special
                            [has_rights] => f
                        )

                )

            [modules] => Array
                (
                    [0] => Array
                        (
                            [id_user] => 2
                            [id_module] => 1
                            [mod_name] => m_admin
                            [has_rights] => f
                        )

                    [1] => Array
                        (
                            [id_user] => 2
                            [id_module] => 2
                            [mod_name] => m_module
                            [has_rights] => f
                        )

                )

        )

)

How should I write my MergeBlock calls?
By: TomH
Date: 2006-08-22
Time: 13:25

Re: Main loop with 2 or more internal loops

We cannot see what you are doing that is not working...
please, post the code (both the PHP and the HTML template code) thatg you are using and any output that you are getting.
By: Skrol29
Date: 2006-08-22
Time: 13:49

Re: Main loop with 2 or more internal loops

Ok, so you have the data structure :)
With your PHP arrays, the merging should be:

HTML :
<tr>
  <td>

     [user.u_username;block=tr]

     Groups:
     <ul>
       <li>[grp.grp_name;block=li;bmagnet=ul;p1=[user.$]]</li>
     </ul>

     Modules:
     <ul>
       <li>[mod.mod_name;block=li;bmagnet=ul;p1=[user.$]]</li>
     </ul>

  </td>
</tr>
PHP :
Assuming that $users is a global variable containg your php array.
// main block
$TBS->MergeBlock('user',$users);

// subblock Groups
$TBS->MergeBlock('grp','array','users[%p1%][groups]');

// subblock for Modules
$TBS->MergeBlock('mod','array','users[%p1%][modules]');

By: SoftExpert
Date: 2006-08-23
Time: 10:17

Re: Main loop with 2 or more internal loops

That's it! Exactly what I needed.
Thank you very much for your help (and sorry for tanking your time)!