I am working on a cms for properties/ads in oop php for learning purposes. I have a gallery of photos for each property/ad and three tables.
properties (id, location, price, main_photo_id)
property_photo (id, property_id, photo_id)
photos (id, name, extension)
I am trying to display main photo on a page (from main_photo_id) that is same ID as photo_id (from property_photo) and also same ID from photos table. I need to select name and extension from photos table and then to connect to main_photo_id through query but when I write query I get no result on page and when I vardump variable it says bool(false), but in database it changes main_photo_id correctly. Any help is appreciated. Here is my code:
AdModel:
public function showMainPhoto($id)
{
$this->db->query('SELECT p.id, p.name, p.extension FROM photos AS p
RIGHT JOIN property_photo AS pp ON pp.photo_id = p.id
RIGHT JOIN properties AS pro ON pro.main_photo_id = pp.photo_id
WHERE pro.main_photo_id = :main_photo_id');
$this->db->bind(':main_photo_id', $id);
$row = $this->db->single();
return $row;
}
AdsController:
public function viewAction()
{
if (!isset($_GET['id'])) {
$ad_id = $_SESSION['ad_id'];
} else {
$ad_id = $_GET['id'];
}
$ad_data = $this->AdModel->adData1($ad_id);
$data = $this->AdModel->getPhotoForPropertyView($ad_id);
$data1 = $this->AdModel->showMainPhoto($ad_id);
var_dump($data1);
$data2 = $this->AdModel->getPhotosForProperty($ad_id);
$this->view->render('ads/view', $data, $ad_data, $data1, $data2);
}
view.php:
<picture>
<h1>Main Photo</h1>
<img src="<?php echo '/public/photos/'.$data1->name.'.'.$data1->extension ?>" class="img-fluid img-thumbnail" width="350" height="350">
</picture>