Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Yablochnyi's avatar

Why is the cycle duplicated?

Good afternoon! I got an old project from the archive, I did not write it. Locally I ran it, when choosing a category of posts, they are duplicated. Please advise how to fix this problem. A project was written on lumen, there is not enough information on the Internet that could help me with my poor knowledge.

There is a form in which I select post categories

<form class="leftPanelMenu" data-form="listProjects" data-action="{{route('front_postGetProjects')}}"
                  autosend>
                @foreach ([
               'all' => 'all',
               1 => 'home',
               4 => 'house',
               2 => 'ecommerce',
                ] as $val => $name)
                    <label class="lpm-custField">
                        <input type="radio" name="type" value="{{$val}}" {{$val == 'all' ? 'checked' : ''}}>
                        <span><span class="hidden">{{$name}}</span><a>{{$name}}</a></span>
                    </label>
                @endforeach
            </form>

in controller

public function postGetProjects(Request $request) {

      $type = $request->input('type');
      $projects = AdmService::getProjects(['type' => $type]);
      $grid = self::grid($projects, true);
      $prGrid = $grid[0];
      $f = $grid[1];

      $list = self::grid($projects, true, true);
      $prList = $list[0];
      $fM = $list[1];

      return $this->rqSuccess($this->render('front.block.listCardsGrid', [
        'prGrid' => $prGrid,
        'full' => $f,
        'prList' => $prList,
        'fullMob' => $fM
      ]));
    }

in class AdmService

public static function getProjects($filter = []) {
    $projects = \DB::table('project')->select(\DB::raw('project.*'))->where('project.active', '=', 1)->orderBy('order');


    foreach ($filter as $n=>$f) {
      switch ($n) {
        case 'id':
          $projects = $projects->where('project.id', '=', $f);
        break;
        case 'url':
          $projects = $projects->where('project.url', '=', $f);
        break;
        case 'other':
          $projects = $projects->where('project.id', '!=', $f)->whereIn('project.type', [1,2]);
        break;
        case 'type':
          if ($f != 'all')
            $projects = $projects->where('project.type', '=', $f);
        break;
      }
    }

    $projects = $projects->get()->toArray();

    foreach ($projects as $k=>$p) {
      $p->text = json_decode($p->text, true);
      $p->props = json_decode($p->props);
      $p->authors = json_decode($p->authors, true);

      $size = getimagesize(base_path().'/public/img/content/'.$p->preview);
      $p->orient = $size[0] >= $size[1] ? 'h' : 'v';

      foreach ($p->text as $kb=>$b) {
        if (isset($b['imgs']) && (!isset($b['difference']) || !$b['difference'])) {
          $imgs = [];
          $b['imgs'] = is_array($b['imgs']) ? $b['imgs'] : [$b['imgs']];
          foreach ($b['imgs'] as $img) {
            try {
              $size = getimagesize(base_path().'/public/img/content/'.$img);
              $imgs[$img] = [$size[0], $size[1]];
            }
            catch (\Exception $e) {
              $check = false;
            }
          }
          $imgs = self::lineImgs($imgs);
          $p->text[$kb]['imgs'] = $imgs;
        }
      }

      $projects[$k] = $p;
    }

    if (isset($filter['id']) || isset($filter['url'])) {
      if (count($projects))
        return $projects[0];
      else
        return null;
    }

    return $projects;
  }

code in front.block.listCardsGrid

<div class="listCards listCardsGrid">
  @foreach (['l', 'r'] as $side)
    <div class="{{$side}}">
      @foreach ($prGrid[$side] as $p)
        @if ($p != 'fb')
          <a href="{{route('front_project', ['name' => $p->url])}}" class="card {{$p->orient == 'v' ? 'card-big' : ''}}"
             data-type="{{$p->type}}">
            <img data-src="/img/content/{{$p->preview}}" alt="{{$p->name}}" title="{{$p->name}}"/>
            <div class="info">
              <div class="name"><h2>{{$p->name}}</h2></div>
              <div class="accent">{{isset($p->props->typeHome) ? $p->props->typeHome : ''}}<br>{{$p->props->area}}м<sup>2</sup>
              </div>
              <div class="status">{{$projectStatus[$p->status]}}</div>
            </div>
          </a>
        @else
          <a href="{{$urlFeedback}}" class="card card-link">
            <h2 class="h1">news</h2>
          </a>
        @endif
      @endforeach
    </div>
  @endforeach
  @if ($full)
    <a href="#" class="card card-link card-full" data-modal-open="modal-feedback">
      <h2 class="h1">news</h2>
    </a>
  @endif
</div>

the first category is displayed perfectly, but if you click on the categories, then the block with the listCardsGrid class is added to the markup, although there should be only one.

0 likes
0 replies

Please or to participate in this conversation.