0

I found this questions which helps me with what I am doing, but the problem that I'm running into is - I keep getting an Undefined index error for this $images = glob($imagesDir. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

Here is my full code (it is a part of an if/elseif):

elseif($result['avatar_type'] == 'Random'){ 
    $images = array(); //Initialize once at top of script
    $imagesDir = 'avatar/random/';
    if(count($images)==0){
      $images = glob($imagesDir. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
      shuffle($images);
      }
    $avatar = array_pop($images);
}

What I am trying to do is if the database has the avatar_type set to Random then display a random image from the Random directory, but like I said above I keep getting an Undefined index error.

Does anyone see anything wrong with what I am doing and why I would be receiving this error?

Community
  • 1
  • 1
iamthestreets
  • 733
  • 1
  • 15
  • 38
  • 1
    Could you post the error message? – James Westman Dec 02 '14 at 22:35
  • 1
    he said it , its undefined index error – meda Dec 02 '14 at 22:36
  • Usually the error message isn't just "undefined index error", though – James Westman Dec 02 '14 at 22:37
  • @kittycat3141 what is it usually ? – meda Dec 02 '14 at 22:38
  • 2
    Echo out `$images` and make sure it looks ok. You may have to be more explicit about the path to the photos. – Jay Blanchard Dec 02 '14 at 22:42
  • have you tried `elseif (isset($result['avatar_type']) && $result['avatar_type'] == 'Random') { `? – Roman Kliuchko Dec 02 '14 at 22:45
  • @meda mine said something like `Undefined offset: 1 in /this/is/a/path.php`. It should say what the undefined index is. – James Westman Dec 02 '14 at 22:47
  • 1
    BTW, `if(count($images)==0){` doesn't make any sense, because you've set `$images` to empty array directly, so this statement will always return true. – Roman Kliuchko Dec 02 '14 at 22:47
  • 1
    does `$images` contain anything after your `glob()`? `print_r($images);` I'll bet you are reading a non existing directory. Try using `dirname(__FILE__)` to get the full path starting with a `/` – blots Dec 02 '14 at 22:49
  • Are you sure it's in this code? I couldn't get this error no matter what I tried. – James Westman Dec 02 '14 at 22:58
  • @iamthestreets please do not shuffle a "who knows how long" array to take the first element. If you need a random element from array at least compute a (pseudo)random number (between 0 and array's length) and take it from the array directly (the access complexity will be O(1)). [Great info about how arrays are implemented in PHP](http://stackoverflow.com/questions/2350361/how-is-the-php-array-implemented-on-the-c-level) – PauloASilva Dec 02 '14 at 23:06

1 Answers1

1

A few suggestions:

This isn't neccessary as glob will return an array:

$images = array(); //Initialize once at top of script

see http://php.net/manual/en/function.glob.php

This will cause a warning (but not an error) if glob previously returned false:

$avatar = array_pop($images);

http://php.net/manual/en/function.array-pop.php

If you make sure to check return types in the manual you will know what to check for in your code.

if (empty($var)) is great because it checks for false, null, or undefined without throwing an error.

Also, as array_pop returns the last element and glob is likely to return the elements in the same order it will not be as random as array_rand would be.

$avatarKey = array_rand($images, 1); //return 1 random result's key
$avatar = $images[$avatarKey]; //set the random image value (accessed w/ the rand key)

Your error message shouldn't be caused by the glob line, it's actually probably from this:

elseif($result['avatar_type'] == 'Random'){ 

If avatar_type isn't set on the result array or the result array is empty you will get an undefined index.

To prevent that error from happening you would check the array exists before trying to access the avatar_type key:

function example:

function getRandomAvatar($result)
{
    if (empty($result) || empty($result['avatar_type'])) {
        return;  //quit execution if the data is bad
    }
    //rest of code here -
}

inline code example:

if (empty($result) || empty($result['avatar_type'])) {
    //do nothing, render an error, whatever - stops execution of the next statement
} else {
    //this code will only run if $result and $result['avatar_type 
    //are set and wont cause errors
    if ('$result['avatar_type'] == 'Random') {
        //do code here

Your error should have a line number. Check that line and the line right before it.

Matt A
  • 1,096
  • 6
  • 6
  • Thanks for the suggestions. It started working on it's own this morning, but if there is a better way to write it I would like to do so. I understand what your asking me to change, but I'm not sure how to do it. Can you give an example? – iamthestreets Dec 03 '14 at 14:06