Jump to content

breaking apart filename for select


smordue

Recommended Posts

I have a directory of files that all have the same convention:

 

Finance-Calculator 1.jpg

Finance-Calculator 2.jpg

Finance-Stack of Coins.jpg

Real Estate-House 1.jpg

Real Estate-Contract.jpg

etc..

 

I am tying to create a Select dropdown and have most of it working with the following:

 

<?php
 if ($handle = opendir('images/heads/')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
	$file_x = substr($file, 0, -4);
	$front = explode("-", $file_x);
          	$thelist .= '<option value="picture-switcher.php?picture='.$file_x.'">'.$file_x.'</option>';
			          }
		       }
  closedir($handle);
  }
?>
<select onchange="window.location.href=this.options[this.selectedIndex].value">
<?=$thelist?>
</select>

 

Here is the tricky part at least for me, I want my list to appear with OptionGroup Labels:

 

<select>

    <optgroup label=Finance>

          <option>etc.</option>

          <option>etc.</option>

          <option>etc.</option>

    </optgroup>

</select>

 

I have different quantities of each categorized file, but want to pluck out just the word Finance for example for the optgroup that would just contain the files that begin with Finance, etc.

 

I got the front exploded above I think, but not sure how to work it the code, or limit it to one occurance per group

 

Is this possible?

 

 

Link to comment
Share on other sites

I havn't tested the following code, but try something like this:

 

<?php
if($handle = opendir('images/heads/')){
  $last_front = "";
  $thelist = "";
  $first_group = true;

  while(false !== ($file = readdir($handle))){
    if($file != "." && $file != ".."){
      $file_x = substr($file, 0, -4);
      $front = explode("-", $file_x);

      if($front[0] != $last_front){
        if(!$first_group){
          $thelist .= '</optgroup>';
        }
        else{
          $first_group = false;
        }
        $last_front = $front[0];
        $thelist .= '<optgroup label="' . $last_front . '">';
      }

      $thelist .= '<option value="picture-switcher.php?picture='.$file_x.'">'.$file_x.'</option>';
    }
  }

  closedir($handle);
}
?>


<select onchange="window.location.href=this.options[this.selectedIndex].value">
<?php echo $thelist; ?>
</select>

 

My code keeps track of the current/last group name. If there was a change, then output an optgroup tag.

Link to comment
Share on other sites

Untested.

$cur_group = "";

if ($handle = opendir('images/heads/')) {
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".."){
           $file_x = substr($file, 0, -4);
         $group = substr($file, 0, strpos($file, '-'))
            if(empty($cur_group)) {
                $thelist .= '<optgroup label="'.$group.'">';
                $cur_group = $group;
            }
            if($group != $cur_group) {
                $thelist .= '</optgroup>';
                $thelist .= '<optgroup label="'.$group.'">';
                $cur_group = $group;
            }
          $thelist .= '<option value="picture-switcher.php?picture='.$file_x.'">'.$file_x.'</option>';
      }
   }
   closedir($handle);
    $thelist .= '</optgroup>';
}

Faux Edit: I wrote this whilst the other two replied. The code is fairly similar to tomcats. The option suggested by salathe is also perfectly valid.

Link to comment
Share on other sites

Tomcant, that is pretty close, but it shows the optgroups multiple times.

 

cags, throws Parse error: syntax error, unexpected T_IF error on this line:  if(empty($cur_group)) {

 

salathe's idea is over my newbie head.

 

I appreciate you guys' help.

 

Steve

 

 

Link to comment
Share on other sites

If you are using PHP 5.x then you can try something more like this...

 

$cur_group = '';
$files = scandir('images/heads');
sort($files);

foreach($files as $file) {
   $file_x = substr($file, 0, -4);
   $group = substr($file, 0, strpos($file, '-'))
   if(empty($cur_group)) {
      $thelist .= '<optgroup label="'.$group.'">';
      $cur_group = $group;
   }
   if($group != $cur_group) {
      $thelist .= '</optgroup>';
      $thelist .= '<optgroup label="'.$group.'">';
      $cur_group = $group;
   }
   $thelist .= '<option value="picture-switcher.php?picture='.$file_x.'">'.$file_x.'</option>';
}

If your not using PHP 5.x then you will have to replace scandir with the code from your original script that fetches filenames, instead of adding them to the string add them to an array. If needs be I'm sure we can help you with that.

Link to comment
Share on other sites

You see this is the problem with helping like this. Tell us EXACTLY what you want to achieve and we'll see what we can do to help. The problem is I could give you a solution, you'll throw in another requirement that will make it unsuitable again.

Link to comment
Share on other sites

To add the selected item simply amend that to $thelist before starting the loop. If you want a specific 'varaible' item to be selected you change the code to something along the lines of...

 

// from this
[color=rgb(0, 0, 187)]$thelist [/color][color=rgb(0, 119, 0)].= [/color][color=rgb(221, 0, 0)]'<option value="picture-switcher.php?picture='[/color][color=rgb(0, 119, 0)].[/color][color=rgb(0, 0, 187)]$file_x[/color][color=rgb(0, 119, 0)].[/color][color=rgb(221, 0, 0)]'">'[/color][color=rgb(0, 119, 0)].[/color][color=rgb(0, 0, 187)]$file_x[/color][color=rgb(0, 119, 0)].[/color][color=rgb(221, 0, 0)]'</option>'[/color][color=rgb(0, 119, 0)];[/color]

// to this
$thelist .= '<option value="picture-switcher.php?picture='.$file_x.'"';
$thelist .= ($file_x == $selected_item) ? ' selected="selected"' : '';
$thelist .= '>'.$file_x.'</option>';

Where $selected_item is the name of the item you wish to select.

 

What do you mean by, get the sub-items in alphabetical order? Since the strings are all of the form...

 

  Legal-passport

  Legal-Gavel 1

  General-Puzzle

  General-People

  Legal-Gavel 2

  Construction-Tractor

  Legal-Gavel 3

...and you are sorting the array or those values, the sub-items should already be in order since General-People alphabetically is before General-Puzzle.

Link to comment
Share on other sites

I appreciate your help, I am learning alot.

 

I added the following:

 

  $file_y = explode("-", $file_x);

 

and changed the list to:

 

  $thelist .= '<option>'.$file_y[1].'</option>';

 

So the prefix would not show in the sublist, picky I know.

 

I guess I am trying to figure out how to sort $file_y[1] without messing up the first sort.

Link to comment
Share on other sites

I don't understand. If you wish to explode $file_x to get rid of the first part that's fine (though not required, it can be done with string manipulation). I don't get how this should effect the order. Let's say the values in the quote of my previous post are all the files in your folder and they load in that order. After you have called sort on the array, it looks like this...

 

Array
(
    [0] => Construction-Tractor
    [1] => General-People
    [2] => General-Puzzle
    [3] => Legal-Gavel 1
    [4] => Legal-Gavel 2
    [5] => Legal-Gavel 3
    [6] => Legal-passport
)

 

As you can see, since the 'categories' are all prefixed and the array is sorted alphabetically, both the categories are in alphabetical order and the 'image names' are in alphabetical order for that category. I don't understand why/what you wish to sort?

Link to comment
Share on other sites

Here is what I have now that seems to be working well, except for the two blank lines at the beginning, I do not know where the . and .. are in this array:

 

<?php
$cur_group = '';
$files = scandir('images/siteimgs');
sort($files);
$thelist .= '<option selected="selected"';
$thelist .= '>Select Site Image</option>';
foreach($files as $file) {
   $file_x = substr($file, 0, -4);
   $file_y = explode("-", $file_x);;
   $group = substr($file, 0, strpos($file, '-'));
   if(empty($cur_group)) {
      $thelist .= '<optgroup label="'.$group.'">';
      $cur_group = $group;
   }
   if($group != $cur_group) {
      $thelist .= '</optgroup>';
      $thelist .= '<optgroup label="'.$group.'">';
      $cur_group = $group;
   }
   $thelist .= '<option value="picture-switcher.php?picture='.$file_x.'">'.$file_y[1].'</option>';
}?>

<select onchange="window.location.href=this.options[this.selectedIndex].value">
<?php echo $thelist; ?>
</select>

Link to comment
Share on other sites

<?php
$cur_group = '';
$files = scandir('images/siteimgs');
sort($files);
$thelist .= '<option selected="selected"';
$thelist .= '>Select Site Image</option>';
foreach($files as $file) {
if ($file == '.' || $file == '..') { continue; }
   $file_x = substr($file, 0, -4);
   $file_y = explode("-", $file_x);;
   $group = substr($file, 0, strpos($file, '-'));
   if(empty($cur_group)) {
      $thelist .= '<optgroup label="'.$group.'">';
      $cur_group = $group;
   }
   if($group != $cur_group) {
      $thelist .= '</optgroup>';
      $thelist .= '<optgroup label="'.$group.'">';
      $cur_group = $group;
   }
   $thelist .= '<option value="picture-switcher.php?picture='.$file_x.'">'.$file_y[1].'</option>';
}
?>

try this.

 

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.