Connecting Tech Pros Worldwide Help | Site Map

how to sort images after readdir numerically

anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#1: Jul 2 '09
Hi,

What would be the best way or how should i say do i sort images that are named numerically in order?

this is my original code, i was told that it should sort perfecty fine with what ive got but it doesn't.....its all mixed up

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $path = "upload/$random_digit/images/"; // path to the directory to read ( ./ reads the dir this file is in)
  4. if ($handle = opendir($path)) {
  5.    while (false !== ($file = readdir($handle))) {
  6.     if ($file != "." && $file != "..") {
  7.         if(!is_dir($file)){
  8.             $item[] = $file;
  9.             }
  10.        }
  11.    }
  12.    closedir($handle);
  13. }
  14.  
  15. $total_items = count($item);
  16. $max_items = ceil($total_items / 5); // items per <td>
  17. $start = 0;
  18. $end = $max_items
  19.  
  20. //generate the table
  21. ?>
  22.  
  23.  
  24. <center>
  25. <table width="675px" border="0" cellspacing="5" cellpadding="5" align="center">
  26. <tr>
  27. <?php
  28. $r = 0;
  29. $pn=1;
  30. $pto=1;
  31.  
  32. for($n=0; $n<$total_items; $n++) {
  33. if($n !=0 && fmod($n, 5) == 0) {
  34. echo "</tr><tr>";
  35. }
  36.       $imageL= $path.$item[$n];
  37.      if (substr($imageL,-5) != 'b.jpg')
  38.      {
  39.       $img_path="http://theauctionwinners.com/resources/$imageL";
  40.       $editLink = "http://theauctionwinners.com/resources/imgEdit.php?img=$img_path";
  41.       $iframeName= 'if1';
  42.       $iframeHeight= '955px';
  43.       $click= 'onClick';
  44.             // display the item
  45.             echo '<td><center><p><a href="'.$img_path .'"><img src= "'.$path.$item[$n] .'" height="100" width="100"></a></p></center>';
  46.             echo '<center><p><input type="hidden" name="picT[]" value="pic'.$pn++.'"/></p></center>';
  47.             echo '<center><p><input type="hidden" name="photoT[]" value="PHOTO '.$pto++.'"/></p></center>';
  48.             echo '<center><p><input type="text" name="captionT[]" value=""/></p></center>';
  49.             echo '<center><p><a href="'.$editLink .'" '.$click.'="document.getElementById('.$iframeName .').height='.$iframeHeight .'; document.getElementById('.$iframeName .').src=this.href; return false;"> > Edit Image < </a></p></center><br></td>';  
  50.     }
  51. }
  52.  
  53. if($r>0) {
  54. for($m=$r; $m<5; $m++) {
  55. echo "<td>&nbsp;</td>";
  56. }
  57. }
  58.  
  59. ?>
  60. </tr>
  61. <td><tr><center><input name="create" type="submit" value="Submit" /></center></tr></td>
  62. </form>
  63. </table>
  64.  
Member
 
Join Date: May 2009
Posts: 81
#2: Jul 2 '09

re: how to sort images after readdir numerically


http://www.google.com/search?hl=en&q...ay&btnG=Search
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,746
#3: Jul 4 '09

re: how to sort images after readdir numerically


The asort function should help with that.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $list = array(3, 6, 2, 5, 4, 1);
  3. asort($list);
  4.  
  5. print_r($list);
  6. ?>
Outputs:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [5] => 1
  4.     [2] => 2
  5.     [0] => 3
  6.     [4] => 4
  7.     [3] => 5
  8.     [1] => 6
  9. )
  10.  
anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#4: Jul 7 '09

re: how to sort images after readdir numerically


thanks for the help....in the end i used

sort($item,SORT_NUMERIC);

and it worked perfectly
Reply