Add comma between foreach looped items but not at end - php

I'd like to achieve on adding a comma between my authors in my foreach but each time I make any references, it adds a comma at the end also.
Here is what I have below:
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> -
<?php foreach ($recipe->get_authors() as $profile): ?>
<?= $profile->get_name() ?>,
<?php endforeach; ?>
<p><?= $recipe->get_content() ?></p>
</div>
Which gives me the following results:
How can I prevent a comma at the end?

Since you were skeptical about implode, here's a simple example (just showing the loop):
<?php foreach ($recipe->get_authors() as $profile):
$links[] = '' . $profile->get_name() . '';
endforeach; ?>
<?= implode(', ', $links); ?>

An alternative using join:
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> - <?= join(", ", array_map(function ($profile) { return sprintf('%s', $profile->get_url(), $profile->get_name()); }, $recipe->get_authors())) ?>
<p><?= $recipe->get_content() ?></p>
</div>

Wasn't able to test it but basically store results to $authors first, and in this way you can count it without recalling the method get_authors. While $cnt keeps track base 1 and can be compared directly on the length.
<?php
$authors = $recipe->get_authors();
$cnt = 0;
?>
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> -
<?php foreach ($authors as $profile) { $cnt++; ?>
<?= $profile->get_name() ?><?php
if ($cnt < count($authors)) echo ",";
?>
<?php } ?>
<p><?= $recipe->get_content() ?></p>
</div>

Related

If count equals number, append class

So I have the variable $count that will give me a count. What would be the best approach to append a class name to the <div class="column"> section?
Here is sort of what I'm looking for:
If count === 1, render <div class="column">
If count === 2, render <div class="column is-half">
If count === 3, render <div class="column is-one-third">
If count === 4+, render <div class="column is-one-quarter">
Here is the code:
<?php $count = count($sub_items) ?>
<div class="column">
<h1 class="title is-6 is-mega-menu-title mb-2"><?= $sub_item->get_title(); ?></h1>
<?php if ($sub_child_items = $sub_item->get_sub_items()): ?>
<?php foreach ($sub_child_items as $sub_child_item): ?>
<a class="navbar-item" href="<?= $sub_child_item->get_url(); ?>">
<?= $sub_child_item->get_title(); ?>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
All help would be appreciated!
I'll do something like this using switch and please note the last case default:
<?php
$count = count($sub_items);
$className = "column ";
switch($count) {
case 2:
$className .= "is-half";
break;
case 3:
$className .= "is-one-third";
break;
case 1:
default:
$className = ($count >= 4) ? "column is-one-quarter" : "column";
}
?>
<div class="<?php echo $className; ?>">
<h1 class="title is-6 is-mega-menu-title mb-2"><?= $sub_item->get_title(); ?></h1>
<?php if ($sub_child_items = $sub_item->get_sub_items()): ?>
<?php foreach ($sub_child_items as $sub_child_item): ?>
<a class="navbar-item" href="<?= $sub_child_item->get_url(); ?>">
<?= $sub_child_item->get_title(); ?>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
Also, I would suggest to use <?php echo $varName; ?> instead of using <?= $varName; ?> for compatibility purposes.

How to convert foreach loop to while loop?

I am currently using a foreach loop to display the data from my database on my website but have just tried to include pagination which is using a while loop.
<?php
foreach ($posts as $post): ?>
<div class="post clearfix">
<img src="<?php echo BASE_URL . '/assets/images/' . $post['image']; ?>" alt="" class="post-image">
<div class="post-preview">
<h2><?php echo $post['title']; ?></h2>
<i class="far fa-user"> <?php echo $post['username']; ?></i>
<i class="far fa-calendar"> <?php echo date('F j, Y', strtotime($post['created_at'])); ?></i>
<p class="preview-text">
<?php echo html_entity_decode(substr($post['excerpt'], 0, 150) . '...'); ?>
</p>
Read More
</div>
</div>
<?php endforeach; ?>
while ($row = mysqli_fetch_array($result)) {
}
?>
How do I go about transferring the data here/incorporating them both together into the while loop so that the pagination works smoothly and the data remains displayed as it currently is? Thanks
To convert foreach to while loop. Simply,
Note: if "$posts" is an object array
try this below
<?php
$i=0;
while($i < sizeof($posts)){
$post=$posts[$i++]; ?>
//use $post object in your code
//something like $post['image']
<?php }?>

I want to apply css class as per below condition

I want to apply css class as per below condition but after applying this code each content is repeated by 8 times and if i complete for loop before div then css classes are not getting applied.
<?php $size=count($Spacategories);
for($i=0;$i<8;$i++){
if($i==0 || $i==5){ ?>
<li class="valign">
<?php } elseif($i==1 || $i==7) { ?>
<li class="lalign">
<?php } elseif($i==2 || $i==6) { ?>
<li class="talign">
<?php } else { ?>
<li class="ralign">
<?php } ?>
<div class="image">
<img src="<?php echo $spacategory['thumb']; ?>" title="<?php echo $spacategory['name']; ?>" alt="<?php echo $spacategory['name']; ?>" />
</div>
<div class="text">
<h3><?php echo $spacategory['name']; ?></h3>
<p><?php echo substr($spacategory['description'],0,150); ?></p>
<?php echo $text_view_more; ?>
</div>
</li>
<?php } } ?>
Change all instances of
$spacategory
to
$Spacategories[$i]
This will fix it.
Also, I don't see what's populating $text_view_more. You should probably change that to some data point from $Spacategories?
Finally, it seems you need to be looping based on the number of categories, so change
for($i=0;$i<8;$i++){
to
for($i=0;$i<$size;$i++){
In your code, $spacategory is not an item from $spacategories with the index $i in each iteration. You should have started the for:
$spacategory = $spacategories[$i];
or use a foreach instead:
<?php
$classesIndexMap = array(
0 => 'valign',
1 => 'lalign',
2 => 'talign',
5 => 'valign',
6 => 'talign',
7 => 'lalign',
);
$html_view_more = htmlspecialchars($text_view_more, ENT_NOQUOTES, 'UTF-8');
?>
<?php foreach($spacategories as $i => $spacategory) : ?>
<li class="<?= isset($spacategory[$i]) ? $spacategory[$i] : 'ralign' ?>">
<div class="image">
<img src="<?= htmlspecialchars($spacategory['thumb'], ENT_QUOTES, 'UTF-8') ?>"
title="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>"
alt="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>" />
</div>
<div class="text">
<h3><?= htmlspecialchars($spacategory['name'], ENT_NOQUOTES, 'UTF-8') ?></h3>
<p><?= substr($spacategory['description'],0,150); ?></p>
<a href="<?= htmlspecialchars($spacategory['href'], ENT_QUOTES, 'UTF-8'); ?>"
class="learmmore"><?= $html_view_more ?></a>
</div>
</li>
<?php endforeach; ?>

Nested foreach array loop in PHP

I have for each loop return an array
<?php foreach( get_uf_repeater( 'clients' ) as $document_files ): extract( $document_files ) ?>
<div class="ui-grid-a my-breakpoint">
<div class="ui-block-a"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
<div class="ui-block-b"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
</div>
<?php endforeach ?>
how can i jump to the next array for the second line "" instead of displaying the same of the first one, I assume I need nested loop but didnt know how do it exactly.
You could use array_chunk
<?php foreach (array_chunk(get_uf_repeater( 'clients' ), 2, true) as $array) { ?>
<div class="ui-grid-a my-breakpoint">
<?php foreach($array as $document_files) { ?>
<?php extract( $document_files ); ?>
<div class="ui-block-a"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
<?php } ?>
</div>
<?php } ?>

PHP while loop, display in pairs of two

In Wordpress, I am creating a slider to slide through some content that the user specify. I am using ACF (advanced custom fields) if anyone is familiar with that, however what I am trying to accomplish is to show two content items at a time, while sliding through the jQuery slider.
Here is my loop:
<?php while(has_sub_field('popular_topic')): ?>
<li>
<div class="slide">
<img src="<?php the_sub_field('popular_topic_image'); ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
echo '<p>'.$newContent.'</p>';
?>
<a class="more-arrow" href="<?php the_sub_field('popular_topic_link'); ?>">Read More</a>
</div>
</div>
</li>
<?php endwhile; ?>
This is currently working, however it only shows one slide. I want it to show two slides and a time. Is there something I can do with a counter? Does this make sense?
my syntax might be a little off but maybe give this a shot?
<?php $i = 0; ?>
<?php while(has_sub_field('popular_topic')): ?>
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
$contentVar[$i] = array (
'img' => the_sub_field('popular_topic_image'),
'title' => $newContent,
'link' => the_sub_field('popular_topic_link')
);
$i++;
?>
<?php endwhile; ?>
<?php $j = 0; ?>
<?php while ($j < $i) : ?>
<li>
<div class="slide">
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php $j++; ?>
<?php if ($j <= $i) : ?>
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php endif; ?>
</div>
</li>
<?php $j++; ?>
<?php endwhile; ?>

Categories