I am wondering how I would re-write this code so that directories found are sorted aplhabetically..

<?PHP
$filearray = array(); 
if ($fil = opendir("apps/$_GET[lang]")) {
	while (($file = readdir($fil)) !== false) {
		if ($file != "." && $file != "..") { 
			$filearray[] = $file;
			$filepieces = explode(".", "$file");
			$filename = $filepieces[0];
                        //-------------------------
			?>


<div class="listbox" style="width: 240px; height: 160px;"> <a href="<?php echo strtolower($file) . ".php"; ?>"><img style="float: left; margin-right: 15px;" src="images/<?php echo $file; ?>.png" border="0" width="48"></a> 
<a href="<?php echo strtolower($file) . ".php"; ?>"> <b>
<?php echo $file; ?>
</b> </a> 

<p>

<?PHP 
$FG = file_get_contents(apps/$_GET[lang]/$filename/caption.php?lang=$_GET[lang]&app=$filename&show=$_GET[show]&csymbol=$_GET[csymbol]&currency=$_GET[currency]&qun=$_GET[qun]&qpw=$_GET[qpw]&domain=$_GET[qun]"); 
echo $FG;
?>
</p>
</div>
<?PHP
                        //-------------------------			    
} } closedir($fil); } ?>

    Store the directory names/paths in an array, and use one of the array sorting functions (e.g. [man]natsort/man) to sort that array before processing it.

      I would create your array of directory items, asort your array and then loop through your array to display your data on the screen.

      example:

      $filearray = array();  
      if ($fil = opendir("apps/$_GET[lang]")) { while (($file = readdir($fil)) !== false) { if ($file != "." && $file != "..") {
      $filearray[] = trim($file); } } } asort($filearray); //sort your array echo '<pre>'.print_r($filearray,1).'</pre>'; // just a printed out example of the sorted array //then foreach() loop through your array to display your HTML

        [man]glob/man can streamline the process of getting the paths into an array. Note that if you just want file names, you can chdir() to the desired directory first:

        $curDir = getcwd();
        chdir('apps/'.$_GET['lang']);
        $files = glob('*.php');
        chdir($curDir);
        natcasesort($files);
        // do stuff with the $files array now
        //
        
          NogDog wrote:
          natcasesort($files); 

          Of course this line is only necessary if glob's lexicographical sorting doesn't match your own definition of "alphabetical"; in which case you might save a whisker by telling glob not to bother doing its own sorting.

            Weedpacket;11011703 wrote:

            Of course this line is only necessary if glob's lexicographical sorting doesn't match your own definition of "alphabetical"; in which case you might save a whisker by telling glob not to bother doing its own sorting.

            Might be OS-dependent, too?

              NogDog wrote:

              Might be OS-dependent, too?

              Quite possible (haven't checked); "alphabetical order" isn't really that well-defined, so different orderings are possible.

                Write a Reply...