0

i'm trying to make an autocomplete that will display the name of files in a folder. The autocomplete is ok except the part that it only display for the last file name. your help and suggestions much appreciated! =)

code to open the folder and list the excel file names.

if ($handle = opendir('C:AppServ/data')) {
    echo "Directory handle: $handle\n";
   echo "Entries:\n";


    while (false !== ($entry = readdir($handle))) {

       $withext = basename("$entry", ".d").PHP_EOL;

        $info = pathinfo($withext);
        $file_name = basename($withext,'.'.$info['extension']);
        $only_year = preg_replace( "/[^0-9]/", "", $file_name );
            $only_file = preg_replace( "/[^a-z]/i", "", $file_name );

            }

closedir($handle);
}

autocomplete code

 <script>
    $(function() {
        var availableTags = ["<?php echo $only_file; ?>"

        ];
        $( "#tags" ).autocomplete({
            source: availableTags
        });
    });
    </script>
2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by eyeda
2

Consider the logic of your loop.

Loop through each file:
    Assign file name to $only_file

Print $only_file

Each iteration of the loop assigns the name of the current file to the variable, overwriting whatever value was assigned to it before. So, at the end of it all, only the last file name is in there.

What you need to do is store each file name in an array, and then print each item in the array into your Javascript array. Something like this:

<?php
$files = array();
while (/* there are valid files */) {
    $file_only = /* insert code to get file name here */
    $files[] = $file_only;
}
?>

<script>
var availableTags = ["<?php echo implode('","', $files); ?>"];
</script>
Votes + Comments
Clear and concise explanation.
0

Thank you @Atli for your response.I've done as your suggestion. Is this right?

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery UI Autocomplete - Default functionality</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />


<?php 
if ($handle = opendir('C:AppServ/data')) {



$files = array();
while (false !== ($entry = readdir($handle))) 
{   
  echo $entry;

 $file_only = $entry;
    $files[] = $file_only;
    }
    }


    ?>

   <script>
   var availableTags = ["<?php echo implode('","', $files); ?>"];
    $( "#tags" ).autocomplete({
            source: availableTags
        });
    });


   </script>

   </head>
<body>

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>


</body>
</html>

Its display nothing. I'm sorry if i misunderstood u.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.