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
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
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
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.