Jump to content

[SOLVED] First 10 files


sosoro

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.