Categories > TinyButStrong general >

conditional blocks

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: mielem
Date: 2003-07-15
Time: 17:21

conditional blocks

Hello,

I'd like to display a set of record changing the appearence of each one depending on a field value.
None of my test was ok.

I made something like this:

a mySQL DB with a table named "categories"

id_categoria  categoria
19            primo
20            secondo
21            terzo
22            quarto

my template was a table with 2 columns:

[blk.id_categoria]    [blk.categoria;block=row;if [blk.id_categoria]=19]
[blk.id_categoria]    [blk.categoria;block=row;if [blk.id_categoria]=19]
[blk.id_categoria]    [blk.categoria;block=row;if [blk.id_categoria]=20]
[blk.id_categoria]    [blk.categoria;block=row;else]

every raw had a different background color, to simulate a different format.

my PHP was:

$TBS = new clsTinyButStrong ;
$TBS -> LoadTemplate ("iftest.html" ) ;
$TBS -> MergeBlock ("blk" ,$cnx_id ,"SELECT * FROM categories") ;
$TBS -> Show () ;

and my result was:

19  prima
20
21
22  quarta

with the same color sequence of the template.

The raws of the table were alternated as usual and the only effect of the if statement was to hide the value of the "categoria" field.

Is there a way to display each record in a different format according to a field value?

Thanks in advance!
By: mielem
Date: 2003-07-15
Time: 19:10

Re: conditional blocks

I've found the solution by myself!
The triks is that  the block and the check must have a different scope.
In the following, the block is the whole table and the check a single row.
So, for each row, the block reoeats an entire table, but the check delete every unwanted row.

[blk.id_categoria;block=table]    [blk.categoria][tbs_check.1;block=tr;if [blk.id_categoria]=21]
[blk.id_categoria]    [blk.categoria][tbs_check.1;block=tr;if [blk.id_categoria]=20]
[blk.id_categoria]    [blk.categoria][tbs_check.1;block=tr;if [blk.id_categoria]=19]
[blk.id_categoria]    [blk.categoria][tbs_check.1;block=tr;else]

I'figuring what you can do using the operator block=begin and block=end.
Thanks, Skrol29 for a so powerful tool!
By: Skrol29
Date: 2003-07-16
Time: 10:11

Re: conditional blocks

Hello mielem,

Your solution is correction, but there is a usual smarter way.
Just define a CSS style for each category.
example: line19, line20,...
And then change the look of the row with CSS styles.
<tr class="line[blk._id_categoria]">...
By: mielem
Date: 2003-07-17
Time: 18:36

Re: conditional blocks

hello Skrol29,

Thanks for the trick, it is really effective to change the style of a row, while the other way is more effective if you want to change the whole appearence of each record (the fields shown, the layout of the "card", and so on) depending on a "category" field or on a user level.

Again, thanks for your work.
mielem

Oh, by the way, the "else" row never appears:is the example I sent (the second one) correct?
By: Skrol29
Date: 2003-07-17
Time: 19:01

Re: conditional blocks

Hello mielem,

The syntax seems correct to me.
If you have several records to display, then they will be a lot of [tbs_check.1] blocks and few chance that non records has the category 19-20 or 21.

There is 2 thinks you can do :

1/ : change [tbs_check.1;...] to
[tbs_check.[blk.rec_id];...]

or (more complicated)

2/ instead of tbs_check, make a standard block with one section for each category. And another simple block with another name but with a 'onformat' function.
In PHP, use the GetBlockSource() method to get the several sections into a php array.
Then merge the second simple block and, in the Php function linked defined by 'onformat', change the source of the block into the array item corresponding to the category.

By: mielem
Date: 2003-07-17
Time: 19:14

Re: conditional blocks

I' sure there is a record #22 because these were test values, and I only had 4 records (19-22).

I'm not sure I understood:

"There is 2 thinks you can do.." are referred to the "else" problem?
And, anyway, what the number after tbs_check represents (what happens when you change n in tbs_check.n)?
By: Skrol29
Date: 2003-07-17
Time: 19:29

Re: conditional blocks

My previous post was only about your "else" problem.
1/ and 2/ are two issues for the "else" problem.
(sorry for that)

the n in tbs_check.n is the unicity of the check process. n can be a string.

If you have
[tbs_check.x;if [a]=1]
[tbs_check.x;else]
[tbs_check.y;if [a]=2]
[tbs_check.y;else]
Then the result is gonna be at least 2 rows. One for x and one for y.

But with your template, after the blk merge, you have something more like this:
[tbs_check.x;if [a]=1]
[tbs_check.x;else]
[tbs_check.x;if [a]=2]
[tbs_check.x;else]
The result is different.