trecool999 Posted February 12, 2007 Share Posted February 12, 2007 How could I add a filter to this script: <? echo "<form>"; echo "<select name='Files'>"; $dirpath = "../Text_Editor/"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { if (!is_dir("$dirpath/$file")) { echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; } } closedir($dh); echo "</select>"; echo "</form>"; ?> So that you can only see .txt files? Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/ Share on other sites More sharing options...
wildteen88 Posted February 12, 2007 Share Posted February 12, 2007 See if changing: while (false !== ($file = readdir($dh))) { if (!is_dir("$dirpath/$file")) { echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; } to this works: while (false !== ($file = readdir($dh))) { echo substr($file, -4, 4) . "<br />\n"; if(substr($file, -4, 4) == '.txt') { if (!is_dir($dirpath . '/' . $file)) { echo ' <option value="' . $file . '">' . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "</option>\n"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182976 Share on other sites More sharing options...
trecool999 Posted February 12, 2007 Author Share Posted February 12, 2007 Argh! I just found out the original script doesn't work now either... I'll see to the problem in a few minutes. Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182978 Share on other sites More sharing options...
wildteen88 Posted February 12, 2007 Share Posted February 12, 2007 Rety my code again. I had major problems with it. I recoded and tested and you script works fine. <?php echo "<form> <select name=\"Files\">\n"; $dirpath = "./"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { if(substr($file, -4, 4) == '.txt') { if (!is_dir($dirpath . '/' . $file)) { echo ' <option value="' . $file . '">' . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "</option>\n"; } } } closedir($dh); echo "</select> </form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182980 Share on other sites More sharing options...
trecool999 Posted February 12, 2007 Author Share Posted February 12, 2007 Sweetness ! Works like a charm, do you know of any way to use 'onClick' with each option? If not, don't worry and thanks! Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182983 Share on other sites More sharing options...
wildteen88 Posted February 12, 2007 Share Posted February 12, 2007 You will want to add it to the select tag. So if you do this: echo "<form name=\"file\"> <select name=\"Files\" onchange=\"document.file.submit();\">\n"; When you choose an option it will submit the form. Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182998 Share on other sites More sharing options...
trecool999 Posted February 12, 2007 Author Share Posted February 12, 2007 No, what I meant was, if you click on one of the options, it will copy that option into the text box, but I just figured that out, so... yeah, s'all good. I stuck this: <?php echo "<form> <select name=\"Files\">\n"; $dirpath = "./"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { if(substr($file, -4, 4) == '.txt') { if (!is_dir($dirpath . '/' . $file)) { echo ' <option value="' . $file . '" onClick="form1.Title.value += \'' . $file . '\'">' . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "</option>\n"; } } } closedir($dh); echo "</select> </form>"; ?> In an include file. New problem When it copies it into to the box, it brings the .txt extension with it, even though it uses the exact same variable ($file) as the display in the list does... any solution? Or is there any way I could cut it out when it processes after submission? Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-183002 Share on other sites More sharing options...
wildteen88 Posted February 12, 2007 Share Posted February 12, 2007 It is bescause you are passing it with unmodified value for the file variable. But then later on you modify the value on the variable to remove the extension. Simple change this: echo ' <option value="' . $file . '" onClick="form1.Title.value += \'' . $file . '\'">' . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "</option>\n"; to this: $f = htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))); echo ' <option value="' . $file . '" onClick="form1.Title.value += \'' . $f . '\'">' . $f . "</option>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-183012 Share on other sites More sharing options...
trecool999 Posted February 12, 2007 Author Share Posted February 12, 2007 Woohoo! Thanks, that's all I needed to know. Now, over the the Javascript forum! Quote Link to comment https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-183018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.