I am trying to call functions in a class "HelperImageGallery" from within a nested foreach loop but I get "Trying to access array offset on value of type bool" errors. The target class "HelperImageGallery" that I want to call is in a separate file which I have included at the top of this class. The functions within the target class work and return correct data as I have used them before elsewhere on this project.
I have also called the target class elsewhere from within a foreach loop and it works. Issue is occurring when I try to call the functions of class "HelperImageGallery" from within the nested foreach loop.
For testing purposes I can get the data within the nested foreach loop performing an SQL query so I can leave this as a permanent solution but for neatness of code I would very much like to know what I am doing wrong that calling class "HelperImageGallery" within nested foreach loop fails.
<?php
require_once('includes/classes/HelperAttractionMeta.php');
require_once('includes/classes/HelperAttraction.php');
require_once('includes/classes/HelperAttractionGallery.php');
require_once('includes/classes/HelperImageGallery.php');
class AutoGenerateSitemapFile {
public function __construct($con) {
$this->con = $con;
} // End of Constructor
public function autoGenerateSitemapXmlFile() {
$this->HelperAttractionMeta = new HelperAttractionMeta($this->con, null);
$attIdArr = $this->HelperAttractionMeta->sitemapAttractionPagesUrlIdSelector();
foreach ($attIdArr as $attId) {
$this->HelperAttraction = new HelperAttraction($this->con, $attId);
$getAttractionTitle = $this->HelperAttraction->getAttTitle();
$getAttUpdatedDateTime = $this->HelperAttraction->getAttUpdatedDateTime();
$this->HelperAttractionGallery($this->con, $attId);
$imgIdComaSeperatedString = $this->HelperAttractionGallery->getAttGalImages();
$imgIdArr = $this->HelperAttractionGallery->galImgIdArray($this->con, $imgIdComaSeperatedString);
// Nested foreach loop //
foreach ($imgIdArr as $imgId) {
// RUNNING A QUERY WORKS //
$query = $this->con->prepare("SELECT imgWebpFullSizePath, IptcDescription, imgCity, imgState, imgCountry, iptcTitle FROM imagegallery WHERE imgId=:imgId");
$query->bindParam(":imgId", $imgId);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
$imgLoc = $row["imgWebpFullSizePath"];
$imgCaption = $row["IptcDescription"];
$imgCity = $row["imgCity"];
$imgState = $row["imgState"];
$imgCountry = $row["imgCountry"];
$imgTitle = $row["iptcTitle"];
} // End of While loop
// CALLING THE DATA VIA CLASS & FUNCTION FAILS //
$this->HelperImageGallery = new HelperImageGallery($this->con, $imgId);
// $imgLoc = $this->HelperImageGallery->getImgWebpFullSizePath();
// $imgCaption = $this->HelperImageGallery->getIptcDescription();
// $imgCity = $this->HelperImageGallery->getImgCity();
// $imgState = $this->HelperImageGallery->getImgState();
// $imgCountry = $this->HelperImageGallery->getImgCountry();
// $imgTitle = $this->HelperImageGallery->getIptcTitle();
} // End of Nested foreach loop
} // End of foreach loop
} // End of class AutoGenerateSitemapFile
any feedback greatly appreciated.
$var = false; echo $var['foo'];will generate the error as a hint of what to look for.$this->HelperImageGallery = new HelperImageGallery($this->con, $imgId)but I don't see enough here to be able to help, the issue is something somewhere is trying to access an array index off a boolean value according to that very specific error message you quoted.