iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-2007, 08:05 PM
aventvoy's Avatar
WebProWorld Member
 
Join Date: Jul 2003
Location: Vancouver
Posts: 48
aventvoy RepRank 0
Default PHP form and use of <select ...> menu option

Hello,
Here comes today's newbie question... Some will laugh and see this sooooo easy... But I'm stuck...
I am working on a form (page1.php) with username and password, no problem so far...
On this same page (and once username and password are validated, so far I managed), the user must be able to select a file from a file list, in a menu like this:
<select name="file">

<option value="empty">Select a file</option>
<option value="file1">File 1</option>
<option value="file2">File 2</option>
etc...
The files are stored in a folder (./folder).
But in this folder I have many files, .txt or .jpg, and I only want to display the .txt files.
I can do all this in html, no problem, and display the file on a page2.php, but it means I have to manually type as many <option value="etc"...> as there are files in this folder. And there are many!

I assume there must be a way to retrieve all the .txt files and display them in a drop down/select menu for selection, yes?
How can I do that? I read something about traversing a directory (rewind() and dir()) but I don't know how to use these functions and diplay the files in a select menu...
Could anyone help me?
Thanks for your patience with newbies!
A.
__________________
http://www.aventurevoyages.com
Reply With Quote
  #2 (permalink)  
Old 03-05-2007, 06:01 AM
sands's Avatar
WebProWorld Veteran
 
Join Date: Sep 2005
Location: Kerala, India
Posts: 395
sands RepRank 1
Default

Try this:

Code:
<select name="file">
<option value='empty'>Select a file</option> 
<?php
$dirname = "folder/";
$dh = opendir($dirname);
$count = 0;

while ($file = readdir($dh)) {
if (substr($file, -4) == ".txt") {
$count++;
print "<option value='$dirname$file'>File $count</option>\n"; }
}
closedir($dh);
?>
</select>
Did not have time to check it out. But it should work. If not, dabble with it till it does :-)
__________________
My Nook | My Biz | My Photos
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:16 PM.



Search Engine Optimization by vBSEO 3.3.0