By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

wrong order of images with readdir()

P: 1
Hi guys....
i know this question has been asked already and I found a few answers but it would be soo great if anyone could help me with this script, which just browses images on a webpage in an image folder on a server.

the script so far is working (a friend gave it to me):

http://dev.perfectday.gb.com/tom/chiaraweb/photo.php

the number on the images are the filenames. I want to order the images by filenames.

the only thing is the order of the images.
I know there is a way with scandir but as I am a beginner with php I am not able to fit the right piece of code into the right bit of my photos.php.

if anyone could tell me how I can make this work would be amazing.
I am making this website for a freind so that I cannot hire any programmer for that :(

this is the code of the photo.php.

I didnt ue the part with the category.

thanks a lot
kirstenkirsche
.............................................

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Chiara Romagnoli Photographer</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <link href="style.css" rel="stylesheet" type="text/css">
  6. <script type="text/javascript">
  7. <!--
  8. function MM_preloadImages() { //v3.0
  9.   var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  10.     var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  11.     if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  12. }
  13. //-->
  14. </script>
  15. </head>
  16.  
  17. <body>
  18. <table align="center" cellpadding="0" cellspacing="0">
  19.   <tr>
  20.     <td><a href="index.html"><img src="pictures/header.gif" alt="Chiara Romagnoli Photographer" width="501" height="84" /></a></td>
  21.   </tr>
  22.   <tr>
  23.     <td><div align="right"><a href="photo.php"><img src="pictures/gallery.gif" alt="gallery" width="62" height="20"></a><a href="about.html"><img src="pictures/about.gif" alt="about" width="60" height="20"></a><a href="contact.html"><img src="pictures/contact.gif" alt="contact" width="75" height="20"></a></div></td>
  24.   </tr>
  25. </table>
  26. <table border="0" cellspacing="0" cellpadding="5" align="center">
  27.   <tr>
  28.     <td align="center">
  29.       <table border="0" cellspacing="0" cellpadding="0">
  30.         <tr>
  31.           <td>
  32.             <div align="center"><br>
  33. <?php
  34.  
  35. $Pfadcat1 = "./pictures/gallery/";
  36. $Pfadcat2 = "./photos/2/";
  37. $Pfadcat3 = "./photos/3/";
  38.  
  39. #echo "<hr>index-übergeben<br>"."$index"."<hr>";
  40. #echo "<hr>cat-übergeben<br>"."$cat"."<hr>";
  41.  
  42.  
  43. #echo "Per POST übergebene Variablen:<br>\n";
  44. while (list ($key, $val) = each ($_GET))
  45. {
  46.     #echo "$key => $val<br>";
  47.     if ($key=="cat")
  48.     {
  49.      $cat=$val;
  50.     }
  51.     if ($key=="index")
  52.     {
  53.      $index=$val;
  54.     }
  55. }
  56.  
  57. #echo "<hr>index-übergeben<br>$index<hr>";
  58. #echo "<hr>cat-übergeben<br>$cat<hr>";
  59.  
  60. if ($cat==1)
  61. {
  62.  $Pfad = $Pfadcat1;
  63. }
  64. if ($cat==2)
  65. {
  66.  $Pfad = $Pfadcat2;
  67. }
  68. if ($cat==3)
  69. {
  70.  $Pfad = $Pfadcat3;
  71. }
  72. if (($cat!=1)&&($cat!=2)&&($cat!=3))
  73. {
  74.  $Pfad = $Pfadcat1;
  75. }
  76.  
  77. #echo $Pfad;
  78. #echo "<hr>index-übergeben<br>$index<hr>";
  79. #echo "<hr>cat-übergeben<br>$cat<hr>";
  80.  
  81. $counter = 0;
  82. #anzahl datein ermitteln
  83. $i=0;
  84. $verz=opendir ($Pfad);
  85. while ($file=readdir($verz))
  86.  
  87.  
  88.  
  89. {
  90.     if (filetype($Pfad.$file)!="dir")
  91.     {
  92.         $i++;
  93.         #echo "<br>.$file";
  94.  
  95.  
  96.     }
  97. }
  98. closedir($verz);
  99. #linkzähler ermitteln
  100. #echo "<hr>anzn<br>".$i."<hr>";
  101. #sicherheit
  102. if($index<1||$index>$i)
  103. { $index = 1;}
  104.  
  105. $prev = $index-1;
  106. $next = $index+1;
  107. if($prev<1)
  108. { $prev = $i;}
  109. if($next>$i)
  110. { $next = 1;}
  111.  
  112. if($Verzeichniszeiger = opendir($Pfad))
  113. {
  114.     while($Datei = readdir($Verzeichniszeiger))
  115.     {
  116.         if($Datei != "." && $Datei != "..")
  117.         {
  118.             $Bilddaten = getimagesize($Pfad.$Datei);
  119.             $counter++;
  120.             #echo "<hr>".$counter."<hr>";
  121.  
  122.             if($index==$counter)
  123.             {
  124.  
  125. ?>
  126.               <table border="0" align="center" cellpadding="5" cellspacing="0">
  127.  
  128.                 <tr>
  129.                   <td colspan="3">
  130.                     <div align="center">
  131.                       <table width="100" cellpadding="4" cellspacing="0" bgcolor="#FFFFFF">
  132.                         <tr>
  133.                           <td>
  134.                             <div align="center"><a href="photo.php?index=<?php echo$next; ?>&cat=<?php echo$cat; ?>"><img src="<?php echo$Pfad.$Datei; ?>"></a><br>
  135.                           <?php
  136.             }
  137.         }
  138.     }
  139.     closedir($Verzeichniszeiger);
  140. }
  141. #echo "<hr>".$counter."<hr>";
  142. ?> </div> </td>
  143.                       </tr>
  144.                         </table>
  145.                   </div></td>
  146.                 </tr>
  147.                 <tr valign="top">
  148.                   <td height="25"><a href="photo.php?index=<?php echo$prev; ?>&cat=<?php echo$cat; ?>"><img src="images/button1.gif" alt="1" border="0"></a></td>
  149.                   <td height="25"><div align="center"></div></td>
  150.                   <td height="25"><div align="right"><a href="photo.php?index=<?php echo$next; ?>&cat=<?php echo$cat; ?>"><img src="images/button2.gif" alt="2" border="0"></a></div></td>
  151.                 </tr>
  152.  
  153.               </table>
  154.             </div>          </td>
  155.         </tr>
  156.        </table>    </td>
  157.   </tr>
  158. </table>
  159. <div align="center"></div>
  160. </body>
  161. </html>
Oct 8 '08 #1
Share this Question
Share on Google+
2 Replies


Markus
Expert 5K+
P: 6,050
Hey there, Kirsten.

When posting code on the forums, be sure to use [code] tags. [code] .. code goes here [/code]. If you neglect to do so, you will be given a warning and possibly a temporary ban. Please read the Posting Guidelines on How To Ask A Question

Markus.
Oct 8 '08 #2

pbmods
Expert 5K+
P: 5,821
Heya, kirstenkirsche.

Use scandir() to store the filenames in an array (http://php.net/scandir), then use sort() to sort the array (http://php.net/sort).
Oct 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.