Question

I'm trying to resolve the undefined offset. I've Google and looked through stack overflow, but the examples I've found either didn't apply or were too complex for someone of my skill to fathom at this time. I'm very green, but I'd done diligence I promise before asking for help and wisdom.

This is the function as it now exists:

function PrintFolio($aaPh) //aaPh =associative array place holder

{
//print out X number rows of unto 4 images from array with X items
//if array had 7 items, print one row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.
    //if array had 13 items, print 3 rows of 4, final row with one item

$itemsCount = sizeof($aaPh);//get size of array

$height = (int)ceil(sizeof($aaPh)/4);//rounded up division by 4

//$height = $height + 1;
$keys = array_keys($aaPh); //get keys

//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row < $height; $row++)    //looping through the rows

    {
        echo '<div class="row flush">'; //open div
        for($image = 0; $image < 4; $image++)    //each row is composed of 4 images
        {
             $aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array
             if( $row*4+$image < $itemsCount ) {
                 $aaPhIndex = $keys[$row*4+$image];
                 printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
                 //$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>';
            }
        }

        echo '</div>'; //end div group of 4
    }//end loop
}//end function

It takes an array, slices it up in to units of four and then prints the array as a series of images to the screen. But if I don't have a number which is exactly devisable by 4, it will display whatever open slots remain as undefined offset errors (I hope I am saying that correctly).

  • My goal is to not have the undefined offset errors print to these screen without revising my site error reporting capabilities (I think doing that would be cheating as it wouldn't correct the problem, just hide it which doesn't seem very above board to me).
Was it helpful?

Solution

Why don't you just put a line break after each 4th element?

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
for($row = 0; $row < Count($aaPh); $row++)    //looping through the rows
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    /* show your picture code here */

}//end loop
echo '</div>';
}//end function

Here is an update after your comments. Because you do not need values of your associative array, only keys, the code can be changed as follows:

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
$row=0;
foreach(array_keys($aaPh) as $img)    //looping through array keys only
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    echo '<img src="' . $img . '">';
    $row++; // increase row counter.
}//end loop
echo '</div>';
}//end function

Make sure to put proper path into the tag

OTHER TIPS

Since you can't assume that you're always going to have a number of elements that's exactly divisible by four, you need to test each one. In your inner loop where you calculate the index and then use it to refer to the array, just add a check to ensure that array element exists. From this:

$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);

To this:

$aaPhIndex = $keys[$row*4+$image];
if (isset($aaPh[$aaPhIndex])) {
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
}

The error in your logic is that on this line

for($image = 0; $image < 4; $image++)    //each row is composed of 4 images

You assume there are always 4 images. But like you said yourself, if the array has for example 13 items, then the last row will only contain one image. This will cause $aaPhIndex = array_keys($aaPh)[$row*4+$image]; to access an array index that doesn't exist.

To solve the problem you will either have to modify $image < 4 to also account for the last row, or just check that the index doesn't exceed the item count. For example by placing the error line under the condition that you already wrote:

if( $row*4+$image < $itemsCount ) {
    $aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array
    $aaPhIndex = $keys[$row*4+$image];
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
    //$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>';
}

Hope this works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top