php string operation - php

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));
?>

Related

Remove comma on last item of foreach

I have next code for WordPress loop of tags for single post:
<?php if ($tags) : foreach($tags as $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>,
<?php endforeach; endif; ?>
I have comma added to last anchor. There is also white space after comma.
How can I remove the comma from last anchor while I am doing this with foreach() PHP loop?
Thanks for ideas and help!
Check if your loop is working on the last one:
<?php if ($tags) : ?>
<?php $count = count($tags); ?>
<?php foreach($tags as $i => $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php if ($i < $count - 1) echo ", "; ?>
<?php endforeach; ?>
<?php endif; ?>
What has a higher cost, calling a function or setting a variable? Here's another way to do it perhaps, which sets a variable and removes the offending chars at the end - no extra math or if checks needed.
<?php
$tagoutput = '';
if ($tags) {
foreach ($tags as $tag)
$tagoutput .= '' . $tag->name . ', ';
$tagoutput = rtrim($tagoutput, ', ');
}
echo $tagoutput;
?>
You can do it the other way around (remove it from the first one). If your array is numeric you can try something like this:
<?php if ($tags): ?>
<?php foreach ($tags as $key => $tag): ?>
<?php if ($key > 0): ?>,<?php endif ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php endforeach ?>
<?php endif ?>
You can also try it using counter.
$values = array('value','value','value');
$count = count($values);
$i = 0;
foreach($values as $value){
$i++;
echo $value;
if($count > $i){
echo ', ';
}
}
Output: value, value, value

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; ?>

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

endforeach in loops?

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>";
}
?>

Categories