I have a foreach loop in php.
When the loop is greater than 2 items I would like to display some text instead of the loop. Is this possible?
For example: A loop of 2 or less items shows= item 1, item2
The loop of more items shows the text = Mulitple items.
The example code for indication:
$count++;
foreach($attValConfig as $attValConfigSingle) {
if ($attValConfigSingle["frontend_label"] == "LABELTEXT") {
echo ('<div class="attributes_row">Text</div>');
foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {
if ($count++ > 2) { echo 'SomeNewText'; }
else echo "<option>"list of items"</option>";
I think you want to break
in your if statement:
if ($count++ > 2) {
echo 'SomeNewText';
break;
}
Just declare a variable in which you store your text built during the foreach loops. When you're out of it, print this string if count is less than 3, your other text otherwise.
You can count
the array before looping:
if(count($attValConfigSingle['values']) > 2) {
// More than 2 items
echo "Lots of things";
} else {
// 2 items or less
foreach($attValConfigSingle['values'] as $value) {
// ...
}
}
Maybe I didn't understand you correctly. If you want the text:
item 1, item 2, other items...
Then you need to use break
to break out of your loop:
foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {
if ($count++ > 2) {
echo 'SomeNewText';
break;
} else {
echo "<option>"list of items"</option>";
}
}