Jump to content

Filtering output


smordue

Recommended Posts

I am using the following code, which you guys helped me with last week.

 

<?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>';
}
?><select onchange="window.location.href=this.options[this.selectedIndex].value">
<?php echo $thelist; ?>
</select>

 

It is working great, but I have a new need and that is to on another page, filter the results to only show images with a certain prefix.

 

My images are like so:

 

Finance-Money.jpg

Legal-Gavel.jpg

 

etc.

 

I would like to add something to the above code so that it only shows the images that start with "Legal-" for example.

 

Thanks for your help, I could not do this without you guys!

 

Steve

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>';
$filter = 'WHATEVER YOUR LOOKING FOR'
foreach($files as $file) {
if ($file == '.' || $file == '..' || strpos($file,$filter) === false) { 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>';
}
?><select onchange="window.location.href=this.options[this.selectedIndex].value">
<?php echo $thelist; ?>
</select>

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.