Y
Y
Yuri Strelkov2017-08-21 23:12:58
PHP
Yuri Strelkov, 2017-08-21 23:12:58

How to display elements in different div classes?

There is a layout in which div classes change, inside you need to display the title of the element, the announcement text and the link.
So, everything is displayed, it seems to be as it should, but instead of 4 elements, 1 is displayed everywhere
I can’t figure out how to do it right

news.list component template
<?php
$caritem = array ("car__item_left", "car__item_left-bottom", "car__item_middle", "car__item_middle-bottom");
?>
  <div class="car__items">	
  <?foreach($caritem as $key => $value):?>
   <?foreach ($arResult['ITEMS'] as $key => $arItem):?>
    <div class="car__item <?echo "$value "?>">
    <div class="car__button"></div>			
      <div class="car__hover car__hover_mobile">			
        <h3 class="car__title"><?echo $arItem["NAME"]?></h3>			
        <div class="car__text">
          <p><?echo $arItem["PREVIEW_TEXT"];?></p>
        </div>
 <a class="link link_car" href="<?echo $arItem["DETAIL_PAGE_URL"]?>">Подробнее</a>
      </div>			
    </div>		
  <?endforeach;?><?endforeach;?>
  </div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Luchnikov, 2017-08-21
@lu4nik

1. In the component settings, did you look at how many elements are displayed on the page?
2. Do not use the same key in the inner foreach as the first foreach. You use $key in both cases.

N
novrm, 2017-08-23
@novrm

Why not rewrite the code like this:

<?php $caritem = ["car__item_left", "car__item_left-bottom", "car__item_middle", "car__item_middle-bottom"]; ?>
<div class="car__items">	
    <?php foreach($caritem as $key1 => $value): ?>
        <?php foreach ($arResult['ITEMS'] as $key2 => $arItem): ?>
        <div class="<?= 'car__item' . $value ?>">
            <div class="car__button"></div>
            <div class="car__hover car__hover_mobile">
                <h3 class="car__title"><?= $arItem['NAME'] ?></h3>
                <div class="car__text">
                    <p><?= $arItem['PREVIEW_TEXT'] ?></p>
                </div>
                <a class="link link_car" href="<?= $arItem['DETAIL_PAGE_URL']?>">Подробнее</a>
            </div>
        </div>
        <?php endforeach ?>
    <?php endforeach ?>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question