endforeach in loops? - php

I use brackets when using foreach loops. What is endforeach for?

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.

It's the end statement for the alternative syntax:
foreach ($foo as $bar) :
...
endforeach;
Useful to make code more readable if you're breaking out of PHP:
<?php foreach ($foo as $bar) : ?>
<div ...>
...
</div>
<?php endforeach; ?>

as an alternative syntax you can write foreach loops like so
foreach($arr as $item):
//do stuff
endforeach;
This type of syntax is typically used when php is being used as a templating language as such
<?php foreach($arr as $item):?>
<!--do stuff -->
<?php endforeach; ?>

It's just a different syntax. Instead of
foreach ($a as $v) {
# ...
}
You could write this:
foreach ($a as $v):
# ...
endforeach;
They will function exactly the same; it's just a matter of style. (Personally I have never seen anyone use the second form.)

How about this?
<ul>
<?php while ($items = array_pop($lists)) { ?>
<ul>
<?php foreach ($items as $item) { ?>
<li><?= $item ?></li>
<?php
}//foreach
}//while ?>
We can still use the more widely-used braces and, at the same time, increase readability.

Using foreach: ... endforeach; does not only make things readable, it also makes least load for memory as introduced in PHP docs
So for big apps, receiving many users this would be the best solution

How about that?
<?php
while($items = array_pop($lists)){
echo "<ul>";
foreach($items as $item){
echo "<li>$item</li>";
}
echo "</ul>";
}
?>

Related

How can I make one column per foreach using bootstap?

For a web dev projet for my school, I've to program a Trello-like. I've to display board, with different columns but I really don't know how to generate one column per element in my foreach loop.
If someone could help me I'd be thanksfull!
Here's the foreach loop:
<?php foreach ($currentColumns as $currentColumn): ?>
(<?= $currentColumn['title']?> ) <?= $currentColumn['position'] ?>
<?php endforeach; ?>
here, you can do like this..
<table>
<tr>
<?php foreach ($currentColumns as $currentColumn): ?>
<td>
(<?= $currentColumn['title']?> ) <?= $currentColumn['position'] ?>
</td>
<?php endforeach; ?>
</tr>
</table>

PhpStorm: How to separate PHP & HTML indentation

Is it possible to configure PhpStorm 8 to indent HTML and PHP code separately?
I'll copy the examples from this question: How to properly indent PHP/HTML mixed code?
How PhpStorm formats the code currently:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
How I want it to look like:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
No, not currently possible. See this comment

Nesting Foreach loop inside Foreach loop using short_open_tags

I have the following foreach loop:
<?php
$fields = CFS()->get('list-item-field');
?>
<? foreach ($fields as $field) : ?>
<?= $field['list-item-title'] ?>
<? endforeach ?>
And I would like to add another foreach inside the loop, like so:
<?php
$fields = CFS()->get('item-field');
?>
<? foreach ($fields as $field) : ?>
<?= $field['list-item-title'] ?>
<?php
$values = CFS()->get('color');
?>
<? foreach ($values as $value => $label) : ?>
<? echo $value ; ?>
<? endforeach ?>
<? endforeach ?>
However this doesn't work, and I get the error:
Invalid Argument Supplied For Foreach()
Alright I needed to expirement a bit but I figured it out, I doubt this will be helpful to many but regardless here's what I needed to do:
<?php
$fields = CFS()->get('item-field');
?>
<? foreach ($fields as $field) : ?>
<?= $field['list-item-title'] ?>
<? foreach ($field['color'] as $colors => $label) :?>
<? echo $colors ; ?>
<? endforeach ?>
<? endforeach ?>
This post helped: http://customfieldsuite.com/forums/questions/925/loop-within-a-loop

php foreach. select specific value from array and use it as parameter for display loop

I want to be able to display titles of the books of the author who is currently logged in. I'm using PHP session
<? foreach ($books as $book ): ?>
<? foreach ($book as $selbook => $author): ?>
<option value="$selbook>"$author['author'] == $_SESSION["sess_username"] ? ' selected="selected"' : ''?>> $author?></option>
<li class="active"><a href=""><span class="pull-right"><input id="button" type="submit" name="submitr" value="Edit"></span><i class="icon-fire$
<? echo htmlspecialchars($book['Title'], ENT_QUOTES, 'UTF-8'); ?> <strong> - </strong><em>
<? echo htmlspecialchars($book['author'], ENT_QUOTES, 'UTF-8');?></em></a></li>
<? endforeach; ?>
<? endforeach; ?>
You start 2nd foreach with this:
<?php foreach ($book as $selbook => $author) { ?>
And end with this:
<?php endforeach; ?>
It is not correct. Use this:
<?php foreach ($book as $selbook => $author): ?>
// some code
<?php endforeach; ?>
Also, why u use <?php? Short tag is not enabled? Use <? it is much faster to write and code is more readable. Also, when you want to echo some variable, using php tag use this:
<?=$variable;?>
Much faster and more readable too.
Update
Try this:
<? foreach ($books as $book ): ?>
<? foreach ($book as $selbook => $author): ?>
<option value="<?=$selbook;?>" <? if($author['author'] == $_SESSION["sess_username"]): ?>selected="selected"<? endif; ?> ><?=$author;?></option>
<li class="active"><a href=""><span class="pull-right"><input id="button" type="submit" name="submitr" value="Edit"></span><i class="icon-fire$
<? echo htmlspecialchars($book['Title'], ENT_QUOTES, 'UTF-8'); ?> <strong> - </strong><em>
<? echo htmlspecialchars($book['author'], ENT_QUOTES, 'UTF-8');?></em></a></li>
<? endforeach; ?>
<? endforeach; ?>

php string operation

i have the following code in html
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
and that will result
as test1,test2,test3,
how avoid that last comma in simple method . I cant use complicated code in html like
<? $i = 0 ;?>
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
<? $i++ ;?>
<? if($i != count($tst)) :?>
,
<?endif;?>
<? endforeach;?>
Please Help :)
Use implode on an interim array:
<?php
$a= array();
foreach($tst as $test) {
$a[]= $test->id;
}
echo(implode(', ', $a));
?>

Categories