'php calling a function in a class from within a nested foreach loop
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.
Solution 1:[1]
I found the root cause: I am storing imgIDs as a coma separated string in a table column of imgIdArr with primary key column of attId. Two common errors are typo of missing coma or the ImgId refers to an imgId that does not exist in the ImgTable.
Solution was to write a test script that turns the coma separated string into an array of imgIds then parse each one for string length greater than 8 or imgId is not in imgTable. screen output highlights these errors with a NOK output in bold red font.
**************** TOTAL_IMAGES_FOR A0014 IS 5 *****************
A0014-01A0014-02 [NOK] ** A0014-03 [OK] ** A0014-04 [OK] ** A0014-05 [OK] ** A0014-99 [NOK] **
<<<<<<<<<<<<<<<<<< END of IMAGES FOR A0014 >>>>>>>>>>>>>>>>>>>
Huge thanks to Scuzzy for focusing my diagnostic in the right area.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elliott Farmer |
