我正在使用yii\bootstrap\Carousel
,并且我的图像文件名存储在数据库中。因此,我需要检索图像文件名并将它们包装为图像源和有效URL,然后再将其传递到Carousel小部件。我试图从控制器执行此任务,但遇到一个挑战,即数组中未传递任何内容或返回了Invalid argument supplied for foreach()
错误消息。这是我的代码:
控制器视图操作:
/**
* Displays a single Listing model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->listing_id]);
} else {
$count = \common\models\ListingImages::find()->count();
// $query = new yii\db\Query;
$images = \common\models\ListingImages::find()->where(['listing_id' => $model->listing_id])->all();
if($count>0){
$directory = Yii::$app->params['uploadUrl'];
$items = [];
foreach($images as $image){
$image_url_link = $image->image_url_link;
$path = '@web/'. $directory . $image_url_link;
$image_path = Html::img($path);
$items = ArrayHelper::merge($image_path, $items);
}
} else {
$items = [];
}
// echo var_dump($items);
return $this->render('view', ['model' => $model, 'items' => $items]);
}
}
这是我的view.php代码:
<?php
use yii\helpers\Html;
use kartik\detail\DetailView;
use kartik\datecontrol\DateControl;
use yii\helpers\ArrayHelper;
/**
* @var yii\web\View $this
* @var common\models\Listing $model
*/
$this->title = $model->listing_title;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Listings'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div>
<p>
<?= Html::a('Update', ['update', 'id' => $model->listing_id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->listing_id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this Listing?',
'method' => 'post',
],
]) ?>
</p>
<?php if (Yii::$app->session->hasFlash('delete_unsuccessful')): ?>
<div class="alert alert-error alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<h4><i class="icon fa fa-check"></i>Unable to Delete!</h4>
<?= Yii::$app->session->getFlash('delete_unsuccessful') ?>
</div>
<?php endif; ?>
</div>
<div class="row">
<div class="col-md-8">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Listing Images</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<?php echo yii\bootstrap\Carousel::widget(['items'=>$items]); ?>
</div>
</div>
</div>
</div>
我期望在items数组中像
$items=[
'<img src="http://placehold.it/1000x1000?text=Product+01">',
'<img src="http://placehold.it/1000x1000?text=Product+02">',
'<img src="http://placehold.it/1000x1000?text=Product+03">',
'<img src="http://placehold.it/1000x1000?text=Product+04">',
'<img src="http://placehold.it/1000x1000?text=Product+05">',
'<img src="http://placehold.it/1000x1000?text=Product+06">',
'<img src="http://placehold.it/1000x1000?text=Product+07">',
'<img src="http://placehold.it/1000x1000?text=Product+08">'
];
请协助我解决问题。我似乎在网上找不到答案。
这就是我在if函数之前var_dump
变量$images
时得到的结果...
array(5) { [0]=> object(common\models\ListingImages)#167 (12) { ["filename"]=> NULL ["image"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["image_id"]=> int(9) ["listing_id"]=> int(28) ["image_url_link"]=> string(36) "sLgINOnRlkJuu6uKYwnAchcJswaiHxM-.jpg" ["updated_at"]=> string(19) "2018-11-09 12:48:34" ["created_at"]=> string(19) "2018-11-09 12:48:34" ["created_by"]=> int(2) ["active"]=> string(1) "Y" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["image_id"]=> int(9) ["listing_id"]=> int(28) ["image_url_link"]=> string(36) "sLgINOnRlkJuu6uKYwnAchcJswaiHxM-.jpg" ["updated_at"]=> string(19) "2018-11-09 12:48:34" ["created_at"]=> string(19) "2018-11-09 12:48:34" ["created_by"]=> int(2) ["active"]=> string(1) "Y" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } } [1]=> object(common\models\ListingImages)#178 (12) { ["filename"]=> NULL ["image"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["image_id"]=> int(10) ["listing_id"]=> int(28) ["image_url_link"]=> string(36) "ZKTPUE_dR7P3lF7CT9UZnOcC0ZL2Ws43.jpg" ["updated_at"]=> string(19) "2018-11-09 12:48:34" ["created_at"]=> string(19) "2018-11-09 12:48:34" ["created_by"]=> int(2) ["active"]=> string(1) "Y" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["image_id"]=> int(10) ["listing_id"]=> int(28) ["image_url_link"]=> string(36) "ZKTPUE_dR7P3lF7CT9UZnOcC0ZL2Ws43.jpg" ["updated_at"]=> string(19) "2018-11-09 12:48:34" ["created_at"]=> string(19) "2018-11-09 12:48:34" ["created_by"]=> int(2) ["active"]=> string(1) "Y" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } } [2]=> object(common\models\ListingImages)#179 (12) { ["filename"]=> NULL ["image"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["image_id"]=> int(11) ["listing_id"]=> int(28) ["image_url_link"]=> string(36) "vSZYiTTk1-bPk9MNGQ4RuU1Rih3adfgJ.jpg" ["updated_at"]=> string(19) "2018-11-09 12:48:35" ["created_at"]=> string(19) "2018-11-09 12:48:35" ["created_by"]=> int(2) ["active"]=> string(1) "Y" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["image_id"]=> int(11) ["listing_id"]=> int(28) ["image_url_link"]=> string(36) "vSZYiTTk1-bPk9MNGQ4RuU1Rih3adfgJ.jpg" ["updated_at"]=> string(19) "2018-11-09 12:48:35" ["created_at"]=> string(19) "2018-11-09 12:48:35" ["created_by"]=> int(2) ["active"]=> string(1) "Y" } ["_related":"yii\db\BaseActiveRecord":private
所有数据库值均按预期加载。
答案 0 :(得分:1)
好吧,我找不到在这里使用ArrayHelper::merge($image_path, $items);
的任何正当理由,因为您的变量$image_path
是字符串而不是数组,并且ArrayHelper::merge()
要求两个参数都为arrays
而不是字符串,如果您担心重复的图像并且只希望输出数组具有唯一的图像,则只需将操作内部的代码更改为以下内容即可。
$items = [];
foreach ($images as $image) {
$image_url_link = "1000x1000";
$path = '@web/' . "my-uploads" . $image_url_link;
$items[] = \yii\helpers\Html::img($path);
}
$items = array_unique($items);