Re: [PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-07 Thread Kevin Waterson
This one time, at band camp, PHP Gen [EMAIL PROTECTED] wrote:

 Hi,
 I have a function that reads jpg files (thumbnails)
 from a directory and puts all the files names into an
 array...I want to sort that array by the filename,
 problem is, I dont know if filenames will be pure
 numeric (eg 001.jpg,002.jpg) or alpha-numeric
 (asdf001,asdf002)


?php 
  $hdl = opendir('./'); 
  while ($dirEntry = readdir($hdl)) { 
if (substr($dirEntry, 0, 1) != '.') {
  $listing[] = $dirEntry; 
  }
}
  natsort($listing);
  closedir($hdl);
  foreach($listing as $blah) { echo $blah.'br /'; }
?

or something.

Kevin

-- 
 __  
(_ \ 
 _) )            
|  /  / _  ) / _  | / ___) / _  )
| |  ( (/ / ( ( | |( (___ ( (/ / 
|_|   \) \_||_| \) \)
Kevin Waterson
Port Macquarie, Australia

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-06 Thread PHP Gen
Hi,
I have a function that reads jpg files (thumbnails)
from a directory and puts all the files names into an
array...I want to sort that array by the filename,
problem is, I dont know if filenames will be pure
numeric (eg 001.jpg,002.jpg) or alpha-numeric
(asdf001,asdf002)

It HAS to be sequential from small to big (eg: 0-10)
as I am making static html pages from them further
down the program.

Looking in the manual I have tried sort() without any
luck, then looking further I found natcasesort() which
would be perfect for my needs right now, but cant get
it to work :-( 

Below is the function (its not big)

*** Start function 

function directory($dir,$filters){
$handle=opendir($dir);
$files=array();
if ($filters == all){while(($file =
readdir($handle))!==false){$files[] = $file;}}
if ($filters != all){
$filters=explode(,,$filters);
while (($file = readdir($handle))!==false) {
for ($f=0;$fsizeof($filters);$f++):
$system=explode(.,$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}




$pics=directory(pics,jpg,JPG,JPEG,jpeg,png,PNG);

*** End function 

I have tried putting natcasesort() in many places but
not working

Thanks in advance.
-Mag

=
--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)



__
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-06 Thread Curt Zirzow
* Thus wrote PHP Gen:
...
 
 Looking in the manual I have tried sort() without any
 luck, then looking further I found natcasesort() which
 would be perfect for my needs right now, but cant get
 it to work :-( 
 
 Below is the function (its not big)
 
 *** Start function 
 
 function directory($dir,$filters){
   $handle=opendir($dir);
   $files=array();
   if ($filters == all){while(($file =

For starters, if your going to provide some code, make sure its
readable by others.. trying to figure out what it is doing is
nearly impossible the way it is written..

function directory($dir,$filters){

  $handle=opendir($dir);
  $files=array();

  if ($filters == all){

while(($file = readdir($handle)) !== false) { 
  $files[] = $file;
}

  }

  if ($filters != all) {
$filters=explode(,,$filters);

while (($file = readdir($handle)) !== false) {

  for ($f=0;$fsizeof($filters);$f++) {
$system=explode(.,$file);

if ($system[1] == $filters[$f]) {
  $files[] =+ $file;
}

  }

}
  }

  closedir($handle);

  return $files;
}

ahh.. much better, but before we go on.. some redefining of the
function: 

/* if no filter provided, this will
 * return all files in the directory */
function directory($dir,$filters=''){

  $handle=opendir($dir);
  if (! $handle ) { /* better check this!! */
return false;
  }

  $files=array();

  /* I may have extension I want that ends in .all 
   * function is redefined to reflect this */

  //if ($filters == all)
  if (! $filters){

while(($file = readdir($handle)) !== false) { 
  $files[] = $file;
}

  } else {
/* Don't need to re-evaluate $filters since this
 * is an if else situation. */

//$filters=explode(,,$filters);
/* not wise to change the meaning of a variable */

$filter_list = explode(',', $filters);

while (($file = readdir($handle)) !== false) {

  /* ouch..!  this is rather expensive, in a tight loop
   * always try to have the php engine do the filtering
   * in situations like this. see after /intensive code..
   
  for ($f=0;$fsizeof($filters);$f++) {
$system=explode(.,$file);

// note: this is assuming that there is only one dot
if ($system[1] == $filters[$f]) {

  // I'm assuming =+ was by accident, should've been =
  $files[] =+ $file;

}
  }
  // end intensive code
  */

  // instead... simple extension finder
  if ($dot = strrpos($file, '.') !== false) {

// and see if it passes the filter
if (in_array(substr($file, $dot+1), $filter_list)) {
  $files[] = $file;
}
// note: preg_match'ing a pattern would probably be most
// optimal, since the pattern is cached.
  }

}
  }

  closedir($handle);

  return $files;
}

 $pics=directory(pics,jpg,JPG,JPEG,jpeg,png,PNG);
 
 *** End function 

 
 I have tried putting natcasesort() in many places but
 not working

oh. well.. how were you trying natcasesort()?

With the example provided in the manual:

$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png',
'img2.png', 'img1.png', 'IMG3.png');

//...

natcasesort($array2);
print_r($array2);

should help.


Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-06 Thread PHP Gen
Hi Curt,

Damn, looks like I (unintentionally) gave you guys
quite a challenge! You're the first one to reply and
looks like you sure worked on it!

 For starters, if your going to provide some code,
 make sure its
 readable by others.. trying to figure out what it is
 doing is
 nearly impossible the way it is written..

Sorry about that, I didnt write the code, I snipped
it off from phpbuilder (i think, might have been
another php help site) and since it was working I
didnt touch or do anything.

Will go through the modifications and advise you gave
me and will write back if.. any problems.

Thanks again,
-Mag

=
--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)



__
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php