J
J
Jorupbek2017-02-25 13:27:31
MySQL
Jorupbek, 2017-02-25 13:27:31

How to display database values ​​(dropdowlist) in text format on the website page?

The page site should display text values ​​and not the values ​​from the database
For example , the site should display the value Lieutenant instead of '1'
<?php
$items = [
'Rank' => [
'0' => 'Jr. Lieutenant',
'1' => 'Lieutenant',
'2' => 'Senior Lieutenant',
'3' => 'Captain',
'4' => 'Major',
'5' => 'Polkovnik',
'6' = > 'Colonel',
],
];
$params = [
'prompt' => 'Select a status...',
];
echo $form->field($model, 'hit')->dropDownList($items,$params);
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-02-25
@slo_nik

Good afternoon.
It looks like it should be like this:
If you get only an integer from the base and want to match it with text, then create a public static method in the model that will return an array. Pretty much the same as you did.

// В моделе
public static function getItems()
{
return [
'0' => 'мл.Лейтенант',
'1' => 'Лейтенант',
'2' => 'ст.Лейтенант',
'3' => 'Капитан',
'4' => 'Майор',
'5' => 'п.Полковник',
'6' => 'Полковник',
];
// В представлении
$form->field($model, 'hit')->dropDownList(ModelName::getItems(), ['prompt' => 'Select'])

J
Jorupbek, 2017-02-26
@Jorupbek

slo_nik :

<?php
                    
                    $items = [
                    'Звание' => [
                    '0' => 'мл.Лейтенант',
                    '1' => 'Лейтенант',
                    '2' => 'ст.Лейтенант',
                    '3' => 'Капитан',
                    '4' => 'Майор',
                    '5' => 'п.Полковник',
                    '6' => 'Полковник',
                    ],
                    ];
                    $params = [
                    'prompt' => 'Выберите статус...',
                    ];
                    echo $form->field($model, 'hit')->dropDownList($items,$params);
                    
                    ?>
                    <!-- Выпадающий список-->

<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            // 'id',
            // 'category_id',
             [
            'attribute' => 'category_id',
            'value' => function ($data) {
                    return $data->category->name;
            },
            ],
            'name',
            'date',
            'content:html',
            'dolzhnost',
            'datestart',
            'dateend',
            // 'price',
            'keywords:html',
            'description:html',
            [
                'attribute' => 'image',
                'value' => "<img src='{$img->getUrl()} '>",
                'format' => 'html',
            ],
            'hit:html',  //modules/admin/views/product/view.php 54 строка
            'year',
        ],
    ]) ?>

<p><b>Звание: </b><?= $product->hit?></p>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question