NYCPHP Meetup

NYPHP.org

[nycphp-talk] foreach ERROR

Rob Marscher rmarscher at beaffinitive.com
Wed Jul 19 10:09:18 EDT 2006


Looks like you put the "if(!$return){return array();}" in the wrong spot.

The underlying problem here is that glob("$dir/*.mp3") is not finding 
any mp3 files.  My suggestions were working towards preventing a php 
error from occuring if it doesn't find any mp3 files.  The reason that 
you are getting errors is because you have to supply any "foreach" 
statement with an array.  Currently, some of your foreach's are getting 
supplied NULL when no mp3 files are found and that's what causes the errors.

Here's my updated version of the original 19 lines you supplied (line 4a 
is what I added):

    1.  <?
    2. function readD($dir){
    3. if(!$dir){$dir = '.';}
    4. foreach(glob("$dir/*.mp3") as $item){$sort[]= 
end(explode('/',$item));}
    4a. if(!is_array($sort)) { return array(); }
    5.  foreach($sort as $sorteditem){
    6.   if($sort){natsort($sort);}
    7.     foreach($sort as $item){$return[]= $item;}
    8.      if(!$return){return array();}
    9.      }
   10.   return $return;
   11.   } ?>
   12. <?php header("Content-type: text/xyxml") ?>
   13. <? echo(' <?xml version="1.0" encoding="UTF-8"?> ') ; ?>
   14. <? echo '<player showDisplay="yes" showPlaylist="no" autoStart="yes">
   15. ' ;
   16.
   17. $sFolder = '../Audio/Music' ;
   18. foreach(readD($sFolder) as $item)
   19. {

Hope that clears it up.
-Rob


CED wrote:
> This is the most recent, and stripped down, state:
> 
> <?
> $dir = '../Audio/Music';
> foreach(glob("$dir/*.mp3") as $item){$sort[] = (end(explode('/',$item)));}
>     foreach($sort as $item){$return[]= $item;}
>      if(!$return){return array();}
> 
>   echo($return[0]) ;
>   ?>
> 
> 
> And this is still the error:
> 
> Warning: Invalid argument supplied for foreach() in
> /hsphere/local/home/skoilnog/edwardprevost.info/Audio/mp3/Audio.php on line
> 4
> 
> I Am Completely Baffled.
> 
> -Ed
> 
> 
> 
> ----- Original Message ----- 
> From: "Ken Robinson" <kenrbnsn at rbnsn.com>
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Sent: Tuesday, July 18, 2006 7:37 PM
> Subject: Re: [nycphp-talk] foreach ERROR
> 
> 
> At 07:27 PM 7/18/2006, CED wrote:
>> Rob,
>>
>> I placed that iteration areound the last portion of the function, still no
>> worky.
>>
>> Code errors on Line 23 now..
>>
>> foreach(readd($sFolder) as $item)
> 
> 
> 
>   Can you post the current non-working script? With so many different
> suggestions, I'd like to be able to see what you're seeing.
> 
> Ken
> 
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
> 
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
> 
> 
> 
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
> 
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
> 



More information about the talk mailing list