th3penguinwhisperer
th3penguinwhisperer

Reputation: 466

Using the same template for multiple sets each in its own table

I'm doing some experimenting with CakePHP and I came across the following:

Take users, each user is in a specific state (f.e. inactive, banned, new, ...). Each user has only one of these states.

Now apart from this state all users are part of the same Model (Users). So their visual representation in a template with a html table is exactly the same.

What I want to do now is putting all the users in the same state in the same table. Having a different table for each state.

It looks silly to go around and repeating the same template code for each set of users.

What's the proper way to handle this in CakePHP 3?

Upvotes: 0

Views: 59

Answers (1)

Greg Schmidt
Greg Schmidt

Reputation: 5098

If you really want a different table for each set, then an element might be the way to do. Or simply wrap the entire table in a foreach ($states as $state) in your view. Either are easily done.

But I'd suggest that perhaps what you want is really to put them all in one table with subheadings? That way, the columns will all be the same width for each state. To do that, I'd have all the users in a single query result, ordered by state (and then name, or whatever is helpful), and then as you loop through them, you check to see whether the state is different than it was for the last user, and if so output an extra line with <th colspan="x"><?= $user->state ?></th> or some such.

Upvotes: 1

Related Questions