Answer the question
In order to leave comments, you need to log in
What is the correct way to display foreach in php?
Hello. There are several strings that are converted to an array:
$name_array = explode(", ", $name);
$company_array = explode(", ", $company);
$rev_array = explode(", ", $rev);
<?php foreach ( $photo as $photos ) { ?>
<div class="swiper-slide ">
<div class="tm tm--default">
<div class="tm__author">
<div class="tm__avatar">
<img src="/upload/<?php echo html_image_src($photos, $size_preset='original', $is_add_host=false, $is_relative=true) ?>" class="js-bg">
</div>
<div class="tm__info">
<h6 class="tm__name">
<?php foreach($name_array as $names) { ?>
<?= $names; ?>
<?php } ?></h6>
<p class="tm__position">Компания</p>
</div>
</div>
<div class="tm__content">
<p class="tm__text">“Been using the theme for 4-5 years or more, should have given a review earlier. I amm not a web design pro but I know the basics.”</p>
</div>
</div>
</div>
<?php } ?>
Answer the question
In order to leave comments, you need to log in
<?php
foreach ( $photo as $idx => $photos ) { ?>
<div class="swiper-slide ">
<div class="tm tm--default">
<div class="tm__author">
<div class="tm__avatar">
<img src="/upload/<?php echo html_image_src($photos, $size_preset='original', $is_add_host=false, $is_relative=true) ?>" class="js-bg">
</div>
<div class="tm__info">
<h6 class="tm__name"><?= $name_array[$idx]; ?></h6>
<p class="tm__position"><?= $company_array[$idx]; ?></p>
</div>
</div>
<div class="tm__content">
<p class="tm__text"><?= $rev_array[$idx]; ?></p>
</div>
</div>
</div>
<?php } ?>
Remove this crap and make a normal data structure into one array, and not shove it into different arrays. You go through the foric and substitute the key.
$array = [
0 => [
'name' => 'Алексей',
'surname' => 'Петреченко',
'photo' => 'путь'
],
1 => [
'name' => 'Саша',
'surname' => 'Васильченко',
'photo' => 'путь'
]
];
foreach($array as $item) {
echo $item['name'];
echo $item['photo'];
}
"<?php echo html_image_src($photos, $size_preset='original', $is_add_host=false, $is_relative=true) ?>"
what is this? foreach
, or iterate over one of the arrays while()
or for()
and work in each array with an iterable index/key (although you can foreach
use it only with $key => $value), only you must be 146% sure that the arrays have the same number of values and corresponding keys/indexes
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question