How to limit container value?

I have a loop and index but an issue arise when index is a value 3. It should stay as a constant 2 within a loop. As our container involves values for each day, we need index. An example within HTML:

<div class='container".($index+1)."'>

Index starts at 0 till 10 but class should be kept as value 2 and limit index (1) that we get
<div class='container2'>

Is it technically possible to stay at 2 after index increases to 2 as class value should not reach 3:

<div class='container3'>

It works if index has value 1 but when index is 2 it will collapse HTML as we need to stay at number 2:

<div class='container2'>

Your explanation lost me, but if I understand you correctly, it sounds like you need a ternary operator:.

(not tested as my php is REALLY rusty but general idea)

<div class='container".($index > 1 ? 2 : $index+1)."'>

Alternatively:

<div class='container".min(2, $index+1)."'>

Edit: used min rather than max as @m_hutley pointed out

1 Like

Like I said, rusty. @rpkamp’s version is cleaner and easier to read….

except you dont want max. you want min.

max(2,3) = 3. min(2,3) = 2.

There’s a word for this logical mindscrew (“I want the maximum value to be X”, “The function you want is the minimum.”), but i dont know it.

EDIT: The word my brain is probably trying to dig up is “counterintuitive”, but it is also trying to tell me there’s an actual name for things like this.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.