lovelilili 2019-07-15 18:40:23 2268次浏览 0条回复 1 0 0

数据库vod表中有两个字段:

vod_play:

qq$$$iqiyi$$$sohu$$$youku$$$mgtv

vod_url:

1$http://v.qq.com/x/cover/llws99784lrtpz7/g003189nkf3.html
2$http://v.qq.com/x/cover/llws99784lrtpz7/f0031cijuxc.html
$$$1$http://www.iqiyi.com/v_19rse7ldt0.html
2$http://www.iqiyi.com/v_19rse7mbz0.html
$$$1$http://tv.sohu.com/v/MjAxOTA2MjUvbjYwMDczMDI0MS5zaHRtbA==.html
2$http://tv.sohu.com/v/MjAxOTA2MjUvbjYwMDczMDI0My5zaHRtbA==.html
$$$1$http://v.youku.com/v_show/id_XNDI0Mjg3NDcyOA==.html
2$http://v.youku.com/v_show/id_XNDI0Mjg3NDczMg==.html
$$$1$http://www.mgtv.com/b/330256/5878154.html
2$http://www.mgtv.com/b/330256/5878408.html

我想在表单中分组显示,qq对应qq的播放地址,iqiyi对应iqiyi的播放地址。。。: QQ截图20190715183259.png

目前实现的方法如下:

一,在model类中新建个方法分割字符串重新组成数组;

public function getPlaylist(){
$datas['vod_play'] =  explode('$$$', $this->vod_play);
$datas['url'] = explode('$$$', $this->vod_url);
$playlist=[];
if (is_array($datas)) {
    foreach ($datas as $key => $value) {
        foreach ($value as $k => $v) {
            $playlist[$k][$key] = $datas[$key][$k];
        }
    }
    return $playlist;
}
}

二,在控制器中将数组传给视图:

public function actionUpdate($id)
{
$model = $this->findModel($id);
$playlists= $model->getPlaylist();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
    return $this->redirect(['view', 'id' => $model->vod_id]);
}

return $this->render('update', [
    'model' => $model,
    'playlists'=>$playlists,
]);
}

三,在视图中循环这个数组:

<?php foreach ($playlists as $playlist):?>
<div class="form-group field-player">
<label class="control-label" for="player">播放器</label>
<input type="text" id="player" class="form-control" name="player[]" value='<?php echo $playlist['vod_play'];?>' maxlength="30" aria-invalid="false">
<div class="help-block"></div>
</div>
<div class="form-group field-url">
<label class="control-label" for="url">播放地址</label>
<textarea id="ffvod-vod_content" class="form-control" name="url[]" rows="12" aria-invalid="false"><?php echo $playlist['url']?></textarea>
<div class="help-block"></div>
</div>
<?php endforeach;?>

这样视图中不能用 ActiveForm 去处理了,还写了一堆 html 和 php 的混合代码,

求助大神,有没有其他的思路去实现,可以在视图中方便使用 ActiveForm 去处理?

谢谢!

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册