1

I have the following select but I dont want it to show .jpg can you guys help me and if I select a name option it has to echo the image file.

    <?php 

    $images = glob("smoelenboek/*.{jpg}", GLOB_BRACE);
    echo '<select name="image">';
    foreach($images as $image)
    {
    echo '<option>' . basename($image) . '</option>';
    }
    echo '</select>';
    echo '<option>' . basename($image) . '</option>'

    ?>
Sumurai8
  • 20,333
  • 11
  • 66
  • 100

4 Answers4

1

You're almost there, use the second parameter to basename():

echo '<option>' . basename($image, '.jpg') . '</option>';

Although, you will need to reconsider this method should you start to list files with other suffixes

NDM
  • 6,731
  • 3
  • 39
  • 52
AlexP
  • 9,906
  • 1
  • 24
  • 43
0

The second parameter of basename allows you to cut off the extension, if it is present:

echo basename('test.jpg', '.jpg'); // echoes test

note that while basename accepts a path as first parameter, it does not actually check the file system to validate the file, any string can thus be passed.

edit

For the additional question about displaying the images itself in the select: as seen in this question that is not really possible in a cross browser way.

You could use custom components for this like the jQuery Selectable

Community
  • 1
  • 1
NDM
  • 6,731
  • 3
  • 39
  • 52
  • I want it to echo the full image not only the name if that is possible – Baki Yüksel Sep 13 '13 at 10:57
  • what do you mean "echo the full image"? display the image in the select? – NDM Sep 13 '13 at 10:59
  • A select does not allow you to display images inside it, you will have to use a custom component for this. see [this question](http://stackoverflow.com/questions/2965971/how-to-add-a-images-in-select-list) – NDM Sep 13 '13 at 11:01
  • hmm okey i will search for that and if i want it to show the image in an other div? – Baki Yüksel Sep 13 '13 at 11:04
  • That's another question entirely, which I will not try to resolve in a comment :) – NDM Sep 13 '13 at 11:05
0

try this

$images = glob("smoelenboek/*.{jpg}", GLOB_BRACE);
echo '<select name="image">';
foreach($images as $image)
{
echo '<option value="'.basename($image, '.jpg').'">' . basename($image) . '</option>';
}
echo '</select>';
developerCK
  • 4,418
  • 3
  • 16
  • 35
0

try this one

<?php 

        $images = glob("smoelenboek/*.{jpg}", GLOB_BRACE);
        echo '<select name="image">';
        foreach($images as $image)
        {
        echo '<option style="background: url('. $image . ') no-repeat; ">'. basename($image) . '</option>';
        }
        echo '</select>';
       echo '<option style="background: url('. $image . ') no-repeat;">'. basename($image) . '</option>';

        ?>
ratnesh dwivedi
  • 352
  • 2
  • 8