This is a discussion on fileorder? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I'm reading out some files from a directory with: $handle=OpenDir (PicDir); /* Verzeichnis �fnen und ein handle erstellen ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I'm reading out some files from a directory with: $handle=OpenDir (PicDir); /* Verzeichnis �fnen und ein handle erstellen welches daraufzeigt */ $PicArray=array(); /* Array erstellen, in welchem sp�er die Bilddaten gespeichert werden */ while($tmpPic=readdir ($handle)) /* schleife durchlaufen, bis alle Dateien durch sind */ { if (FileExt_Check("jpg",$tmpPic)) /* berprfen ob die File-Extension *.jpg ist...*/ { array_push($PicArray,$tmpPic); /* Dateinamen des Bildes in Array kopieren */ } } In what order are they read out? I'm not able to figure it out. Am I able to modify it anyway? Thank you! -- chEErs roN I'm root, I'm allowed to do this! ;) keep on rockin' |
|
|||
|
roN wrote:
> Hi, > I'm reading out some files from a directory with: > $handle=OpenDir (PicDir); /* Verzeichnis �fnen und ein handle > erstellen welches daraufzeigt */ > $PicArray=array(); /* Array erstellen, in welchem sp�er die Bilddaten > gespeichert werden */ > while($tmpPic=readdir ($handle)) /* schleife durchlaufen, bis alle Dateien > durch sind */ > { > if (FileExt_Check("jpg",$tmpPic)) /* berprfen ob die File-Extension *.jpg > ist...*/ > { > array_push($PicArray,$tmpPic); /* Dateinamen des Bildes in Array > kopieren */ > } > } > In what order are they read out? I'm not able to figure it out. Am I able to > modify it anyway? Nope. I guess its sorted by creation time or maybe by position in a FAT. To sort it read all files and sort it yourself. Regards Stefan > Thank you! |
|
|||
|
roN wrote:
> [snip] >> >> Nope. I guess its sorted by creation time or maybe by position in a FAT. >> To sort it read all files and sort it yourself. > > oh :( that sucks... :( Hm, anyway, I did it like this. Is it possible to sort it for more conditions like just the alphabetical one with sort()? I'd like to sort the items in the Array 1st: by a containing String (like if tex contain the word "test" they should come first) 2nd: Alphabetical listening Thank you again! -- chEErs roN I'm root, I'm allowed to do this! ;) keep on rockin' |
|
|||
|
roN wrote:
> roN wrote: > > >>[snip] >> >>>Nope. I guess its sorted by creation time or maybe by position in a FAT. >>>To sort it read all files and sort it yourself. >> >>oh :( that sucks... :( > > Hm, anyway, I did it like this. > Is it possible to sort it for more conditions like just the alphabetical one > with sort()? I'd like to sort the items in the Array > 1st: by a containing String (like if tex contain the word "test" they should > come first) > 2nd: Alphabetical listening Sure that is possible - find a sort function of php that supports a callback function where you can say which of two items is greater - or if there is not such a function write a quicksort by yourself (isn't hard) and use a callback function for the comparison of two items in the callback function you first check for your search text "test" the element that has this text is smaller than the one without, if both items have this text or both haven't you sort by alphabet. Regards Stefan > Thank you again! |
|
|||
|
Stefan Rybacki wrote:
> roN wrote: >> roN wrote: >> >> >>>[snip] >>> >>>>Nope. I guess its sorted by creation time or maybe by position in a FAT. >>>>To sort it read all files and sort it yourself. >>> >>>oh :( that sucks... :( >> >> Hm, anyway, I did it like this. >> Is it possible to sort it for more conditions like just the alphabetical >> one with sort()? I'd like to sort the items in the Array >> 1st: by a containing String (like if tex contain the word "test" they >> should come first) >> 2nd: Alphabetical listening > > Sure that is possible > - find a sort function of php that supports a callback function where you > can say which of two items is greater is uksort() such a function? > - or if there is not such a function write a quicksort by yourself (isn't > hard) and use a callback function for the comparison of two items > > in the callback function you first check for your search text "test" the > element that has this text is smaller than the one without, if both items > have this text or both haven't you sort by alphabet. may you help me a bit further please? Thank you! -- chEErs roN I'm root, I'm allowed to do this! ;) keep on rockin' |
|
|||
|
wouldn't a sort-algorithm like this work:
function MySort($Array, $StringA) { $i=Array; for ($i=0; $i>=Array.size(); $i++) { while (strpos($Array[$i],$StringA))//if Array $i contains StringA, { move Element $i to $i+1; and move $i+1 to $i; } } } but how to move the elements? -- chEErs roN I'm root, I'm allowed to do this! ;) keep on rockin' |
|
|||
|
roN wrote:
> wouldn't a sort-algorithm like this work: > function MySort($Array, $StringA) > { > $i=Array; > for ($i=0; $i>=Array.size(); $i++) > { > while (strpos($Array[$i],$StringA))//if Array $i contains StringA, > { > move Element $i to $i+1; and > move $i+1 to $i; > } > } > } > but how to move the elements? Well, can't you help me anymore? -- chEErs roN I'm root, I'm allowed to do this! ;) keep on rockin' |
|
|||
|
roN wrote:
> roN wrote: > > >>wouldn't a sort-algorithm like this work: >>function MySort($Array, $StringA) >>{ >> $i=Array; >> for ($i=0; $i>=Array.size(); $i++) >> { >> while (strpos($Array[$i],$StringA))//if Array $i contains StringA, >> { >> move Element $i to $i+1; and >> move $i+1 to $i; >> } >> } >>} >>but how to move the elements? > > > Well, can't you help me anymore? I can. uksort is such a function, but you need usort. Use a compare function like this: function mycompare ($a, $b) { //first check for search text in $a and $b $isInA=(strpos($a,'test')!==false); $isInB=(strpos($b,'test')!==false); //so if $isInA==$isInB then compare alphabetical if ($isInA == $isInB) { if ($a == $b) return 0; return ($a > $b) ? 1 : -1; } else { //the one that is true comes first return ($isInA) ? -1:1; } return 0; } this function sorts entries with 'test' in it to the front and alphabetical in case of more than one entries containing this text or in case that there is no 'test' in that entry. use it like this: usort($PicArray,"mycompare"); BTW: your commentaries are in german, may you better switch to english very soon Regards Stefan |
|
|||
|
Stefan Rybacki wrote:
> roN wrote: >> roN wrote: >> >> >>>wouldn't a sort-algorithm like this work: >>>function MySort($Array, $StringA) >>>{ >>> $i=Array; >>> for ($i=0; $i>=Array.size(); $i++) >>> { >>> while (strpos($Array[$i],$StringA))//if Array $i contains >>> StringA, >>> { >>> move Element $i to $i+1; and >>> move $i+1 to $i; >>> } >>> } >>>} >>>but how to move the elements? >> >> >> Well, can't you help me anymore? > > > I can. > > uksort is such a function, but you need usort. > > Use a compare function like this: > > function mycompare ($a, $b) { > //first check for search text in $a and $b > $isInA=(strpos($a,'test')!==false); > $isInB=(strpos($b,'test')!==false); > > //so if $isInA==$isInB then compare alphabetical > if ($isInA == $isInB) { > if ($a == $b) return 0; > return ($a > $b) ? 1 : -1; > } else { > //the one that is true comes first > return ($isInA) ? -1:1; > } > > return 0; > } > > this function sorts entries with 'test' in it to the front and > alphabetical in case of more than one entries containing this text or in > case that there is no 'test' in that entry. > > use it like this: > > usort($PicArray,"mycompare"); > > BTW: your commentaries are in german, may you better switch to english > very soon erm what commentaries? and thank you very much for your help! I think I'll get it done now :) my array looks like this: p1010001-05-09-03.jpg p1010001-05-09-04.jpg p1010001-05-09-05,06.jpg p1010001-05-09-07,08,09.jpg p1010001-05-09-12.jpg p1010001-05-09-13,14.jpg p1010001-rest.jpg p1010002-05-09-03.jpg p1010002-05-09-04.jpg p1010002-05-09-05,06.jpg p1010002-05-09-07,08,09.jpg p1010002-05-09-12.jpg p1010002-05-09-13,14.jpg .... .... .... and i would like to sort it like this: p1010001-05-09-03.jpg p1010002-05-09-03.jpg p1010001-05-09-04.jpg p1010002-05-09-04.jpg p1010001-05-09-05,06.jpg p1010002-05-09-05,06.jpg p1010001-05-09-07,08,09.jpg p1010002-05-09-07,08,09.jpg p1010001-05-09-12.jpg p1010002-05-09-12.jpg p1010001-05-09-13,14.jpg p1010002-05-09-13,14.jpg p1010001-rest.jpg .... .... .... how do I have to manipulate the function you provided to do that? Did you get the order by the way? first ordered by umber then by date... Thank you again! :) -- chEErs roN I'm root, I'm allowed to do this! ;) keep on rockin' |