2

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.

6
  • You must have a function that doesn't return an array as you expect, you probably need to review your validation when function calls fail and don't return the data you expect before using it. you also haven't shown us the lines of code related to that specific error. $var = false; echo $var['foo']; will generate the error as a hint of what to look for. Commented Jan 28, 2022 at 4:31
  • I have added some more code to try and show the issue. The target class "HelperImageGallery" and its functions return data as expected because I have used them multiple times within the project with no errors. The error is occurring uniquely when I call them from within the nested foreach loop of this class. If there is more code that I can show to diagnose this issue please indicate what because the project is some 15000 lines of code in about 100 files. Commented Jan 28, 2022 at 5:01
  • 1
    I think you need to figure out exactly where the issue happens I see you highlight $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. Commented Jan 28, 2022 at 6:14
  • So in principle the code to call a class and function from within a nested foreach loop is ok? First time I have done this which is why I am querying it. For reference I am using the latest xampp which is ver 8.1.1 of PHP. From what I have read these Boolean errors are a new thing from php 7 or higher produced when the called function returns nothing, i.e. a false. Particularly baffling is that the function being called is being used elsewhere in the project and works fine. And yes any function I call with $this->HelperImageGallery = new HelperImageGallery($this->con, $imgId); fails. Commented Jan 28, 2022 at 7:21
  • 1
    The PHP error reports a file and line number, include the code at that line in your question. Commented Jan 28, 2022 at 7:48

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

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.