How to foreach this data

Published 1 month ago by AliTalib

The extraction values are separate and non-duplicated

this my database table https://i.imgur.com/QxnS67l.png

When it is extracted it is like this

https://i.imgur.com/QF1PrIk.png

I want to be like that

https://i.imgur.com/lDbz9sl.png

Please apply to this example

@foreach($genres as $genre)

<a href="{{ url('cinema/movies/genre/'.$genre->genre) }}" class="item">{{ $genre->genre }}</a>

@endforeach 
Best Answer (As Selected By AliTalib)
AliTalib
                        @foreach($genres as $genre)

                            <?php $splitGenres = explode(",", $genre->genre);?>

                            @for ($i = 0; $i < count($splitGenres); $i++)

                                <?php $thisGenre = trim($splitGenres[$i], " "); ?>

                                @if (!in_array($thisGenre, $trackedGenres))

                                    <?php $trackedGenres[] = $thisGenre; ?>

                                    <a href="{{ url('cinema/movies/genre/'.$thisGenre) }}" class="item">{{ $thisGenre }}</a>

                                @endif

                            @endforeach 

                        @endforeach
Leandro_Haruki

So basically you need to list all existent movie genres. The best practice i was taught is that for types/genres/etc you create a new table for it, so it can be accessed and incremented dynamically.

AliTalib

@Leandro_Haruki

I asked this question because I have other tables similar to this table, but I want to learn the idea until I modify those tables

Leandro_Haruki

Well, i guess you should use a PHP function called explode() then.

http://php.net/manual/en/function.explode.php

AliTalib

@Leandro_Haruki

Can you give me an example using Laravel

Leandro_Haruki
@foreach($genres as $genre)
    @foreach(explode($genre) as $genreName)
        <a href="{{ url('cinema/movies/genre/'$genreName) }}" class="item">{{$genreName}}</a>
    @endforeach 
@endforeach 
AliTalib
cmdobueno
@foreach($genres as $genre)
    @foreach(explode($genre,',') as $genreName)
        <a href="{{ url('cinema/movies/genre/'. trim($genreName)) }}" class="item">{{trim($genreName)}}</a>
    @endforeach 
@endforeach 

He forgot the delimiter (in this case a comma)

I additionally added trim, because the names are probably like this:

name 1, name 2, name 3

Trim will remove the leading and ending spaces so as to not break your urls.

AliTalib
                        @foreach($genres as $genre)

                            <?php $splitGenres = explode(",", $genre->genre);?>

                            @for ($i = 0; $i < count($splitGenres); $i++)

                                <?php $thisGenre = trim($splitGenres[$i], " "); ?>

                                @if (!in_array($thisGenre, $trackedGenres))

                                    <?php $trackedGenres[] = $thisGenre; ?>

                                    <a href="{{ url('cinema/movies/genre/'.$thisGenre) }}" class="item">{{ $thisGenre }}</a>

                                @endif

                            @endforeach 

                        @endforeach
AliTalib
wilk_randall

Use a pivot table, it would make your life a lot easier...

cmdobueno

Additionally, logic you are doing should not be done in a blade... generally speaking that is wrong... so I will be nice and give you a real answer... its not for you, its for somebody that may someday use this to solve their problem, and I dont want them doing it how you are doing it... pivot is the best answer, but you dont have that so here is how things should go

Your function in your controller

public function view(){
    $unfiltered_genres = []; //You have to fill in where your genres come from... I have no idea....

    $genres = [];

    foreach( $unfiltered_genres as $genre ){

        foreach( explode($genre,',') as $g ){
            $genres[] = trim($g);
        }

    }


    return view('path-to-your-view')
        ->withGenres($genres); //Add whatever else you need
}

Now in your blade it would look like this:

@foreach($genres as $genre)
    <a href="{{ url('cinema/movies/genre/'.$thisGenre) }}" class="item">{{ $thisGenre }}</a>
@endforeach 

This method is done because generally speaking the only purpose of a blade is display your already processed data.

Now please understand there is a much better way to get this all working. I have no idea the context of genres, nor where they come from. This is the proper way, and it also allows for easier maintaining of the code at later times.

erikverbeek

You could clean this up by using Laravel Collections. You'd go through 3 steps:

  • Map explode(', ', $genre) onto every genre string to break them up into arrays. Notice the extra space I added after the comma. This way you don't have to trim the left over spaces.
  • Flatten the separate arrays into a single array.
  • Filter out any duplicate values with unique()
$genres = collect($genres)->map(function($genre) {
    return explode(', ', $genre);
})->flatten()->unique();

These kinds of data manipulations are better placed inside a Controller. That gives a much cleaner blade file:

@foreach($genres as $genre)
    <a href="{{ url("cinema/movies/genre/$genre") }}" class="item">{{ $genre }}</a>
@endforeach 
cmdobueno

^^^ This right here should be the correct answer.... just saying.

Leandro_Haruki

@Leandro_Haruki thanks

You're welcome.

Indeed, should use @erikverbeek 's answer and put it in your controller as @cmdobueno said.

AliTalib

thanks @Leandro_Haruki @erikverbeek @cmdobueno

Your comments improved code and made work faster

Please sign in or create an account to participate in this conversation.