sosoro Posted March 5, 2007 Share Posted March 5, 2007 Source ---------------------------------------- if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { // Is it a valid extension? if(!is_dir($file) && is_numeric(strpos($file, "."))) { if($this->_IsValidExtension($file)) $arrImages[] = $file; } } closedir($dh); } } How to change the code to have in $arrImages[] just the first 10 files from that directory, not all files (Order: DESC or ASC). Thanks! Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/ Share on other sites More sharing options...
Orio Posted March 5, 2007 Share Posted March 5, 2007 Add a condition in the while (and first define arrImages as an array...): <?php $arrImages = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false && count($arrImages) < 10) { // Is it a valid extension? if(!is_dir($file) && is_numeric(strpos($file, "."))) { if($this->_IsValidExtension($file)) $arrImages[] = $file; } } closedir($dh); } } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/#findComment-200054 Share on other sites More sharing options...
sosoro Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks. But who to order asc or desc? Thank you ahain! Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/#findComment-200066 Share on other sites More sharing options...
mbtaylor Posted March 5, 2007 Share Posted March 5, 2007 You mean you want the array sorted? $arrImages = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { // Is it a valid extension? if(!is_dir($file) && is_numeric(strpos($file, "."))) { if($this->_IsValidExtension($file)) $arrImages[] = $file; } } closedir($dh); } } array_multisort ($arrImages, SORT_DESC, SORT_STRING); //or array_multisort ($arrImages, SORT_ASC, SORT_STRING); $num_elements = count ($arrImages); array_splice ($arrImages, 10, $num_elements); This is untested code, but I am basically using array_multisort to sort the elements then splicing anything over 10. Good luck! Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/#findComment-200089 Share on other sites More sharing options...
sosoro Posted March 5, 2007 Author Share Posted March 5, 2007 Yes. Is working. For exemple: If i have (SORT_DESC) and i want to show the last 3: 1.jpg 2.jpg 3.jpg 4.jpg 5.jpg Will show: 3.jpg 4.jpg 5.jpg What to add to show like this: 5.jpg 4.jpg 3.jpg Thanks! Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/#findComment-200188 Share on other sites More sharing options...
mbtaylor Posted March 6, 2007 Share Posted March 6, 2007 Well its an array so just use a for loop to show the elements you want. For example: $numelements = count ($arrImages); for ($i=0;$i<3;$i++) { print ($arrImages[$i]); # prints out first 3 elements } for ($i=3;$i<6;$i++) { print ($arrImages[$i]); # prints out elemets 3 - 6 } Hope that helps. Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/#findComment-200638 Share on other sites More sharing options...
sosoro Posted March 8, 2007 Author Share Posted March 8, 2007 But how to sort by number .. just like in myslq. Like this: 1.jpg 2.jpg . . .9.jpg 10.jpg 11.jpg The code sort like this: 1.jpg 10.jpg 2.jpg . . 9.jpg Link to comment https://forums.phpfreaks.com/topic/41287-solved-first-10-files/#findComment-202826 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.