2021年4月30日星期五

How do I insert a function call inside img src?

I'm trying to display images from my database. I'm trying to call my getImages() function from inside my output function (both in same DBdata class) which outputs the properties from my database. I just need this method to call from inside the <img src= line.

However, using $this->getImages() inside the img src= doesn't work and in fact PhpStorm throws an error saying "field getImages() not found in DBdata"... however getImages() ISN'T a field and it's a method.

Below is my code which I'm not sure if it will even output correctly yet so if you have a different way to do it please let me know.

output() method---- line <img src=\"'.$this->getImages().'\" alt=\"\"> is where my error is.

public function output(){      echo "             <div class=\"tab-pane fade show active\" id=\"horizental-post\" role=\"tabpanel\" aria-labelledby=\"horizental-tab\">              <div class=\"sales-items horizental-view\">                  <div class=\"sale-item\">                      <div class=\"row\">                          <div class=\"col-lg-4 col-md-4 col-sm-4\">                              <div class=\"item-img\">                                  <img src=\"'.$this->getImages().'\" alt=\"\">                                  <span class=\"item-status\">" . $this->status . "</span>                                  <a href=\"single_properties.php?id=". $this->id . "\" title=\"\" class=\"view-btn\">View</a>                              </div><!--item-img end-->                          </div>                          <div class=\"col-lg-8 col-md-8 col-sm-8\">                              <div class=\"item-info\">                                  <h3><a href=\"single_properties.php?id=". $this->id . "\" title=\"\">" . $this->name . "</a></h3>                                  <span>" . $this->address . "</span>                                  <p>Aenean sollicitudin, lorem quis bibendum auctor, nisielit quat ipsum, nec sagittis sem nibh id elit. Morbi accumsan sum velit. </p>                                  <div class=\"specs-info\">                                      <ul>                                          <li>Beds: " . $this->beds . " </li>                                          <li>Baths: " . $this->baths . "</li>                                          <li>Sqft: " . $this->sqft . "</li>                                      </ul>                                      <strong class=\"item-price\">£ " . $this->price . "</strong>                                  </div><!--specs-info end-->                              </div><!--item-info end-->                          </div>                      </div>                  </div><!--sale-item end-->              </div><!--sales-items end-->          </div>      ";  }  

The getImages() method is:

public function getImages(){      require_once("assets/database.php");      $conn = new connectDB();      $myDatabase = $conn->getConnectDB();      $sqlQuery= "select image from images WHERE imageID = '". $this->id ."'";      $data = $myDatabase->query($sqlQuery)->fetchAll()[0];      $image = $data['image'];      return '"data:image/jpeg;base64,'.base64_encode($image).'"';  }  
https://stackoverflow.com/questions/67342042/how-do-i-insert-a-function-call-inside-img-src May 01, 2021 at 09:03AM

没有评论:

发表评论