basic array help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MWG_T_H_O_M_A_S
Forum Newbie
Posts: 7
Joined: Mon Jun 28, 2004 10:33 am

basic array help

Post by MWG_T_H_O_M_A_S »

ok heres my script

Code: Select all

<?
	#include_once( "src/config.inc" );
	
	
 function getFileList( $letter )
 {

	$dir = opendir("tom/Cars");
	
	$count = 0;
	while($item = readdir($dir))
	{
		if($item=="." or $item=="..") 
		{
			continue;
		}

		//$item = strtolower( $item );

		if($item[0] == $letter)
		{
	
			$list[$count] = $item;
			$count++;
		}
	}


	return $list;
	
 
 
 }
	
	
	
	
	 $resultList = getFileList( 'B' );
	
	
	 sort($resultList);
	
	
	 foreach ( $resultList  as  $car) 
	{ 
	  $carlink= $car;
	  $carname= str_replace ("_", " ",$car);
	  $carname= str_replace (".php"," ",$carname);
	  
	echo '<a href="tom/Cars/'.$car.'">'.$carname.'</a><br>'; 
	}	   
	   
	
//	echo count($resultList);		
//	print_r( $resultList );
	
	
	
?>

Basically i am getting errors when i use "getFileList( 'B' )" when i have no files beginning with "B" in the directory, if i add some files beginning with "B" this will work fine, however i need someway to ensure that it wont give me errors when there are no files beginnign with "B". How do i do this :)?

Hope that makes sense, if u need to know anything else pls ask.

Thanx,

Tom

Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

What are the errors you are encountering?

I'm guessing errors about undefined arrays.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd bet the errors are something like "undefined index 0:".

the following should fix that:

Code: Select all

<?php

function getFileList( $letter ) 
{ 
  $letter = strtolower($letter);
  $letter = $letter{0};

   $dir = opendir("tom/Cars"); 
    
   $count = 0; 
   while($item = readdir($dir)) 
   { 
      if($item=="." or $item=="..") 
      { 
         continue; 
      } 

      $item = strtolower( $item ); 

      if($item{0} == $letter) 
      { 
    
         $list[] = $item; 
      } 
   } 


   return $list; 
    


} 

?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I'm thinking it has more to do with the fact that if there are no matches found then $list is never set. So....

Code: Select all

function getFileList($letter)
{
  $list = array();
  $dir = @opendir("tom/Cars");
  if ($dir)
  {
    while (false!==($item = readdir($dir)))
    {
      if($item=="." or $item=="..")
      {
        continue;
      }

      //$item = strtolower( $item );

      if($item{0} == $letter)
      {
        $list[] = $item;
      }
    }
    closedir($dir);
    return $list;
  }
}
feyd,

You can use the [] style syntax on non array values (i.e. strings), personally I wouldn't recommend it but it doesn't seem to cause a problem, e.g.

Code: Select all

$string = "Some string";
echo $string[0];
echo $string{0};
Outputs...

Code: Select all

SS
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah forgot about that... oh well
Post Reply