A
A
Alexey2021-07-06 08:01:08
PHP
Alexey, 2021-07-06 08:01:08

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


When I try to display this whole thing on the site, the following happens: the photos are displayed perfectly, but the problem with the name looks like this:

60e3e2f02db89051243362.jpeg

Here is my code that displays all this:

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


What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nokimaro, 2021-07-06
@urinov

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

N
Nord Dev, 2021-07-06
@Nordic_Alf

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'];
}

J
John Didact, 2021-07-06
@JohnDidact

"<?php echo html_image_src($photos, $size_preset='original', $is_add_host=false, $is_relative=true) ?>"
what is this?
Well, what did you want? You are in the photo cycle, in each iteration, iterate over and display all the names from the name_array.
Either fill the array with data that belongs to each other and iterate over it foreach, or iterate over one of the arrays while()or for()and work in each array with an iterable index/key (although you can foreachuse 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 question

Ask a Question

731 491 924 answers to any question