Endforeach in Loops

endforeach in loops?

It's mainly so you can make start and end statements clearer when creating HTML in loops:

<table>
<? while ($record = mysql_fetch_assoc($rs)): ?>
<? if (!$record['deleted']): ?>
<tr>
<? foreach ($display_fields as $field): ?>
<td><?= $record[$field] ?></td>
<? endforeach; ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
</td>
</tr>
<? else: ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? endif; ?>
<? endwhile; ?>
</table>

versus

<table>
<? while ($record = mysql_fetch_assoc($rs)) { ?>
<? if (!$record['deleted']) { ?>
<tr>
<? foreach ($display_fields as $field) { ?>
<td><?= $record[$field] ?></td>
<? } ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action) { ?>
<option value="<?= $action ?>"><?= action ?>
<? } ?>
</td>
</tr>
<? } else { ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? } ?>
<? } ?>
</table>

Hopefully my example is sufficient to demonstrate that once you have several layers of nested loops, and the indenting is thrown off by all the PHP open/close tags and the contained HTML (and maybe you have to indent the HTML a certain way to get your page the way you want), the alternate syntax (endforeach) form can make things easier for your brain to parse. With the normal style, the closing } can be left on their own and make it hard to tell what they're actually closing.

Endforeach loop different class for first item

The most simple way to do it is to add a counter and check if it is the first value in the counter.

<?php 
$counter = 0;
foreach( $network_value as $key => $value )
{
if($counter == 0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
$counter++;
}
?>

Also I want to add that there are two ways of using foreach and if-statements, but you are trying to mix them, which is wrong.

The first method is using brackets "{" and "}":

foreach($users as $user) {
// do things for each user
echo $user->name; // Example of writing out users name
}

And if-statement:

if(true) {
// do something
}

The second method is using "foreach(statement):" and "endforeach;"

foreach($users as $user):
// do things for each user
echo $user->name; // Example of writing out users name
endforeach;

And if-statement:

if(true):
// do something
endif;

Technical difference between these two types foreach() loops in php?

They are exact same thing with two different syntax.

PHP offers an alternative syntax for some of its control structures;
namely, if, while, for, foreach, and switch. In each case, the basic
form of the alternate syntax is to change the opening brace to a colon
(:) and the closing brace to endif;, endwhile;, endfor;, endforeach;,
or endswitch;, respectively.

Reference : https://php.net/manual/en/control-structures.alternative-syntax.php

Multiple Foreach Loop Codeigniter

$tamu array variable get replaced $tamu string in first foreach so dont't do that

working code is as below:

$tamu = ['gateA'=>'test-A','gateB'=>'sdfs'];

foreach ($tamu as $tamu_item) :
echo $tamu_item->gateA;
endforeach;

var_dump($tamu);

foreach ($tamu as $tamu1) :
echo $tamu1->gateB;
endforeach;

PHP foreach alternate syntax

yes it would still work : is the same as { but you have to write endforeach at the end of your foreach it does the same thing more info check out this page
documentation

break out of if and foreach

if is not a loop structure, so you cannot "break out of it".

You can, however, break out of the foreach by simply calling break. In your example it has the desired effect:

$device = "wanted";
foreach($equipxml as $equip) {
$current_device = $equip->xpath("name");
if ( $current_device[0] == $device ) {
// found a match in the file
$nodeid = $equip->id;

// will leave the foreach loop immediately and also the if statement
break;
some_function(); // never reached!
}
another_function(); // not executed after match/break
}

Just for completeness for others who stumble upon this question looking for an answer..

break takes an optional argument, which defines how many loop structures it should break. Example:

foreach (['1','2','3'] as $a) {
echo "$a ";
foreach (['3','2','1'] as $b) {
echo "$b ";
if ($a == $b) {
break 2; // this will break both foreach loops
}
}
echo ". "; // never reached!
}
echo "!";

Resulting output:

1 3 2 1 !

PHP foreach loop skip first 3 items

For simplicity you could repeat the foreach statement but doing the opposite and continue on the first three items.

<?php foreach ($_productCollection as $_product): ?>
<?php
$count++; // Note that first iteration is $count = 1 not 0 here.
if($count <= 3) continue; // Skip the iteration unless 4th or above.
?>
<li class="category-row-list-item">
<a class="product-name" href="<?php echo $_product->getProductUrl() ?>">
<?php echo $this->htmlEscape($_product->getName()) ?>
</a>
</li>
<?php endforeach ?>

The keyword continue is used in loops to skip the current iteration without exiting the loop, in this case it makes PHP go directly back to the first line of the foreach-statement, thus increasing counter to 4 (since 4th, 5th and 6th is what we're after) before passing the if statement.

Commentary on the approach

I kept it coherent with your existing solution but a more clean way in this case would probably be to use the built in Collection Pagination.

If you use ->setPageSize(3) you can simply iterate the collection to get the first three products and then use ->setCurPage(2) to get the second page of three items.

I'm linking this blog post on the topic here just to give you an example of how it's used but since I don't know your comfort level in working with collections I retain my first answer based on your existing code.

Breaking foreach loop to the limit of three loops

This one should work for you :

  @if (count($images))
@foreach ($images as $counter => $image)
@php $counter++; @endphp
<div class="grid-item">
<a href='{{route('specImage', $image->id)}}'>
</div>
@if ($counter % 5 == 0 && $counter <= 15)
<div class="grid-item">
<div class="new-div">
</div>
@endif
@endforeach
@endif


Related Topics



Leave a reply



Submit