1

Trying to display image in view:

id_image holds image path in db.By using path need to display image. Tried with following code but it not showing any image.

<div class="row">
  <div class="col-xs-6 col-md-3">
    <a href="#" class="thumbnail">
      <img src="<?php $dataProvider->models[0]->id_image; ?>">
    </a>
  </div>

View:

public function actionView($id)
    {
         $searchModel = new CreateBookingsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('view', [
            'model' => $this->findModel($id),
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

Create/Upload File in Controller

public function actionCreate()
    {
        $model = new CreateBookings();

        if ($model->load(Yii::$app->request->post()))
        {
            $imageName = $model->first_name;
            $mobile = $model->primary_mobile;
            $model->file = UploadedFile::getInstance($model, 'file');
            $model->file->saveAs( 'uploads/id_images/'.$imageName.'_'.$mobile.'.'.$model->file->extension);
            //save the path in the db column
            $model->id_image = 'uploads/id_images/'.$imageName.'_'.$mobile.'.'.$model->file->extension;

            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

Path Example saved in db:

uploads/id_images/kumar_9015879992.jpg
6
  • You store a relative path or an absolute path? .. show an example, show also action and view code.. Commented Feb 1, 2016 at 19:44
  • please see the updated question details Commented Feb 1, 2016 at 19:52
  • I have posted an answer .. i hope is useful Commented Feb 1, 2016 at 20:04
  • syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ']'... getting this error Commented Feb 1, 2016 at 20:05
  • missing $ .. i have update the answer Commented Feb 1, 2016 at 20:09

2 Answers 2

4

You have to add the base url to the image path .Use the below one.

  <img src="<?php echo Yii::getAlias('@web').'/'.$dataProvider->models[0]->id_image; ?>">
Sign up to request clarification or add additional context in comments.

6 Comments

its not showing image for the individual record. Its getting path of first record in db not after that. Image is different for every record. Image should change according to id
If you are getting multiple record then you have run an array of dataProvider value. Use the below code in your view file. <?php $data=$dataProvider->getModels(); foreach($data as $key=>$value){ ?> <img src="<?php echo Yii::getAlias('@web').'/'.$value->id_image; ?>"> <?php } ?>
no no i'm creating booking system, in which user needs to fill the form along with its photo id. every user will have different different photo id along with booking id which generated while filling the form. On Dashboard when you click on booking id it will take you to the particular booking details page which contains all the information about the user. On this view page I need to display image of photo id which he uploaded during the booking. The answer which you have provided will show all the images on page i dont want that, I want to display only one image belongs to the user.
It shows that on view page you will get details of the corresponding user id. Then you have to use... <img src="<?php echo Yii::getAlias('@web').'/'.$model->id_image; ?>">
can I use this in gridview for the same purpose ?
|
0

You want to use first_name name before you save it into DB I guess, try it

if ($model->load(Yii::$app->request->post()))
{
    $model->save();

    //then use first_name on your image like you did.

    $model->save();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.