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>

Recommended Answers

All 3 Replies

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>
commented: 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.

okey..i've solved this.
Thanks Atli! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.