Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/38211-solved-quick-help/
Share on other sites

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";
        }
    }
}

Link to comment
https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182976
Share on other sites

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>";

?>

Link to comment
https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-182980
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-183002
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/38211-solved-quick-help/#findComment-183012
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.