ameisez Posted January 4, 2009 Share Posted January 4, 2009 Hi,I found this select tag herehttp://www.w3schools.com/TAGS/tag_Select.aspI need help to be able to generate statement like that in order to be able to put all my folder content in a pulldown menu and links to it.Anyone who can help me please.Thanks Link to comment Share on other sites More sharing options...
jeffman Posted January 4, 2009 Share Posted January 4, 2009 Pretty basic: <select><?php $opt_str = ''; $dirname = 'path_to_your_directory'; // if ($D = opendir($dirname)) { while (false !== ($file = readdir($D))) { if ($file != '.' && $file != '..') { $opt_str .= "<option value=\"$file\">$file</option>\n"; } } closedir($D); echo $opt_str; }?></select> EDIT: it works now. Duh! Link to comment Share on other sites More sharing options...
ameisez Posted January 5, 2009 Author Share Posted January 5, 2009 Hi, Deirdre's Dad,Thanks for your reply. Sadly, I am worst than basic :)I copy your code put mydir name and upload it in a folder along side with mydir. Here's the final code. <select><?php $opt_str = ''; $dirname = 'mydir'; // if ($D = opendir($dirname)) { while (false !== ($file = readdir($D))) { if ($file != '.' && $file != '..') { $opt_str += "<option value=\"$file\">$file</option>\n"; } } closedir($D); echo $opt_str; }?></select> When I view the page, it comes up with a pulldown box with nothing on it.I tried to supply '.mp3' on $opt_str What am I missing?Thanks Link to comment Share on other sites More sharing options...
Synook Posted January 5, 2009 Share Posted January 5, 2009 Try using glob() Link to comment Share on other sites More sharing options...
ameisez Posted January 5, 2009 Author Share Posted January 5, 2009 Try using glob()Sorry.. where should i put glob()?thanks Link to comment Share on other sites More sharing options...
Wander Posted January 5, 2009 Share Posted January 5, 2009 glob(..) returns an array of files that match the first argument<?php echo '<select>'; $files = glob('some_folder/*.mp3'); foreach($files as $file){ echo '<option>'.$file.'</option>'; } echo '</select>';?> Link to comment Share on other sites More sharing options...
jeffman Posted January 5, 2009 Share Posted January 5, 2009 What am I missing? My bad. Basic indeed! I mixed up PHP and Javascript.+= should be .=. is the string concatenation character in PHP.glob() is a good route, also. If you're using Wander's code, you're probably getting the name of the directory at the beginning of each file name. If so, change the glob statement to these 2 statements:chdir('your_directory'); // CHANGE DIRECTORY$files = glob('*.mp3'); Link to comment Share on other sites More sharing options...
ameisez Posted January 6, 2009 Author Share Posted January 6, 2009 My bad. Basic indeed! I mixed up PHP and Javascript.+= should be .=. is the string concatenation character in PHP.glob() is a good route, also. If you're using Wander's code, you're probably getting the name of the directory at the beginning of each file name. If so, change the glob statement to these 2 statements:chdir('your_directory'); // CHANGE DIRECTORY$files = glob('*.mp3'); WOW!! It works.Thanks Link to comment Share on other sites More sharing options...
ameisez Posted August 27, 2009 Author Share Posted August 27, 2009 Hi all,First, thanks for the help you've given me on this.Now, if i may bring this question back to life for an upgrade, may I know how can I add the following features.1. how to style the select (ie color, font, etc) I would prefer an inline styling method. I have an idea how it is done in html but not in php2. how can I make the items downloadable via double click? (click will make it play on my player and double click will download)Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted August 27, 2009 Share Posted August 27, 2009 You only use PHP to print HTML, styling information doesn't change just because the HTML is coming from PHP. You can add your style attribute to the select and option tags the same way you normally would. Select lists don't support double-clicking, I don't think there's a way to override that. As soon as you click a list item it closes the list, it doesn't give you a chance to double-click. You might be able to use Javascript to handle the click event on an option element to return false for the first click (maybe the list will stay open) and listen for another click within a certain amount of time, but that's going to break single-click functionality. It would be better if you just have a download button or something that gets the value from the dropdown and redirects to the file it references. Link to comment Share on other sites More sharing options...
ameisez Posted August 28, 2009 Author Share Posted August 28, 2009 It would be better if you just have a download button or something that gets the value from the dropdown and redirects to the file it references.I like the idea.. can you please help me do it this way. thanks Link to comment Share on other sites More sharing options...
GerryH Posted August 28, 2009 Share Posted August 28, 2009 ... Select lists don't support double-clicking, I don't think there's a way to override that. As soon as you click a list item it closes the list, it doesn't give you a chance to double-click. You might be able to use Javascript to handle the click event on an option element to return false for the first click (maybe the list will stay open) and listen for another click within a certain amount of time, but that's going to break single-click functionality. It would be better if you just have a download button or something that gets the value from the dropdown and redirects to the file it references.You Might want to try this;<html><head><script type="text/javascript">function on_dblclick(id){ var x = document.getElementById(id); var value = x.value; var y = document.getElementById(value); alert(y.innerHTML); x.size = '1';}function on_click(){ document.getElementById("1").size = 4;}</script></head><body><select id="1" onclick="on_click();" ondblclick="on_dblclick('1');" size=1> <option id="v" value="v">Volvo</option> <option id="s" value="s">Saab</option> <option id="m" value="m">Mercedes</option> <option id="a" value="a">Audi</option></select> </body></html> The downside of course is that a lengty list can pollute a page, but considering the <menu> is depreciated, it's a workaround I found satisfactory. Of course if anyone knows a better solution, please share! Link to comment Share on other sites More sharing options...
ameisez Posted September 2, 2009 Author Share Posted September 2, 2009 @Gerry H,Thanks. That should have solved my problem if I got a short list. but you are right. It is kinda polluting when there's too much on the list.Perhaps I need to make two list controls. One that would play the mp3 and the other would download. Can you help me on this? can you also check my html code below with embedded php inside. the php code is working alone but when it is inside the html it doesn't.@justsomeguyI like your idea of download button. Can you help me set it up?Here's the .php page that list the mp3 files <?php echo '<select size=5>'; chdir('mp3'); //change directory $files = glob('*.mp3'); foreach($files as $file){ echo '<option>'.$file.'</option>'; } echo '</select>';?> This is the HTML file where I want the same list to appear but It doesn't <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-gb" xml:lang="en-gb"><title>TEST PAGE - DYNAMIC SELECT</title><body> <select > <?php chdir('mp3'); //change directory $files = glob('*.mp3'); foreach($files as $file){ echo '<option>'.$file.'</option>'; } ?> </select></body></html> Here's the questionsWhy the php code inside my html is not working?How can I add the download button to download the currently selected file on the list?TIA Link to comment Share on other sites More sharing options...
ulisses Posted September 2, 2009 Share Posted September 2, 2009 Here's the .php page that list the mp3 files<?php echo '<select size=5>'; chdir('mp3'); //change directory $files = glob('*.mp3'); foreach($files as $file){ echo '<option>'.$file.'</option>'; } echo '</select>';?> This is the HTML file where I want the same list to appear but It doesn't <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-gb" xml:lang="en-gb"><title>TEST PAGE - DYNAMIC SELECT</title><body> <select > <?php chdir('mp3'); //change directory $files = glob('*.mp3'); foreach($files as $file){ echo '<option>'.$file.'</option>'; } ?> </select></body></html> Why the php code inside my html is not working? I think you should insert php before the select tag. I mean, you should try something like this:<?phpecho '<select>';chdir('mp3');...echo '</select>';?>I may be wrong, because i'm a begginer but that's how I see it. Regards. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 2, 2009 Share Posted September 2, 2009 This is the HTML file where I want the same list to appear but It doesn'tMake sure it's a PHP file, PHP code doesn't run inside regular HTML files. It needs to be named .php. If the folder listing code isn't working, if it's not listing any files, make sure the path to the folder is correct from wherever the PHP script is.If you have a select element with an ID, where the value of each option is a URL, like this:<select id="songs"><option value="path/to/file1.mp3">file1</option><option value="path/to/file2.mp3">file2</option><option value="path/to/file3.mp3">file3</option></select>you can set up a button like this:<button onclick="window.open(document.getElementById('songs').value, '_blank');">download</button> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.