I'm trying to write a simple php script that will populate a drop down unordered list with the file contents of a directory. Specifically .wav files. I'm HTML using and Bootstrap to do so.
Here is the list I've been working on:
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<h4 style="color: #333333">Select a Sound -</h4> <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">Moose.wav</a></li>
<li><a href="#">Duck.wav</a></li>
<li><a href="#">Goose.wav</a></li>
</ul>
</li>
</ul>
<?php
foreach(glob('sounds/*.wav') as $filename){
$rest = substr($filename, 7);
echo "<option>".$rest."</option>";
}
?>
Right now it's populated with hard coded values, Moose, Duck, etc . . . I've been working on this php script, I just don't know how to combine the two.
Is this what you're looking for?
<ul>
<?php
foreach(glob('sounds/*.wav') as $filename){
$rest = substr($filename, 7);
echo "<li><a href='#'>".$rest."</a></li>";
}
?>
</ul>
I'm writing this based on the assumption that all the code you provided are working perfectly as intended.
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<h4 style="color: #333333">Select a Sound -</h4> <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<?php foreach(glob('sounds/*.wav') as $filename){
$rest = substr($filename, 7);
echo "<li>".$rest."</li>";
}
?>
</ul>
</li>
</ul>
The idea is to write a PHP loop to generate your list items. This loop would reside within the tags, hence making the list.
You can use basename() to get only the filename, no need for substr()
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<h4 style="color: #333333">Select a Sound -</h4> <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<?php foreach(glob('sounds/*.wav') as $filename):?>
<li><a href="#"><?php echo basename($filename);?></a></li>
<?php endforeach;?>
</ul>
</li>
</ul>