Jump to content

Sort Alphabetically / Directories


darkapec

Recommended Posts

I am a php newb. I have 2 somewhat simple problems. First I have a huge list of movies that I would like sorted in a drop down list alphabetically. I already have the drop down list made and the movies show up correctly. But I cannot figure out how to sort the list. Here is the code

 

<?php

$dir="/backup/Movies";

if ($handle = opendir($dir)) {

  while (false !== ($file = readdir($handle)))

      {

          if ($file != "." && $file != ".." && $file != "index.php")

  {

          $thelist .= '<option>'.$file.'</option>';

          }

      }

  closedir($handle);

  }

?>

<form action="listen.php" method="post" name="table">

<select name="Movie">

<P><?=$thelist?></p>'

</select>

</form>

 

Second problem is inside this directory there are a few other directories is it possible to make the directories show at the top instead of them being sorted alphabetically with the movies.

 

Thanks in advance

Jake

Link to comment
Share on other sites

You can either use the glob function which returns the files already sorted, or put the files into an array and sort that array before generating the HTML.

Something like:

<?php
$dir="/backup/Movies";
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle)))
{
	if ($file != "." && $file != ".." && $file != "index.php")
	{
		$files[] = $file;
	}
}
closedir($handle);
}
sort($files)
?>
<form action="listen.php" method="post" name="table">
<select name="Movie">
<p>
<?php
$tmp = array();
foreach ($files as $file) {
	$tmp[] = "<option value='$file'>$file</option>";
}
echo implode("\n",$tmp) . "\n";
?>
</p>
</select>
</form>

 

As for your other question, use 2 arrays and put the files in one array and the directories in the other array. Sort each array and then output the contents.

 

Ken

Link to comment
Share on other sites

Thank you much for your help, as for using multiple scripts for each folder... that is exactly what I was thinking, after I posted of course.

 

Thanks again for your fast response time. I found a lot of info on sorting alphabetically on the forums but they all seemed so code specific and because I am so new to .php I really needed the help.

 

Jake

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.