Yii2 display multiple images in gridview row -
i want display multiple images in gridviews single row. example: have table a, table b , table c.
table has my_id.
in table b my_id foreign key. along my_id has c_id.
table c has c_id in reference in table b.
table c has filepath display images.
in table a have my_id
follows: 1, 2, 3, 4, 5, 6.
in table b have my_id
follows. 1 ,1 ,1 ,2 ,3, 3.
in table b have c_id
follows. 1, 2, 3, 4, 5, 6.
in table c c_id's are: 1, 2, 3, 4, 5, 6. , these id's have filepath associated each of them. different images.
now gridview should display 3 different images my_id
because of foreign key constraints. displays 1 image.
my code below:
in model
public function getpictogramsid() { $pictogramsid = sdsrefghspictograms::find()->where(['sdsref_id' => $this->sdsref_id])->all(); foreach ($pictogramsid $picid){ return $picid->pictogram_id; } } public function getpictogrampath() { $pictogramsid = ghspictogram::find()->where(['pictogram_id' => $this->getpictogramsid()])->all(); foreach ($pictogramsid $picid){ $pic = $picid->pictogram_filepath; } return $pic; } public function getpictogramurl() { //var_dump($this->getpictogrampath()); exit(); return \yii::$app->request->baseurl.'/web'.$this->getpictogrampath() ; }
my index file grid view image code
[ 'label' => 'hazards', 'format' => 'raw', 'value' => function ($data) { return html::img($data->getpictogramurl(), ['alt'=>'myimage','width'=>'20','height'=>'30']); }, ],
i trying add bootstrap tool tip this.. tool tip displaying think looping not not done in correct way repeating images.
here updated gridview code.
[ 'label' => 'hazards', 'format' => 'raw', 'value' => function ($data) { $images = ''; // append images foreach($data->getpictogramname() $name) foreach ($data->getpictogramurl() $url) $images = $images.html::img($url,['alt'=>'','width'=>'30','height'=>'30', 'data-toggle'=>'tooltip','data-placement'=>'left','title' => $name ,'style'=>'cursor:default;']); return $images; } ],
you have few logical errors in model , grid view. in these areas dealing 1 item instead of three.
in model
public function getpictogramsid() { $ids = []; $pictogramsid = sdsrefghspictograms::find()->where(['sdsref_id' => $this->sdsref_id])->all(); foreach ($pictogramsid $picid){ $ids[] = $picid->pictogram_id; } return $ids;// returning 3 ids } public function getpictogrampath() { $pic = []; $pictogramsid = ghspictogram::find()->where(['pictogram_id' => $this->getpictogramsid()])->all(); foreach ($pictogramsid $picid){ $pic[] = $picid->pictogram_filepath; } return $pic; } public function getpictogramurl() { $url = []; foreach($this->getpictogrampath() $path): $url[] = \yii::$app->request->baseurl.'/web'.$path; endforeach; return $url; // returning al urls }
now in view loop on urls , append images each url
[ 'label' => 'hazards', 'format' => 'raw', 'value' => function ($data) { $images = ''; // append images foreach($data->getpictogramurl() $url): $images = $images.html::img($url, ['alt'=>'myimage','width'=>'20','height'=>'30']); endforach; return $images; }, ],
Comments
Post a Comment