smordue Posted December 22, 2009 Share Posted December 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/ Share on other sites More sharing options...
salathe Posted December 22, 2009 Share Posted December 22, 2009 Is this possible? Of course! One way would be to first construct a nice array which has the structure that you're looking for (grouping items by their category) and then simply loop over that array writing out the optgroups. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982171 Share on other sites More sharing options...
tomcant Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982187 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982191 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982344 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 Just a typo, the line before that if statement should have a semi-colon at the end. There may be other issues, I didn't test the code. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982352 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 Thanks cags, I added the semi-colin and now it does the same thing as tomcants. Multiple instances of the optgroup. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982367 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 Echo out the contents of $thelist and post it on here. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982368 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 It is echoing the select box, but the contents look like this: Legal Legal-passport Legal-Gavel 1 General General-Puzzle General-People Legal Legal-Gavel 2 Construction Construction-Tractor Legal Legal-Gavel 3 etc.. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982375 Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 From the looks of the output its not reading your filesystem in alphabetical order.. it might be best to build an array first, sort it, then create the options. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982379 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982426 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 cags, you are indeed a PHP Freak. Two more minor items, how can I have the sublist display alphabetically? And how can I set <option selected="selected" >Select Site Image</option> so it will show on load? Steve Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982514 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982544 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 Sorry, I was unaware that the function would overwrite my default selected option, I promise that is all I need now. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982547 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982564 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982569 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982574 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 I must have forgotton to refresh, it seem right now. Any idea why I am getting two blank lines at the top of the dropdown list before the options? Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982588 Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 The scandir function will return . and .. as "files". You need to remove these from your array.. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982589 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982593 Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982597 Share on other sites More sharing options...
smordue Posted December 22, 2009 Author Share Posted December 22, 2009 Thanks tomcant, cags and Buddski, I am all good now thanks to you guys. Quote Link to comment https://forums.phpfreaks.com/topic/185959-breaking-apart-filename-for-select/#findComment-982601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.