Showing Images in gridview

ok i know the title is a bit bad, i’m sorry about that.

hello i’ve been building an app for a little project and one of the things is that it will hold a hella lot of images.

now i want to show the image itself in the gridview. currently it only shows the Image Location.

The image should land where the ‘country_flag’ name is in the db where it is stored

View:





<?php


use yii\helpers\Html;

use yii\grid\GridView;


//basic View Code

//.....


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'country_id',

            'country_name',

            'country_flag',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


</div>



I am unable to post the image of it (tho i would love to show exactly what i mean)

but the actual adress the image has in the database is:

"uploads/flags/Netherlands.png"

what is the easiest way to change it that it actually shows the image.

Thanks in advance,

Shadow

Hi, u can try to do this:


<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'country_id',

            'country_name',

         [

        'attribute' => 'country_flag',

        'format' => ['html'],

        'filter' => false,

        'value' => function ($data) {

            if (!empty($data['country_flag'])) {

                return Html::img(Yii::getAlias($data['country_flag']));

            }

            return "Not choosen";

        },

        ],


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

Worked like a charm :)

Got the image working and it looks perfect.

only thing is the size of the image isn’t scaled yet,

but i’ll figure that one out by myself.

thanks for the help

u can simpy used:


 [

        'attribute' => 'country_flag',

        'format' => ['html'],

        'filter' => false,

        'value' => function ($data) {

            if (!empty($data['country_flag'])) {

                return Html::img(Yii::getAlias($data['country_flag']), ['width' => '70px']); // options of size there

            }

            return "Not choosen";

        },

        ],

Thanks for all the help, now i just need one little bit of advice more, as you can probably guess the country flag has a second spot that it needs to be shown, in the platform view of the application. the platform has a country where it is from, and on the index it needs to show the flag;




[

                'label'=>'Country Flag',

                'attribute'=>'platform_country_id',

                'format'=>['html'],

                'filter'=>false,

                'value' => function ($data) {

                    if (!empty($data['platformCountry.country_flag'])) {

                        return Html::img(Yii::getAlias($data['platformCountry.country_flag']), ['width'=>'80']);

                    }

                    return "Not choosen";

                },

            ],



if i use this, it just shows the ‘not choosen’, but i know it is able to connect to the data if i just use;


'value' => 'platformCountry.country_flag'

With this it shows the actual db record (aka the Location)

any ideas?